0
\$\begingroup\$

I am a beginner at electronics and recently got an arduino and the Arduino Project Handbook and I am having trouble with the push button led project.

I have followed the directions and have the correct resistors but every time I go to press the button the led stay on and if I touch the cable or put my hand near the cable the led comes on.

am I missing something?

Below is the Circuit and Code I am using....

Arduino Project Handbook - Push Button control LED circuit

const int buttonPin = 2;
const int ledPin = 13;
int buttonState = 0;

void setup(){
  pinMode(ledPin, OUTPUT);
  pinMode(buttonPin, INPUT);
}

void loop(){

  buttonState = digitalRead(buttonPin);

  if(buttonState == HIGH){
     digitalWrite(ledPin, HIGH); 
  }
  else{
    digitalWrite(ledPin, LOW);
  }

}
\$\endgroup\$
4
  • \$\begingroup\$ Move the red wire under your button right two holes, and the blue wire over your button left two holes. \$\endgroup\$
    – Majenko
    Nov 4, 2014 at 22:01
  • \$\begingroup\$ I actually tried this already but same issue \$\endgroup\$
    – jonnie
    Nov 4, 2014 at 22:04
  • \$\begingroup\$ You might want to try reading from the same pin you have the blue wire connected to - you have buttonPin set to 3, by the button connected to pin 2. \$\endgroup\$
    – Majenko
    Nov 4, 2014 at 22:05
  • \$\begingroup\$ sorry that's actually a miss type, i tried using different pins just in case \$\endgroup\$
    – jonnie
    Nov 4, 2014 at 22:12

2 Answers 2

4
\$\begingroup\$

It's because your button pin is floating (susceptible to ambient electromagnetic interference). When you wave your hand or touch the power, the button pin might sense random voltage that could be in the "high" range.

Think about how the button is connected internally:

enter image description here

So in your schematic, all that second resistor is doing is draining a little current from power to ground, when it actually should be functioning as the pull-down resistor for the button pin.

Solution: If you move the right leg of that resistor over two holes to the other side of the button, it should work.

\$\endgroup\$
5
  • \$\begingroup\$ Nice this helps me as well! \$\endgroup\$
    – Adam
    Nov 4, 2014 at 22:46
  • \$\begingroup\$ Your solution would work also, although you'd need an internal pull-down instead because the button would pull up when pressed. \$\endgroup\$
    – Shubham
    Nov 4, 2014 at 22:48
  • \$\begingroup\$ Yes I have tried it with no problem. :) \$\endgroup\$
    – Adam
    Nov 4, 2014 at 22:53
  • \$\begingroup\$ Thank you, Adams solution worked too but your answer was very informative and appreciated :) \$\endgroup\$
    – jonnie
    Nov 5, 2014 at 13:14
  • 1
    \$\begingroup\$ I took me like 5 minutes reading your answer that the switch in the main question was rotated, since you show the picture in the way it's supposed to be places in the circuit itself \$\endgroup\$
    – Ferrybig
    Apr 22, 2018 at 15:25
3
\$\begingroup\$

I am a beginner also and I tried the same code a few days ago.

So try connect the button like this:

enter image description here

Also at is mentioned in the site of Energia:

"External pushbuttons can be connected either through PULL-UP or PULL-DOWN resistors to any Digital I/0 pins."

So modify your code like this:

pinMode(buttonPin, INPUT_PULLUP);

Hope it works!

\$\endgroup\$
1
  • \$\begingroup\$ No problem hope it works! \$\endgroup\$
    – Adam
    Nov 4, 2014 at 22:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.