I'm newbies with Arduino but good programmer with C and C++, so I try to put button and led on my board to turn on led when I push on button, but as soon as I upload the following sample code, my led on pin 13 turn on right after upload, when I push on button, led glow more, I got this problem with both Arduino board, one Arduino nano and Duemilanove. I looked for solution on Internet :o, in this forum, but I found nothing, If you have solution, you are welcome.
This is code sample:
// 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);
}
void loop(){
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
// 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);
}
}
Your code is fine. The problem is most likely with how you are wiring up the switch to the input pin. Read up on pull-up, pull down resistors and internal pull-up enable, etc.
Basically if you have a switch that is normally open when not being pressed with no pull-up or pull-down provisions, then the input pin is said to be 'floating' and random electrical circuit noise will cause the pin to randomly read as a high or low depending on the phase of the moon and how you hold your face.
The simplest/cheapest solution to wire a simple switch input is to enable the internal pull-up resistor for that input pin and wire the switch between the input pin and ground. When you press the switch the input pin will read as a zero, so you will have to deal with that 'negative logic' in your software statements.
Thank's very much, but how could I set the connexion of my switch to read HIGH when I push my switch, because, in different case, maybe I'll need to set pin to 5v. For exemple, with solar dectector or light detection and so on...
Anyway I take your first response to test push button, but I'm curius to know how set connexion to kill electrical noise.
To be able to read a HIGH at a digital input pin while a normally open push button switch is pressed requires you to wire a external pull down resistor between the input pin and ground, 10k ohms is fine. Wire the switch to the input pin and +5vdc.
When the switch is not pressed the pull down resistor will force the input pin to read 0vdc (LOW) and pressing the switch will force +5vdc to the input pin reading as a HIGH.
The use of pull down and pull up components solves the noise problem with floating input pins. Input pins should never be allowed to 'float' if they are actively being used by the program.
Some go as far as to set all UNUSED I/O pins to input mode and enable their internal pull-up resistors.
Another caution when using simple mechanical switches is contact bounce. Research about that topic and there are Arduino library routines to help deal with contact bounce problems.
Welcome to the reality of real world electronics, where a HIGH and LOW cannot be assumed, but must be designed for.
Lefty