Arduino Forum

Using Arduino => Programming Questions => Topic started by: Feonx on Jul 11, 2011, 02:26 pm

Title: [ Arduino UNO ] Button state change
Post by: Feonx on Jul 11, 2011, 02:26 pm
Hello,

I've learned at school how to work with a Arduino Duemilanove. I liked playing around with it, so I bought my self a Arduino UNO. Today I try to create a button to turn a led on or off.

I put something together on my breadboard and uploaded the button script example. The script works and the LED can be turned on and off. But if I output the button state value to the Serial monitor it goes to HIGH, LOW, HIGH, LOW by it self. When I press the button, its goes to LOW like i should. Why is this? I don't understand, it should be going constantly on HIGH when I don't press it right?

(http://feonx.com/IMG_0130.JPG)

Code: [Select]
/*
  Button

Turns on and off a light emitting diode(LED) connected to digital 
pin 13, when pressing a pushbutton attached to pin 2.


The circuit:
* LED attached from pin 13 to ground
* pushbutton attached to pin 2 from +5V
* 10K resistor attached to pin 2 from ground

* Note: on most Arduinos there is already an LED on the board
attached to pin 13.


created 2005
by DojoDave <http://www.0j0.org>
modified 28 Oct 2010
by Tom Igoe

This example code is in the public domain.

http://www.arduino.cc/en/Tutorial/Button
*/

// constants won't change. They're used here to
// set pin numbers:
const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin =  13;      // the number of the LED pin

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);     
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT); 

  Serial.begin(9600); 
}

void loop(){
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);


  Serial.println(buttonState);
  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == HIGH) {     
    // turn LED on:   
    digitalWrite(ledPin, HIGH); 
  }
  else {
    // turn LED off:
    digitalWrite(ledPin, LOW);
  }
}


Can someone explain me this?

Kind regards,

(sorry for my bad English)
Mike
Title: Re: [ Arduino UNO ] Button state change
Post by: PaulS on Jul 11, 2011, 02:32 pm
Quote
Can someone explain me this?

Congratulations, you've just discovered the joys of floating pins. (That's a search term, in case you missed it.)
Title: Re: [ Arduino UNO ] Button state change
Post by: Feonx on Jul 11, 2011, 03:17 pm
Thanks for the response, but still I don't understand. I searched for "floating pins" but what does it mean? Did I break something? Did I do something wrong?

Can you explain a bit more please...?
Title: Re: [ Arduino UNO ] Button state change
Post by: bubulindo on Jul 11, 2011, 03:24 pm
What about reading this:

http://www.piclist.com/techref/logic/xtrapins.htm

Should tell you what's wrong.

In any case, why didd you do this:
Quote

* pushbutton attached to pin 2 from +5V
* 10K resistor attached to pin 2 from ground



And not:

* 10K resistor attached to pin 2 from +5V
* pushbutton attached to pin 2 from ground

?

Give it a go and see what happens.
Title: Re: [ Arduino UNO ] Button state change
Post by: PaulS on Jul 11, 2011, 03:41 pm
@bubulindo
Comments in the code notwithstanding, do you see a resistor in the picture?
Title: Re: [ Arduino UNO ] Button state change
Post by: Feonx on Jul 11, 2011, 03:54 pm
Thanks for the responses.

I've read the website and I understand that I leave "unused pins" witch will cause power dissipation and put it on HIGH and LOW at the same time. @PaulS I didn't use a resistor at the end, because I was checking if I could fix the problem without it, but I know now its not the problem.

Now I understand the problem, but how can I fix this? I've tried to connect the button directly to the Arduino but it doesn't change anything. Sorry I'm total beginner to electronics I'm trying to learn!


Title: Re: [ Arduino UNO ] Button state change
Post by: PaulS on Jul 11, 2011, 04:24 pm
Quote
I didn't use a resistor at the end, because I was checking if I could fix the problem without it, but I know now its not the problem.

The missing resistor IS the problem.

With the switch connected between +V and the digital pin, when the switch is pressed, the +V will travel through the switch, and make the input pin HIGH. When the switch is released, no voltage will be at the input pin. On the other hand, there is nothing to force the pin LOW, so the pin floats.

If you don't want to use an external resistor, you don't have to. What you do need to do is read about pull-up and pull-down resistors. Then, read about what happens when you use digitalWrite() on an input pin (it turns on the internal pull-up resistor).

If you use a pull-up resistor, internal or external, you need to know that HIGH means the switch is NOT pressed, while LOW means that it IS pressed.
Title: Re: [ Arduino UNO ] Button state change
Post by: Utopia on Jul 11, 2011, 04:50 pm
Hi Mike - you could check out this example of a switch and wire yours up the same:

http://www.arduino.cc/en/Tutorial/Button

It explains the on/off high/low issue a bit, and also explains the 'floating'
Title: Re: [ Arduino UNO ] Button state change
Post by: Feonx on Jul 11, 2011, 06:30 pm
Thanks everybody for the help! I've got it working now.

I only have one question left: Why is there a separated 5v wire? Why not power it by the same pin 2?

Title: Re: [ Arduino UNO ] Button state change
Post by: PaulS on Jul 11, 2011, 07:00 pm
Quote
Why is there a separated 5v wire? Why not power it by the same pin 2?

In order to provide power to the switch, the pin would need to be set as an output pin, and digitalWrite() used to set the pin HIGH.

Then, reading the switch would simply return the last value you set it to.

Title: Re: [ Arduino UNO ] Button state change
Post by: wdl1908 on Jul 12, 2011, 01:31 pm

Quote
Why is there a separated 5v wire? Why not power it by the same pin 2?

In order to provide power to the switch, the pin would need to be set as an output pin, and digitalWrite() used to set the pin HIGH.

Then, reading the switch would simply return the last value you set it to.


No it does not.

There are also convenient 20K pullup resistors built into the Atmega chip that can be accessed from software. These built-in pullup resistors are accessed in the following manner.

pinMode(pin, INPUT);           // set pin to input
digitalWrite(pin, HIGH);       // turn on pullup resistors

from here http://arduino.cc/en/Tutorial/DigitalPins

The only thing that is missing is the code
Code: [Select]
digitalWrite(buttonPin , HIGH); after
Code: [Select]
pinMode(buttonPin, INPUT); 
Title: Re: [ Arduino UNO ] Button state change
Post by: PaulS on Jul 12, 2011, 02:05 pm
Quote
No it does not.

Care to back that up?
Title: Re: [ Arduino UNO ] Button state change
Post by: wdl1908 on Jul 12, 2011, 02:14 pm
Have you tried this?

The pin 2 is always high but can be read from and when the button is pushed it reads LOW

Code: [Select]
// Connect the button to pin 2 and the other side of the button to GND no resistor needed.

const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin =  13;      // the number of the LED pin

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);     
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT); 
  // Active the pull up
  digitalWrite(buttonPin , HIGH);
 
}

void loop(){
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  if (buttonState == LOW) {     
    // turn LED on:   
    digitalWrite(ledPin, HIGH); 
  }
  else {
    // turn LED off:
    digitalWrite(ledPin, LOW);
  }
}
Title: Re: [ Arduino UNO ] Button state change
Post by: PaulS on Jul 12, 2011, 02:33 pm
Quote
The pin 2 is always high but can be read from and when the button is pushed it reads LOW

You mean this pin?
Code: [Select]
const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin =  13;      // the number of the LED pin

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);     
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT); 

The one you defined as an INPUT pin?

My comment was about using that pin to supply power to the switch. In order to do that, the pin needs to be defined as an OUTPUT pin, and set HIGH.

Reading from an OUTPUT pin will return HIGH or LOW, depending on what the last digitalWrite() set the pin to.
Title: Re: [ Arduino UNO ] Button state change
Post by: wdl1908 on Jul 12, 2011, 02:40 pm

My comment was about using that pin to supply power to the switch. In order to do that, the pin needs to be defined as an OUTPUT pin, and set HIGH.

Reading from an OUTPUT pin will return HIGH or LOW, depending on what the last digitalWrite() set the pin to.


You don't need to set the pin as an OUTPUT pin in order to supply power to the switch. You set it as INPUT and do a digitalwrite HIGH to the pin. when you measure the voltage on the pin you will see that it's powered.
Title: Re: [ Arduino UNO ] Button state change
Post by: PaulS on Jul 12, 2011, 03:13 pm
Quote
You set it as INPUT and do a digitalwrite HIGH to the pin.

What that accomplishes is to enable the pullup resistor, not turn the pin on.
Title: Re: [ Arduino UNO ] Button state change
Post by: wdl1908 on Jul 12, 2011, 04:26 pm

Quote
You set it as INPUT and do a digitalwrite HIGH to the pin.

What that accomplishes is to enable the pullup resistor, not turn the pin on.


And the difference is?
What does a pull up resistor do?
Effectively turn the pin to 5v.
Ok you would not get enough current to get a LED burning bright but that not the goal is it?
Title: Re: [ Arduino UNO ] Button state change
Post by: PaulS on Jul 12, 2011, 04:56 pm
The difference between an INPUT pin, where some external source is supplying the voltage, and an internal pin that is set HIGH or LOW is that one can reasonably assume that the switch is pressed if the voltage is present, and not pressed if there is no voltage present (in a pulldown resistor situation) or the reverse (in a pullup resistor situation).

If the pin is set as OUTPUT, the HIGH/LOW state is dependent on what digitalWrite() was used to set it to, not on whether there is something consuming the voltage/current supplied to the pin.
Title: Re: [ Arduino UNO ] Button state change
Post by: wdl1908 on Jul 12, 2011, 06:11 pm

The difference between an INPUT pin, where some external source is supplying the voltage, and an internal pin that is set HIGH or LOW is that one can reasonably assume that the switch is pressed if the voltage is present, and not pressed if there is no voltage present (in a pulldown resistor situation) or the reverse (in a pullup resistor situation).

If the pin is set as OUTPUT, the HIGH/LOW state is dependent on what digitalWrite() was used to set it to, not on whether there is something consuming the voltage/current supplied to the pin.


Very Correct, but that was not the question was it?

This was the question


I only have one question left: Why is there a separated 5v wire? Why not power it by the same pin 2?


So to get power to an input pin you just do a digitalwrite HIGH to the pin and the pin is powered via a pull up no extra resistor needed. Then connect the pin via the button to GND and the digitalread of the pin will be LOW when the button is pressedn and HIGH when it is not pressed.
Title: Re: [ Arduino UNO ] Button state change
Post by: PaulS on Jul 12, 2011, 07:38 pm
Quote
So to get power to an input pin you just do a digitalwrite HIGH to the pin and the pin is powered via a pull up no extra resistor needed.

No. The digitalWrite() function, for an INPUT pin turns on or off the pull-up resistor. It does NOT turn the pin HIGH or LOW. Only OUTPUT pins can be set (forced) HIGH or LOW.
Title: Re: [ Arduino UNO ] Button state change
Post by: wdl1908 on Jul 12, 2011, 10:09 pm

Quote
So to get power to an input pin you just do a digitalwrite HIGH to the pin and the pin is powered via a pull up no extra resistor needed.

No. The digitalWrite() function, for an INPUT pin turns on or off the pull-up resistor. It does NOT turn the pin HIGH or LOW. Only OUTPUT pins can be set (forced) HIGH or LOW.


Please answer me this.

What does a pull up resistor do?
Title: Re: [ Arduino UNO ] Button state change
Post by: AWOL on Jul 12, 2011, 10:23 pm
Quote
What does a pull up resistor do?

For low currents, it pulls up to (or very near) the supply rail.
But it limits the current available.
Title: Re: [ Arduino UNO ] Button state change
Post by: wdl1908 on Jul 13, 2011, 07:51 am

Quote
What does a pull up resistor do?

For low currents, it pulls up to (or very near) the supply rail.
But it limits the current available.


Thank you AWOL you just proved my point.
Title: Re: [ Arduino UNO ] Button state change
Post by: AWOL on Jul 13, 2011, 08:50 am
Quote
Thank you AWOL you just proved my point.

Do you mind me asking what your point was?
(bearing in mind that the pull-ups are in the range 20 to 50 kOhm)
Title: Re: [ Arduino UNO ] Button state change
Post by: wdl1908 on Jul 13, 2011, 09:40 am

Quote
Thank you AWOL you just proved my point.

Do you mind me asking what your point was?
(bearing in mind that the pull-ups are in the range 20 to 50 kOhm)


The point was that PaulS said that to get power on the pin you need to configure it as an OUTPUT. I said that it's not needed you just need to digitalWrite HIGH to an INPUT pin to get it powered. The original question was.


I only have one question left: Why is there a separated 5v wire? Why not power it by the same pin 2?
Title: Re: [ Arduino UNO ] Button state change
Post by: AWOL on Jul 13, 2011, 10:03 am
Yes, but a couple of hundred micro amps at five volts is not what first comes to mind when someone mentions "power".
Title: Re: [ Arduino UNO ] Button state change
Post by: wdl1908 on Jul 13, 2011, 11:59 am

Yes, but a couple of hundred micro amps at five volts is not what first comes to mind when someone mentions "power".


No you are right it is not but it seems a lot of people who ask questions are not very knowledgeable about electronics but in light of the question and the desired effect it is supposed to accomplish it is enough.