[ Arduino UNO ] Button state change

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?

/*
  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

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.)

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...?

What about reading this:

Should tell you what's wrong.

In any case, why didd you do this:

  • 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.

@bubulindo
Comments in the code notwithstanding, do you see a resistor in the picture?

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!

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.

Hi Mike - you could check out this example of a switch and wire yours up the same:

It explains the on/off high/low issue a bit, and also explains the 'floating'

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?

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.

PaulS:

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
digitalWrite(buttonPin , HIGH); after

pinMode(buttonPin, INPUT);

No it does not.

Care to back that up?

Have you tried this?

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

// 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); 
  }
}

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

You mean this pin?

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.

PaulS:
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.

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.

PaulS:

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?

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.

PaulS:
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

Feonx:
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.

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.