I'm a beginner with microcontrollers and circuitry. I'm building a basic circuit (button and LED wired independently from each other) that when you press the button, the LED lights up. Well, I'm trying to learn the hard way, (for a beginner anyway) and wire them seperately and use IDE to connect them. (Get what I'm saying?) I've run into an issue where my button is giving a HIGH signal when it's not pressed. Can anyone explain why this is happening?
Here's the code I'm running:
//Press and hold down button to turn on LED int led = 12; int button = 8; void setup() {
** pinMode(led, OUTPUT);**
** pinMode(button, INPUT);**
** Serial.begin(9600);** } void loop() {
** if(digitalRead(button) == LOW)**
** {**
** digitalWrite(led, HIGH);**
** }**
WazzupDrewfus:
I've run into an issue where my button is giving a HIGH signal when it's not pressed. Can anyone explain why this is happening?
Your button must be pulled up if it's connected from a pin to GND, or pulled down if it's connected from a pin to +5V. The pullup or pulldown can be a resistor (10K is fine), or an interrnal pullup. If you just wire a pin to a button without a pullup or pulldown, your pin will electrically float, and will be at a random level between 0 and 5V.
Best, in my opinion, is to wire the button between the pin and GND, and in your setup() function, use:
pinMode(btnPin, INPUT_PULLUP);
This will save you an external resistor. Don't forget that if you do this, the button pin will be active LOW when the button is pressed.
do you have a camera(phone) ?
you can upload the photo to tinypic.com and use the link to post it here.
or just use a Paint program to "draw" it out and use that same site to upload the JPG file for posting here.
ok, it looks like you've pulled the button high (to 5V) - that means when the button is pressed the digitalRead(btnPin) == LOW
which your code then executes to switch ON the LED.