2

I am testing this basic button example from the Arduino tutorial page.

This circuit lets power flow to pin 2 when the button is pressed. When the button is not pressed, the power goes through the resistor to ground, but my question is: why would you even need the connection to ground?

Can't you have power flow from 5V to pin2 or just have NO connection at all when the button is not pressed?

Button

  • Don't think in terms of where the 'power flows' (current), think in terms of voltages. It will make more complicated circuits easier to understand. – geometrikal Aug 31 '14 at 12:09
3
When the button is not pressed, the power goes through the resistor to ground

When the button in not pressed its not the 5v pin that is connected to ground but the input pin is connected to ground. This is to ensure that the input does not float between 0v and 5v.

When not connected to 5v or GND the input pin tends to randomly change from `HIGH` to `LOW`. To avoid this and quell external interference you just ground the pin.

  • Ah, I didn't see that the connection is between pin 2 and ground. Those little buttons always confuse me. – Kokodoko Jul 15 '16 at 16:25
11

That resistor is called a pull-down resistor. It's there to make sure pin 2 read low when the button isn't pressed. If you don't put it there pin 2 is left 'floating', and it could erroneously read a high value. Google it for more info.

If you want to simplify you circuit you could instead use a pull-up resistor, and connect the button to ground instead of 5v. The ATMega chip on the arduino has these pull-up resistors build into the chip itself, so you don't need to add one to the breadboard. Just not that the input values will be reversed. So pin2 will read LOW when the button is pressed, and HIGH when not. To activate the internal pull-up use pinMode(2, INPUT_PULLUP)

1

In simple words, to avoid undefined state. This type of resistor arrangement is known as Pull Down Resistor. (Pull-Up also exists)

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.