Go Down

Topic: [ Arduino UNO ] Button state change (Read 16407 times) previous topic - next topic

Feonx

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?



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

PaulS

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.)
The art of getting good answers lies in asking good questions.

Feonx

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

bubulindo

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.
This... is a hobby.

PaulS

@bubulindo
Comments in the code notwithstanding, do you see a resistor in the picture?
The art of getting good answers lies in asking good questions.

Feonx

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!



PaulS

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.
The art of getting good answers lies in asking good questions.

Utopia

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'

Feonx

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?


PaulS

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.

The art of getting good answers lies in asking good questions.

wdl1908


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

PaulS

Quote
No it does not.

Care to back that up?
The art of getting good answers lies in asking good questions.

wdl1908

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

PaulS

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.
The art of getting good answers lies in asking good questions.

wdl1908


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.

Go Up