go_away

Author Topic: Pushbuttons and other inputs?  (Read 2915 times)

0 Members and 1 Guest are viewing this topic.

Offline GalacticNerdTopic starter

  • Jr. Member
  • **
  • Posts: 46
  • Helpful? 0
Pushbuttons and other inputs?
« on: April 07, 2009, 02:54:57 PM »
Hello,

I've been working on a project lately that is not really a robot,
but it uses a microprocessor (atmega8) and prompts for a button input (out of 7 buttons)
and displays the output on a Nokia 3310 LCD.
Now, I allready have the lcd hooked up, and have a library for processing the output to the screen,
but I really have no Idea howto hook up the buttons, and how you recognize and declare the buttonpress in the program.

I hope you can help me out.
Kenny
ps: if some could show me the shematic for a piezo speaker and how to control it, that would be awesome.
« Last Edit: April 07, 2009, 02:58:30 PM by GalacticNerd »

Offline Robotboy86

  • Robot Overlord
  • ****
  • Posts: 139
  • Helpful? 0
Re: Pushbuttons and other inputs?
« Reply #1 on: April 07, 2009, 03:15:14 PM »
Button presses are very simple really.  First thing you need are the actual buttons.  Which I assume you have selected.

Buttons typically have 2 leads coming off of them.  One of these you will connect to an open Atmega digital pin.  The other will be connected to a 5v source.

The button sits between the Atmega and the 5v source, when you push the button, it will connect the Atmega and the 5v source, thus providing the Atmega with a "1"(Digital for ON). 

If you need more help let me know, I can pull some datasheets and tell you which pins, where, code, etc etc :)

Offline GalacticNerdTopic starter

  • Jr. Member
  • **
  • Posts: 46
  • Helpful? 0
Re: Pushbuttons and other inputs?
« Reply #2 on: April 07, 2009, 03:22:02 PM »
Button presses are very simple really.  First thing you need are the actual buttons.  Which I assume you have selected.

Buttons typically have 2 leads coming off of them.  One of these you will connect to an open Atmega digital pin.  The other will be connected to a 5v source.

The button sits between the Atmega and the 5v source, when you push the button, it will connect the Atmega and the 5v source, thus providing the Atmega with a "1"(Digital for ON). 

If you need more help let me know, I can pull some datasheets and tell you which pins, where, code, etc etc :)

wow, thanks a lot for the quick reply!
I could use some extra information.. wich are the digital pins?
A shematic would be useful.

Offline SmAsH

  • Supreme Robot
  • *****
  • Posts: 3,959
  • Helpful? 75
  • SoR's Locale Electronics Nut.
Re: Pushbuttons and other inputs?
« Reply #3 on: April 07, 2009, 03:25:30 PM »
this may help. this is the pinout for atmega168 but they are the same pinouts.
« Last Edit: April 07, 2009, 03:26:45 PM by SmAsH »
Howdy

Offline GalacticNerdTopic starter

  • Jr. Member
  • **
  • Posts: 46
  • Helpful? 0
Re: Pushbuttons and other inputs?
« Reply #4 on: April 07, 2009, 03:37:16 PM »
this may help. this is the pinout for atmega168 but they are the same pinouts.


Thanks! This helps a bit. *prints it out*
But I don't understand this 100% (maybe because my mother tongue is not english)


Buttons typically have 2 leads coming off of them.  One of these you will connect to an open Atmega digital pin.  The other will be connected to a 5v source.

The button sits between the Atmega and the 5v source, when you push the button, it will connect the Atmega and the 5v source, thus providing the Atmega with a "1"(Digital for ON). 

If you need more help let me know, I can pull some datasheets and tell you which pins, where, code, etc etc :)

But that doesn't make sense for me.. __-(Battery)+____(button)___(ATmega8)
That's not even a closed circuit?

Any adittional info would be great.
Sorry for all the nooby questions. I'm just getting started.

Offline GearMotion

  • Supreme Robot
  • *****
  • Posts: 489
  • Helpful? 24
  • Two decades+ of Embedded Design
Re: Pushbuttons and other inputs?
« Reply #5 on: April 07, 2009, 03:42:36 PM »
Buttons typically have 2 leads coming off of them.  One of these you will connect to an open Atmega digital pin.  The other will be connected to a 5v source.
The button sits between the Atmega and the 5v source, when you push the button, it will connect the Atmega and the 5v source, thus providing the Atmega with a "1"(Digital for ON). 

When the button is pushed it is connected to 5V and registers to reading that port as a "1", or high, or 5V (whatever nomenclature you are comfortable with).

The trouble is that when the button is NOT pushed, then nothing is connected to the port. When nothing is connected to the port, the port input is "floating". A floating digital input can be subject to being at an unknown voltage level, which could also register as a 1/high/5V when the port is read.

The solution is to use a high value resistor to pull the input to a known state. In this case, a resistor connected to the input and to ground. Again in this case, a value of 10,000 to 100,000 ohms would be appropriate. The resistor pulls the floating input to 0/low/0V and the switch pulls the input to 1/high/5V.

It is equally valid to pull the input up to 5V (hence the term "pull up") and have the switch connect to ground.


But I don't understand this 100% (maybe because my mother tongue is not english)

Perhaps you should share how you have the LCD connected to the ATmega8. I'm sure you can get help using the remaining pins as button inputs.


But that doesn't make sense for me.. __-(Battery)+____(button)___(ATmega8)
That's not even a closed circuit?

Take my advice above. The ATmega8 input sees either 5V or 0V on the input. See this little schematic: (replace U401 with Atmega8 input)


« Last Edit: April 07, 2009, 03:59:05 PM by GearMotion »

Offline GalacticNerdTopic starter

  • Jr. Member
  • **
  • Posts: 46
  • Helpful? 0
Re: Pushbuttons and other inputs?
« Reply #6 on: April 07, 2009, 03:58:22 PM »
Buttons typically have 2 leads coming off of them.  One of these you will connect to an open Atmega digital pin.  The other will be connected to a 5v source.
The button sits between the Atmega and the 5v source, when you push the button, it will connect the Atmega and the 5v source, thus providing the Atmega with a "1"(Digital for ON). 

When the button is pushed it is connected to 5V and registers to reading that port as a "1", or high, or 5V (whatever nomenclature you are comfortable with).

The trouble is that when the button is NOT pushed, then nothing is connected to the port. When nothing is connected to the port, the port input is "floating". A floating digital input can be subject to being at an unknown voltage level, which could also register as a 1/high/5V when the port is read.

The solution is to use a high value resistor to pull the input to a known state. In this case, a resistor connected to the input and to ground. Again in this case, a value of 10,000 to 100,000 ohms would be appropriate. The resistor pulls the floating input to 0/low/0V and the switch pulls the input to 1/high/5V.

It is equally valid to pull the input up to 5V (hence the term "pull up") and have the switch connect to ground.




I have some basic understanding of electronics, so I understand what you are trying to say.
But however, I still don't see howto wire it up.. Like you can see in the pic I attached, that would result in an open circuit..

Offline GearMotion

  • Supreme Robot
  • *****
  • Posts: 489
  • Helpful? 24
  • Two decades+ of Embedded Design
Re: Pushbuttons and other inputs?
« Reply #7 on: April 07, 2009, 04:01:17 PM »
GalacticNerd:

See my previous post.

But also in your circuit the 5V supply has a ground connection that would share the connection to ground with the ATmega8 ground pins.

Offline chelmi

  • Supreme Robot
  • *****
  • Posts: 496
  • Helpful? 15
Re: Pushbuttons and other inputs?
« Reply #8 on: April 07, 2009, 04:03:10 PM »
Buttons typically have 2 leads coming off of them.  One of these you will connect to an open Atmega digital pin.  The other will be connected to a 5v source.
The button sits between the Atmega and the 5v source, when you push the button, it will connect the Atmega and the 5v source, thus providing the Atmega with a "1"(Digital for ON). 


When the button is pushed it is connected to 5V and registers to reading that port as a "1", or high, or 5V (whatever nomenclature you are comfortable with).

The trouble is that when the button is NOT pushed, then nothing is connected to the port. When nothing is connected to the port, the port input is "floating". A floating digital input can be subject to being at an unknown voltage level, which could also register as a 1/high/5V when the port is read.

The solution is to use a high value resistor to pull the input to a known state. In this case, a resistor connected to the input and to ground. Again in this case, a value of 10,000 to 100,000 ohms would be appropriate. The resistor pulls the floating input to 0/low/0V and the switch pulls the input to 1/high/5V.

It is equally valid to pull the input up to 5V (hence the term "pull up") and have the switch connect to ground.



I recommend LadyAda tutorial on switches http://www.ladyada.net/learn/arduino/lesson5.html


Offline GalacticNerdTopic starter

  • Jr. Member
  • **
  • Posts: 46
  • Helpful? 0
Re: Pushbuttons and other inputs?
« Reply #9 on: April 07, 2009, 04:08:11 PM »
GalacticNerd:

See my previous post.

But also in your circuit the 5V supply has a ground connection that would share the connection to ground with the ATmega8 ground pins.
Okay, I think I get it. Here is the schematic for the lcd:

Offline GearMotion

  • Supreme Robot
  • *****
  • Posts: 489
  • Helpful? 24
  • Two decades+ of Embedded Design
Re: Pushbuttons and other inputs?
« Reply #10 on: April 07, 2009, 04:14:13 PM »
All of the pin connections to port D are available. Duplicate the resistor/switch schematic I posted above for each of 7 inputs.

Offline galannthegreat

  • Supreme Robot
  • *****
  • Posts: 615
  • Helpful? 4
  • Blue-Lensed Blue LEDs?! What?! Impossible!!
Re: Pushbuttons and other inputs?
« Reply #11 on: April 07, 2009, 07:43:05 PM »
I have a question a tad bit off of your topic, what model number Nokia LCD are you using and where can I get one? I've been trying to get a Nokia LCD for awhile without success. :'(
Kurt

Offline GalacticNerdTopic starter

  • Jr. Member
  • **
  • Posts: 46
  • Helpful? 0
Re: Pushbuttons and other inputs?
« Reply #12 on: April 07, 2009, 10:51:37 PM »
I have a question a tad bit off of your topic, what model number Nokia LCD are you using and where can I get one? I've been trying to get a Nokia LCD for awhile without success. :'(

Actually, I ripped it out of an actual 3310.(NHM-5NK)

Offline GalacticNerdTopic starter

  • Jr. Member
  • **
  • Posts: 46
  • Helpful? 0
Re: Pushbuttons and other inputs?
« Reply #13 on: April 07, 2009, 10:56:11 PM »
All of the pin connections to port D are available. Duplicate the resistor/switch schematic I posted above for each of 7 inputs.
Thanks for helping me out, I was a bit confused ;)

Offline GalacticNerdTopic starter

  • Jr. Member
  • **
  • Posts: 46
  • Helpful? 0
Re: Pushbuttons and other inputs?
« Reply #14 on: April 08, 2009, 03:45:36 AM »
...
ANother thing, how do you declare and recognize them in your code?

Offline SmAsH

  • Supreme Robot
  • *****
  • Posts: 3,959
  • Helpful? 75
  • SoR's Locale Electronics Nut.
Re: Pushbuttons and other inputs?
« Reply #15 on: April 08, 2009, 03:52:11 AM »
recognize a switch?
Howdy

Offline GalacticNerdTopic starter

  • Jr. Member
  • **
  • Posts: 46
  • Helpful? 0
Re: Pushbuttons and other inputs?
« Reply #16 on: April 08, 2009, 03:53:34 AM »
recognize a switch?


Yes, I just want to know how to declare and name a switch..

Offline SmAsH

  • Supreme Robot
  • *****
  • Posts: 3,959
  • Helpful? 75
  • SoR's Locale Electronics Nut.
Re: Pushbuttons and other inputs?
« Reply #17 on: April 08, 2009, 03:58:25 AM »
im not too sure on the code side of things but heres the arduino code which uses an atmega168 which is same pinout.

int inPin = 2;   // choose the input pin (for a pushbutton)


void setup() {
  pinMode(inPin, INPUT);    // declare pushbutton as input
}

im not too sure if thats right but it should give you an idea of what it will look like. with the inpin part of things i think you can change the 'in' part of things but im not sure, this is with the arduino after all which may have different code.
Howdy

Offline GalacticNerdTopic starter

  • Jr. Member
  • **
  • Posts: 46
  • Helpful? 0
Re: Pushbuttons and other inputs?
« Reply #18 on: April 08, 2009, 04:03:50 AM »
im not too sure on the code side of things but heres the arduino code which uses an atmega168 which is same pinout.

int inPin = 2;   // choose the input pin (for a pushbutton)


void setup() {
  pinMode(inPin, INPUT);    // declare pushbutton as input
}

im not too sure if thats right but it should give you an idea of what it will look like. with the inpin part of things i think you can change the 'in' part of things but im not sure, this is with the arduino after all which may have different code.




I see...Mine is actually not a complete board, just an ATmega connected to some switches and an lcd, without the use of a pcb.
In your example, you named the switch inPin, or am I mistaken?

Offline SmAsH

  • Supreme Robot
  • *****
  • Posts: 3,959
  • Helpful? 75
  • SoR's Locale Electronics Nut.
Re: Pushbuttons and other inputs?
« Reply #19 on: April 08, 2009, 04:05:23 AM »
yes indeed i did, you can change it but i would leave it as inpin for simplicity, are you using a breadboard?
Howdy

Offline GalacticNerdTopic starter

  • Jr. Member
  • **
  • Posts: 46
  • Helpful? 0
Re: Pushbuttons and other inputs?
« Reply #20 on: April 08, 2009, 04:15:43 AM »
yes indeed i did, you can change it but i would leave it as inpin for simplicity, are you using a breadboard?

No, at the moment, I'm just drawing everything out, and when I'm 100% sure the hardware part is okay, I'll hook it up on a prototyping board.
Thanks for helping. Btw, it can't be named that way, because I'll have 7 buttons. So any name will do?

Offline SmAsH

  • Supreme Robot
  • *****
  • Posts: 3,959
  • Helpful? 75
  • SoR's Locale Electronics Nut.
Re: Pushbuttons and other inputs?
« Reply #21 on: April 08, 2009, 04:18:28 AM »
i think they will as long as its 'blahpin' i think it needs pin at the end. but dont be sure, this is for arduino, not sure if they are the same. but they should be extremly similar.
Howdy

Offline GalacticNerdTopic starter

  • Jr. Member
  • **
  • Posts: 46
  • Helpful? 0
Re: Pushbuttons and other inputs?
« Reply #22 on: April 08, 2009, 04:22:14 AM »
i think they will as long as its 'blahpin' i think it needs pin at the end. but dont be sure, this is for arduino, not sure if they are the same. but they should be extremly similar.



Okay, I'll keep that in mind.
Btw, this is more of a c/c++ question, but can I put the code to declare my buttons in a .h file and just include it?

Offline SmAsH

  • Supreme Robot
  • *****
  • Posts: 3,959
  • Helpful? 75
  • SoR's Locale Electronics Nut.
Re: Pushbuttons and other inputs?
« Reply #23 on: April 08, 2009, 04:28:49 AM »
maybe, as i said im not much on the programming side of things. i would say wait for someone who has more experience programming than myself as i do not want to confirm anything. open up the header file 'SoR_Utils' to see some examples of code.
Howdy

Offline GearMotion

  • Supreme Robot
  • *****
  • Posts: 489
  • Helpful? 24
  • Two decades+ of Embedded Design
Re: Pushbuttons and other inputs?
« Reply #24 on: April 08, 2009, 07:30:48 AM »
yes indeed i did, you can change it but i would leave it as inpin for simplicity, are you using a breadboard?


No, at the moment, I'm just drawing everything out, and when I'm 100% sure the hardware part is okay, I'll hook it up on a prototyping board.
Thanks for helping. Btw, it can't be named that way, because I'll have 7 buttons. So any name will do?



"inPin" is just a variable name and can be named almost anything. (there are rules for naming variables) Best to stick to alphanumerics, though.

All that that statement did was assign the variable "inPin" the value of 2. When "inPin" is used later in the code, it has the value of 2.

Using a define is a better way to do this as it doesn't waste RAM. But I don't think it would do you any good to describe that until you have _some_ sense of programming in C.


To get some information about programming that you can find yourself (rather than being taught in this thread), please look through the tutorials on this site and then look here:

http://www.learn-c.com

http://www.learn-c.com/experiment1.htm



« Last Edit: April 08, 2009, 07:40:46 AM by GearMotion »

Offline GalacticNerdTopic starter

  • Jr. Member
  • **
  • Posts: 46
  • Helpful? 0
Re: Pushbuttons and other inputs?
« Reply #25 on: April 08, 2009, 07:42:44 AM »
yes indeed i did, you can change it but i would leave it as inpin for simplicity, are you using a breadboard?


No, at the moment, I'm just drawing everything out, and when I'm 100% sure the hardware part is okay, I'll hook it up on a prototyping board.
Thanks for helping. Btw, it can't be named that way, because I'll have 7 buttons. So any name will do?



"inPin" is just a variable name and can be named almost anything. Best to stick to alphanumerics, though.

All that that statement did was assign the variable "inPin" the value of 2. When "inPin" is used later in the code, it has the value of 2.

Using a define is a better way to do this as it doesn't waste RAM. But I don't think it would do you any good to describe that until you have _some_ sense of programming in C.


To get some information about programming that you can find yourself (rather than being taught in this thread), please look through the tutorials on this site and then look here:



http://www.learn-c.com

http://www.learn-c.com/experiment1.htm





Well, I have some basic C knowledge, I used to code in PAlib for nds.
I just want to be clear about everything.

Offline GearMotion

  • Supreme Robot
  • *****
  • Posts: 489
  • Helpful? 24
  • Two decades+ of Embedded Design
Re: Pushbuttons and other inputs?
« Reply #26 on: April 08, 2009, 09:33:25 AM »
Well, I wasn't sure. It is hard to judge from this side of the keyboard. Didn't intend to offend.

SmAsH's example shows a way to declare a variable and then set that port to input (using a function call). In embedded design understanding the system is, in general, a tight mix of understanding the language that you are using, the hardware, and how they interact. It can be a tough hurdle.

There are good tutorials related to Arduino (based on the mega168, but applies to other AVRs) which you can easily transpose to what you need to do. There is a good book that might help you tie all of these concepts together and clear up some of the details for you. Take a look here: http://www.circuitgizmos.com/products/cgbookgoodie/cgbookgoodie.shtml


 


Get Your Ad Here