1

Using the Arduino tutorial "Button", I have noticed that if I totally disconnect Pin2 from the circuit, the LED comes on dimly.

I am trying to understand HOW this happens. Is it simply due to being a floating ground?

enter image description here

1

The pinMode(buttonPin, INPUT); line tells the board to set the buttonPin in high impedance mode.

When you disconnect the wire the state of that pin becomes unknown; usually it oscillates. Since it is oscillating, the LED turns on and off randomly: you see it "dimly light".

The usual solution is to use a pull-up or pull-down. I suggest you to wire your buttons towards the ground, declare the pins as pinMode(buttonPin, INPUT_PULLUP); and invert the logic (i.e. buttonState = !digitalRead(buttonPin);). This way it will never oscillate again.

  • Sounds good! Any idea why the tutorials all assume PULLDOWN and ask for resistors rather than coding for PULLUP? – frozenjim Oct 8 '15 at 15:51
  • That's because our mind thinks in a "positive" way. Button is pressed means, for the programmer's mind, a HIGH value. Since the atmega only has pullups, if you want to use it you have to connect the button towards ground, so the button pressed status is signaled by a LOW value. You just have to invert the logic, but... Personally I prefer to put buttons towards ground :P And, by the way, since you are learning I suggest you to experiment with debouncing ;) – frarugi87 Oct 9 '15 at 8:05
  • Yep, de-bouncing was the next question and I've got that one mastered now. I guess we all have the same questions getting started. :-) – frozenjim Oct 10 '15 at 13:18
  • Of course ;) That's why forums are a great way of learning if used properly. There is always someone who has already faced your problem ;) – frarugi87 Oct 12 '15 at 10:58

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.