4
\$\begingroup\$

When using a pull-up resistor with a switch connected to an Arduino, the input pin reads either high or low when the switch is open or closed respectively.

I understand how when the switch is open, the current must flow from VCC to the input pin through the resistor and I read a high voltage.

When the switch is closed, the current flows from VCC to GND and the input pin reads low.

Why doesn't the current also flow to the input pin (as well as GND) in this case?

\$\endgroup\$
8
  • 3
    \$\begingroup\$ The input pin reads the voltage applied to it - it doesn't draw any current. \$\endgroup\$ Jan 21 at 17:37
  • \$\begingroup\$ @JonathanS., indeed the logic input pin does sink and source current: the input leakage current (as per my answer). It's a significant-enough current e.g. when calculating pull-up/down resistors, particularly to multiple gates. \$\endgroup\$
    – TonyM
    Jan 21 at 20:06
  • 1
    \$\begingroup\$ I completely second @Justme. Details only get in the way of understanding circuit principles. \$\endgroup\$ Jan 22 at 19:14
  • 1
    \$\begingroup\$ @Circuitfantasist, inputs take current and it's in the datasheet. It's not a simplification to state they take no current, it's contrary to the datasheet. Could just as easily state that they take negligible current...or just explain what goes on, which is simple and uncomplicated at this level. \$\endgroup\$
    – TonyM
    Jan 22 at 22:24
  • 1
    \$\begingroup\$ You need to understand what voltage divider is and then this (and many other related questions) will answer themselves. Also don't think about logic circuits as current flowing somewhere, think in terms of voltage at a given point. In an ideal logic circuit the current doesn't flow anywhere \$\endgroup\$
    – floppydisk
    Jan 23 at 20:48

3 Answers 3

2
\$\begingroup\$

First, some background info...

The GPIO pin in input mode will draw or source a small leakage current, typically specified as the input current, II. It's a characteristic to be aware of and to take into account, for example, when using pull-up resistors for current-precious battery-powered equipment.

The typical max. input current is less than ±10 µA for modern logic ICs. This value would mean that:

  • <10 µA flows into the input pin when driven above its logic HIGH threshold (IIH)
  • <10 µA flows out of the input pin when driven below its logic LOW threshold (IIL)

As one example, the TI logic family manual (SCBD152) for the commonly-used 74LVC logic family specifies II as ±5 µA.

For a 3.3 V 74LVC input, when driven to 0 V it's like having a 660 kΩ pull-up, and when driven to 3.3 V it's like a 660 kΩ pull-down.

In such a 3.3 V circuit, a pull-up resistor on a 74LVC input must be below 260 kΩ to reliably deliver the VIL of 2 V min. to the input under all circumstances. For four inputs, a single pull-up must be as low as 65 kΩ and so on. And this ignores supply rail tolerances.

Always remember that digital electronics is merely convenient analogue electronics. Digital electronics provides building blocks that simplify and speed the construction of larger digital logic circuits. But those blocks are still made of transistors and have analogue characteristics to be aware of, especially when interfacing them to something other than compatible logic gates.


Onto your question...

The circuit below shows what you describe, along with the input GPIO pin currents.

I understand how when the switch is open, the current must flow from VCC to the input pin through the resistor and I read a High voltage.

You're correct in that a current will flow into the input pin: the input leakage current, IIH. The current will cause a voltage drop in the pull-resistor. This drop is subtracted from the pull-up supply voltage to get the voltage delivered to the input pin.

However, when the switch is closed, the current flows from VCC to GND and the input pin reads Low. Why doesn't the current also flow to the input pin in this case (as well as GND)?

The pressed switch has a very low resistance. One would expect a fraction of an Ohm for a switch with metal contacts, more for a PCB carbon pad switch. Let's use a high-end value of 10 Ω.

A pull-up resistor is typically many kΩ. So the current flowing through such a pressed switch's contacts is a few mA, including the <10 µA flowing out of the input pin. Let's use a high-end value of 10 mA.

From Ohm's Law (V=IR), the voltage developed across the sub-Ohm switch contact is 0.01 x 10 = 0.1 = 100 mV.

So such an input pin would have 100 mV driven onto it. That's a strong microcontroller logic LOW. It means that all of the current is flowing down through the switch to GND. So there is no current into the pin, its leakage current IIL is flowing out of the input pin.

schematic

simulate this circuit – Schematic created using CircuitLab

\$\endgroup\$
4
  • \$\begingroup\$ Thanks Tony. But why does the current flow out of the input pin to GND, does the input pin have nonzero voltage? \$\endgroup\$
    – CustomCalc
    Jan 22 at 5:54
  • \$\begingroup\$ @CustomCalc, current IC technology is not ideal and will draw and leak out some small current. This causes a bias voltage at the gate input. For details, you can try looking in manufacturer documentation such as reference manuals for logic families. \$\endgroup\$
    – TonyM
    Jan 22 at 17:05
  • 2
    \$\begingroup\$ The OP's question is a conceptual one and needs an equally conceptual answer. Your explanations are very valuable but at this stage they are unnecessary and rather confusing for the OP. \$\endgroup\$ Jan 22 at 19:22
  • 1
    \$\begingroup\$ @Circuitfantasist, I do see where you're coming from, and it's great in the right circumstances, but wholeheartedly disagree here. The OP's question is an electronic one, not a conceptual one, and the real circuit activity really is as simple to explain as the apparently 'simplified' one. Input currents are real, on datasheets and need consideration so recognising them from the start is much better. It's an odd claim that the OP (and future readers) is confused, their comment doesn't say that at all. Like I say, I do get your broader pov, it's similar to mine, so I'm clear there. \$\endgroup\$
    – TonyM
    Jan 22 at 22:42
1
\$\begingroup\$

"Ideal" GPIO

When programmed as an input, GPIO has extremely high input resistance (open circuit).

Open switch

I understand how when the switch is open, the current must flow from VCC to the input pin through the resistor and I read a high voltage.

Because of the very high input resistance, practically there is no current flowing into the GPIO pin. In this situation, the resistor transfers voltage without current - there is no current through it so there is no voltage drop across it and the input voltage is equal to Vcc.

By the way, this phenomenon is not understood by many people because it contradicts human intuition - there is a resistor but it does not resist anything (the resistor is not a resistor).

The situation is the same if there is a built-in pull-up resistor inside the controller - the two resistors would be in parallel.

Closed switch

When the switch is closed, the current flows from VCC to GND and the input pin reads low.

Zero input voltage. Exactly! The current flows through a "piece of wire" so there is no voltage drop across it and the input voltage is zero.

Why doesn't the current also flow to the input pin (as well as GND) in this case?

Zero input current. Like a human being:-) it always chooses the "easiest" path (lowest resistance). So, in this particular case, whatever is inside the input the current would not flow in... especially since it has a high resistance.

But why does the current flow out of the input pin to GND, does the input pin have nonzero voltage?

When current exits the input. The case is more interesting if there is a built-in pull-up resistor inside the controller. Then a current exits the input and is added to the current set by the external pull-up resistor (the two pull-up resistors - external and internal, are in parallel). But the result is the same - there is no voltage drop because the switch resistance is zero and the input voltage is zero.

A similar situation occurred when the input was TTL... but that was a thing of the past...

More points of view

"Fighting" pull resistors. You can imagine the switch as a pull-down variable resistor with two resistance values ​​- zero and infinity. Then two elements - a constant and variable resistor, pull the common point in opposite directions - the constant resistor pulls it up and the variable resistor (switch) pulls it down.

"Voltage-divider". Also, you can consider the circuit of the pull-up resistor and switch in series as a voltage divider with transfer ratio K = R2/(R1 + R2). When the switch is open, K = 1; when it is closed, K = 0.

Real GPIO

This case is discussed in detail by @TonyM in their answer. I will consider the problems only at the conceptual level necessary for understanding.

Input structure

Generally speaking, there is some "pull-up" part of the input circuit connected internally between the input and Vcc, and some "pull-down" part connected between the input and ground. Both parts have extremely high resistance. So, figuratively speaking, something like a high-resistance "voltage divider" is formed and we can use it as an "electrical analogy" to illustrate the problem.

schematic

simulate this circuit – Schematic created using CircuitLab

Operation

... does the input pin have nonzero voltage?

There is such a voltage... and that is confusing - it is an input but produces a voltage? For example, the midpoint of the voltage divider R1-R2 is its output, but here it represents the input of the Arduino.

We know that the midpoint voltage Vmid is between zero (ground) and Vcc. We also know that current flows from higher to lower voltage.

High input voltage. So when we apply a voltage Vcc > Vmid through a resistor to this point (just connecting the resistor to Vcc), a very small current begins to flow into it.

schematic

simulate this circuit

Low input voltage. When we apply a zero voltage through a resistor to this point (just connecting the resistor to ground), a very small current starts coming out of it.

schematic

simulate this circuit

\$\endgroup\$
2
  • 2
    \$\begingroup\$ Simple and works at the level of the presented question. Except that current takes all paths of resistance, not just the path with least resistance. I do understand there is ideally one path and the resistance is zero. \$\endgroup\$
    – Justme
    Jan 22 at 20:04
  • \$\begingroup\$ @Justme, That is right... when principles are explained, things are qualitative and like in digital circuitry - either it is there or it is not... just two values. After that, shades are already introduced... I have been explaining this all my life... If you are interested in the philosophy of understanding, explaining and inventing circuits, you can visit my wikibook Circuit Idea and my blog Circuit Stories. \$\endgroup\$ Jan 22 at 20:17
1
\$\begingroup\$

A practical answer to your question is that the input impedance of the MCU pin is so high it can be thought to be infinite, so it can be thought that no current flows in or out of the MCU input pin, and it can be thought that the MCU pin simply senses the pin voltage.

When the switch is not pushed, switch is an open circuit with infinite impedance, so no current flows through the switch, and there is only resistor from VCC to MCU input. As no current flows, there is no voltage drop over resistor, so it sets the MCU pin to VCC.

When the switch is pushed, it is a closed circuit with zero resistance and current flows from VCC through resistor and switch to 0V (GND). As the pusbutton has zero resistance, it will connect the MCU pin directly to 0V potential.

I do acknowledge that the other answers take into account a practical CMOS input which can have a few microamps of leakage current flowing so they are technically correct, and leakage current is an important concept to learn and understand why you can't have low current pull-up in the order beyond megaohms. It can usually be ignored as the impedances are such that a bit of current has very little effect when using strong enough impedances foe the pull-up and switch.

Additionally, a number of other concepts of the MCU pins such as input capacitance could be taken into consideration too, as it will lower the AC impedance, but neither input capacitance or leakage current would change how a simple pushbutton and reaistor basically work to give high and low states to MCU pin.

\$\endgroup\$

Your Answer

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

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