1

There are two types of tutorials for buttons: no power enter image description here

I don't understand how a button on the first image works. Do we just hope that there is some non zero voltage and hope its enough? I think the first image is really bad, but can't explain why.

7

There is a pullup resistor you don't see in the diagram because it is internal to the Arduino. I have added it in red. To make pin 7 an input, you can write:

digitalWrite(7, INPUT_PULLUP);

, which not only makes the pin an input but also engages the internal ~20K pullup resistor.

So, you're right about the first diagram - it looks like the pin is left floating. Without the code, or an explanatory note, the pullup resistor isn't documented. In such a diagram, once you are familiar with the processor and know that there are optional pullups on the pin-drivers, you can assume that the optional pullup will be used.

enter image description here

Update: I drew in the resistor assuming the red rail was connected to 5v - I should have drawn that in, too.

3
  • Just note that with the pull-up the signal is inverted from when you use a pull-down. That is; digitalRead will return LOW when the button is pressed, and HIGH when not pressed. For a beginner, this can be a bit counter-intuitive. I guess that's why a lot of tutorials use this method. It also shows the actual resistor, instead of using the internal one you can't see. Once you understand how things work, and how you shouldn't leave pins floating, you'll probably end up using the internal pull-up method, since it's easier to wire up. – Gerben Jan 1 at 14:02
  • Second note; some MCUs also have pull-down resistors. In that case you can use both methods, without having to add a resistor. Though with laying out a PCB, it's often easier to connect the other leg of the button to GND, that to Vcc. So I'd still prefer the pull-up method. – Gerben Jan 1 at 14:06
  • For inverted signals such as this, I remind myself in an end-of-line comment that the signal is active-Low, and often I'll make signal condition name, such as PRESSED in this case, and #define PRESSED LOW. As often as I take advantage of the built-in pullup, guess who still trips over his own code when skimming it quickly! – JRobert Jan 1 at 21:30
4

The digital pins of an Arduino UNO, or rather the AVR microcontroller used in the UNO, have optional internal pull-up resistors that can be enabled with the INPUT_PULLUP option to pinMode. When you use INPUT_PULLUP (as opposed to INPUT) there is a functional resistor somewhere between 20kOhm and 50kOhm connected between VCC ("5V") and the digital pin. When you account for this internal resistor enabled, the circuits in your two images are structurally mirror images of one another:

Images from original post combined and annotated for showing the mirrored circuit structures. The boxed numbers here are just to clarify what nodes in the wiring diagram correspond to what nodes in the schematic representation. Don't ask where box number 5 is; it's a mystery. I chopped part of the original second image so a new GND connection was hand-drawn in.

In your first image (left) the code would have to enable the internal pull-up resistor to establish a default HIGH state on the digital pin. With INPUT_PULLUP enabled you can imagine that internal resistor there when comparing with your second image (right). Here I've drawn a resistor symbol over the board to represent the internal one when enabled. When the button is pushed some current is drawn through that internal pull-up and the voltage at the pin falls to ground. Note you could just as easily have an external resistor there and use plain INPUT mode.

Your second example image (right) is using an external pull-down resister to establish a default LOW state. So when you push the button, the signal rises toward 5V/VCC or HIGH, and some current flows through the pull-down resistor. This would be used with INPUT mode rather than INPUT_PULLUP; I have not annotated an internal pull-up on the right to indicate that the pull-up is not enabled in this scenario (plain INPUT mode). Note that if you were to enable the internal pull-up in this second configuration it would form a voltage divider with external pull-down; for the 10K external pictured this could result default voltage at the pin which is only bordering on being a proper LOW.

Some people find it less confusing to have a HIGH state when the button is pushed and also using an external resistor like this helps to explain the concept having a pull resistor altogether, because you can see it. In practice though, it's nice to able to use the internal resistors even if they pull in a more confusing direction. Note there do exist microcontrollers have optional internal pull-down resistors as well, but they're not as common; the UNO in particular does not have these.

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.