1
\$\begingroup\$

I've been using Arduinos for years but one thing I have just taken for granted without understanding is why, for example, in a simple button circuit using the internal pullup, you connect the button to ground rather than 5V.

But now I'm trying to teach my kids and they have me stumped and I'd really like a good answer that even a kid can understand (and hence me as well). I've spent the last 2 hours reading articles on the internet but no one has explained it like I'm a 5th grader.

A simple LED control circuit with a pin set to OUTPUT treats the pin like a voltage source - and the circuit connects to GND.

It would seem logical (to a 5th grader) that when the pin is set to INPUT, it's "receiving" the voltage, hence acting like GND, but as we all know this is definitely not the case.

So what exactly is happening at the pin when set to INPUT or INPUT_PULLUP that allows it to "read" the voltage level while still acting like the voltage source?

CLARIFICATION: the question is not about why a pullup (or pulldown) resistor is needed, but more about how the pin can "read" voltage (when set to INPUT) even as it's "sourcing" 5V, as in the button shorting to ground example.

\$\endgroup\$
2
  • \$\begingroup\$ "A simple LED control circuit with a pin set to OUTPUT treats the pin like a voltage source - and the circuit connects to Gnd." - Well, you can connect an LED + resistor between an output pin and 5V. Drive the output low, and it'll power the LED. The output pin is now "acting as gnd". \$\endgroup\$
    – marcelm
    Commented Aug 22, 2022 at 16:03
  • \$\begingroup\$ I"m leaving this link here (gammon.com.au/forum/?id=11955) for my future reference as for some reason I found it helpful, though not directly answering my question about "what is happening at the pin to allow it to read voltage levels" \$\endgroup\$
    – Tom Auger
    Commented Aug 23, 2022 at 13:37

6 Answers 6

2
\$\begingroup\$

The diagram shows a standard microcontroller GPIO interface (simplified). There are variations but they are all very similar. Older microcontrollers had no pullup or pull down resistors. They had to be added externally. First the pull up resistors appeared. I remember being surprised that all were enabled at reset making all the inputs VDD. Currently many default to High-Z (all enable switches open).

The diagram displays a configuration to interface to a LED and to a push button.

IO2 is configured as an output by closing the output enable switch. You can see the path from the output data register to the IO2 pin. Notice that IO2 is still connected to the input data register. This is so that software can read the state of the pin whether it is an input or an output. Applying a 1 to the IO2 data bit in the output data register applies VDD to the LED-resistor pair. The voltage drop across the LED is zero so it if OFF. Applying a 0, grounds the pair turning the diode ON. In this case the voltage is sourced through the data register or an associated amplifier.

IO4 has its output function disabled. The output enable switch is open. Notice that I did not say it was configured as an input because all the pins are always connected as inputs. This function does not need to be configured. The output function is disabled by configuration.

The switch in its normal state is open (essentially non-existent). So, to generate a high state the associated pullup resistor is enabled. In this case VDD is applied through the pullup resistor to IO4. Since there is no path for the current to flow, then the IO4 pin will be 5v. The input data register will be 1 for that bit.

To answer your last question IO4 is an input, but it has a voltage source with a high internal resistance in parallel with the input. The pullup resistor allows the pushbutton to take over.

Closing the switch applies a voltage of 0V to IO4. This creates a path for current to flow through the pullup allowing the voltage to drop across it. Since IO4 is now 0V the associated bit in the input data register will be 0.

I leave it to you to redraw the diagram for the LED connected to ground, and the pushbutton connected tp VDD. Both layouts are valid. Interpretation by software will change. Does Light On map to 0 or 1? Does button pressed map to 0 or 1?

schematic

simulate this circuit – Schematic created using CircuitLab

\$\endgroup\$
2
\$\begingroup\$

A proper INPUT pin has high impedance, and does not pull the voltage anywhere, so it doesn't affect the measurement too much.

For some applications like buttons you have a resistor that pulls the signal high, and the button shorts the pin to GND (or vice versa), so you have a guarantee that you can read two different voltage levels depending on whether the button is pressed or not.

With the internal pull-up, you don't need an extra component for this, but you need to configure whether you want this or not.

\$\endgroup\$
1
  • \$\begingroup\$ This was a helpful answer as it touches on "impedance" as a concept I need to learn more about - if I understand correctly, the high impedance on the pin helps to prevent "Schrödinger's Cat" where the act of observing the voltage on the pin affects the rest of the system in a significant way. \$\endgroup\$
    – Tom Auger
    Commented Sep 1, 2022 at 17:17
2
\$\begingroup\$

Active low inputs have some advantages:

  1. The information source (switch, output from other circuit) doesn't need to know the supply voltage or provide a voltage to transfer the information. Systems with different supply voltages can interact.
  2. The information source device is able to detect the presence of a receiver because this provides a voltage.
  3. TTL chips have a pull-up resistor built in anyway, so no extra component is needed in many cases.
  4. You don't have to provide a short circuit protected supply voltage to external switch boards or connectors while GND is everywhere for free.
\$\endgroup\$
5
  • \$\begingroup\$ TTL chips technically don't have an internal pull-up resistor, and are not intended to be used without external pull-up. Sure, the inputs tend to float high due to internal structures, and may work even without external resistor. But as TTL input will sink and source current anyway, so it does not compare to a CMOS input anyway. \$\endgroup\$
    – Justme
    Commented Aug 22, 2022 at 17:02
  • \$\begingroup\$ @Justme Thanks for the hint. I was curious about the details and took an original SN7408 from 1974. I measure between 0.77 and 0.84 uA input current providing + 5V and 1.01 mA into GND. The open input voltage is 1.58 V, not really a clear "high" I admit, but this behaves in many aspects like an input with pull-up resistor. It needs about 500 uA to GND to see a low, so I assume this is less susceptible to noise than a modern MCU input with internal pull-up. \$\endgroup\$
    – Jens
    Commented Aug 22, 2022 at 18:22
  • \$\begingroup\$ Inputs with pull-ups also can be driven by open-collector (NPN) or open-drain (NMOS) devices, which can be wire-ORed together. \$\endgroup\$
    – PStechPaul
    Commented Aug 22, 2022 at 19:49
  • \$\begingroup\$ When is the last time you worked with a chip that was actual TTL? I hope you're not suggesting that's the technology behind Arduino. \$\endgroup\$ Commented Aug 23, 2022 at 3:20
  • \$\begingroup\$ @Jens If the circuitry pulls the input to 1.58V, that is at about the threshold of the transition, as 2.0V is the minimum legal voltage for TTL logic high. CMOS inputs draw no current so the programmable roughly 40k pull-up can pull the voltage to supply voltage, as generally CMOS inputs require about 70% of the supply voltage to be mininum legal logic high voltage. \$\endgroup\$
    – Justme
    Commented Aug 23, 2022 at 5:03
2
\$\begingroup\$

As an input, the AVR pin can either be in a high impedance mode or pull-up mode.

In high impedance which has no pull-up or pull-down, so the pin voltage it must be externally set to either high or low. The pin cannot sink or source any current in this mode (as we can ignore leakage current here).

In pull-up mode, an internal resistor (it may not actually be a resistance but a current source but forget the details for now) is connected between input pin and supply voltage.

The internal pull-up will source out current if pin externally set low. As there is no pull-down, the pin will not sink any current.

Thus the internal resistor keeps the pin weakly at supply voltage, so the easiest way to connect a button is to make the pin pulled high when button not pushed, and make the button pull the pin low when pushed.

And that's because AVRs have only internal pull-ups. Some other MCUs have selectable pull-ups and pull-downs.

\$\endgroup\$
2
\$\begingroup\$

Simplified internal connection You can think of it as a setting to add a resistor connected between the supply voltage and the pin.

The internal electronics reading the pin stay the same and draw only a little current, in order to not affect the circuit.

If you wouldn't have the resistor in this circuit, there's no guarantee if the pin is high or low.

\$\endgroup\$
1
\$\begingroup\$

Having a pull-down rather than a pull-up is rather similar in a CMOS world. For whatever reason (for example, TTL circuits required an uncomfortably low value pull-down to guarantee a low (~250 ohms), which means a lot of current drawn when the switch pulls it high (20mA)) we tend to have pull-ups. More modern parts (eg. many ARM chips) often have both pull-ups and pull-downs available on-chip, just enable the one (if any) you want. One valid reason to use an on-chip pull-down rather than a pull-up is that on-chip pull-ups are typically not compatible with 5V tolerant pins on a chip operating from a lower voltage.

Another reason is that it's generally better to have something like a switch tied to ground on one side and maybe add some resistance to the input. That way ESD from some hapless user's fat electrically-charged finger doesn't find its way to the power supply or directly to a CMOS input.

Driving LEDs is also typically done with a low-side driver (the LED is connected to Vdd through a resistor). An n-channel MOSFET will have lower voltage drop at the same current etc. than a p-channel MOSFET of the same size (due to carrier mobility differences)- so often CMOS outputs will have asymmetrical output drive capability. The LED doesn't care either way, of course, but lower voltage drop means more consistent brightness and less heating on the MCU chip. Some MCUs like the ones used in base Arduinos have larger p-channel MOSFETs so they have more symmetrical drive capability. As silicon acreage decreases in cost that difference perhaps becomes less important.

TL;DR: It's generally better, or at least no worse, to have the switch grounded and a pullup, and to drive an LED with a low-side driver. So we generally do it that way unless there's a good reason not to.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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