1
\$\begingroup\$

I have a question regarding the GPIO (from a Arduino board for example). How does it work exactly when you use it as an input pin? What are the mechanisms at work when you use it in a pull-up and pull-down resistor circuits with an push-buttons? I have read a few tutorials but they don’t give a deep explanation of the mechanisms, or I completely missed the points they were making. Most tutorials say things like (in a pull-up example), “When the button is pressed, it connects the input pin directly to ground. The current flows through the resistor to ground, thus the input pin reads a low state” (https://learn.sparkfun.com/tutorials/pull-up-resistors ). But in a pull-down resistor circuits with push-buttons, the input pin is also connected to ground when the button is pushed and the input is high in that case.

Also a baseline explanation would not work seem to work, because in case of a the pull-up example how does it know what the high/low value is when you have pushed in the button before connecting the power supply. Even a threshold explanation doesn't seem to explain it entirely, the voltage on the circuit as a whole seems to me to be above 2.5 V in both pushed and non-pushed state, when a 5V Vcc and 10kOhm resistor is used or is there an error in my thinking about this?

Explanations of electrodes that only travel through the pathway of least resistance also does seem to be right. Even if this was true this doesn’t explain the behaviour in the pull-down circuit, because there would never be any input when you would connect a 10 kOhm resistor to the ground (before the push button). Since this is lower than the impedance of the input pin (100k-1MΩ). Components divide the current according to their reciprocal resistances (I1 / I2 = R2 / R1). You can even see it when you connect a LED to the ground site of the circuit, which is on when you push the button and the input reads HIGH. Does in the pull-up case I1 / I2 = R2 / R1 explain the behavior, because there is no resistor in parallel to the resistor of the GPIO and because the connection where no resistor is present the resistance is neglectable (at least over a short distance), all current flow goes to the ground directly?

It seem to me it got something to do with the fact that the connection that has no resistor between the input pin (and the ground or power supply pin) determines the state when both have a closed circuit with it. But what is the mechanism. Does the GPIO unit (in input state) contain a sort of capacitor that “transmits” the charge to the power supply when it is directly connected to it (larger pull with larger voltage), and contains the charge longer (or replaced charge quicker) when it is directly connected to ground, and so influences the internal circuit? Or does it have something to do with the potential difference between the sides of a resistor, and if so how does the input pin read this?

I understand the fact that in you use a ground connection to the input pin (in a pull-down circuit) because otherwise the input pin would be influenced by noise (static) and a resistor because otherwise there would be a direct connection between the Vcc and ground, and this would result in a short circuit. So this part can be skipped in the answer. Of course you can add this in your answer for other users looking for an answer regarding this.

I hope that somebody can give me an (in-depth) explanation of how these circuits work.

\$\endgroup\$
  • 1
    \$\begingroup\$ It is not clear to me what exactly you are asking. Pull-up, pull-down, sequencing between button and power? \$\endgroup\$ – Wouter van Ooijen Nov 1 '14 at 12:05
  • \$\begingroup\$ I wasn't asking for the sequencing that was known to me. \$\endgroup\$ – A_Person Nov 2 '14 at 8:06
  • \$\begingroup\$ "Majenko - not Google" and "hobbs" have answered my question perfectly. Thank you for trying to help. \$\endgroup\$ – A_Person Nov 2 '14 at 8:12
  • \$\begingroup\$ Also John R. Strohm aslo helped me out with my question (not to forget anyone). \$\endgroup\$ – A_Person Nov 2 '14 at 10:41
5
\$\begingroup\$

A GPIO pin, when in INPUT mode, can be thought of as a very very large resistor connected to ground. The GPIO pin is interested in the voltage that is across this resistor. Take the following circuit for example:

schematic

simulate this circuit – Schematic created using CircuitLab

A logic HIGH is seen by the Arduino when the voltage at the node labelled GPIO is at, or near, \$V_{CC}\$ (in this case 5V). A LOW is seen when the voltage at GPIO is at or near \$0V\$.

With the switch SW1 open, there are just the two resistors in play - the pull-up, and the internal GPIO port's resistor. So, using simple maths, we can calculate the voltage that would be at GPIO.

First we calculate the ratio of the two resistors, using \$\frac{R2}{R1 + R2}\$, and then multiply it by the voltage, which is \$5V\$. So we have the sum:

$$ \frac{10,000,000}{10,000 + 10,000,000}×5 $$

We can of course simplify that by doing the addition, then cancelling out trailing zeros above and below the line:

$$ \frac{10,000,000}{10,010,000}×5 $$ $$ \frac{1,000}{1,001}×5 $$

And so the answer comes out as \$4.995V\$ - pretty much the full \$5V\$. So the Arduino see that as being HIGH, since it is above its "input logic high threshold", also known as \$V_{IH}\$ in datasheets.

So now what happens when we press the button? Well, basically we create a short circuit across the internal GPIO resistor. So now we can completely ignore that resistor, since we have essentially put a wire across it to short circuit it.

So now our sum gets changed slightly, since \$R2\$ is now \$0\Omega\$ (the resistance of the wire shorting out \$R2\$).

$$ \frac{0}{0 + 10,000}×5 = 0V $$

And of course, \$0V\$ is below the "input logic low threshold", or \$V_{IL}\$.

Another way of looking at it is that the GPIO, when the button is pressed, is directly connected to ground. No amount of tweaking of the resistor \$R1\$ will ever change the fact that the voltage at ground is \$0V\$. The only way you can change that is by short circuiting \$R1\$ so that becomes \$0\Omega\$ as well, and then you have basically short circuited your battery, and all your wires have now melted.

For reference, here is part of Table 28.2 from the ATMega328P data sheet detailing the input voltage thresholds:

enter image description here

We can see there the \$V_{IL}\$ and \$V_{IH}\$ voltages for the \$2.4V - 5.5V\$ \$V_{CC}\$ range listed as \$0.3V_{CC}\$ and \$0.6V_{CC}\$ respectively. Now, this doesn't refer to \$0.3V\$ and \$0.6V\$, but to \$0.3×V_{CC}\$ and \$0.6×V_{CC}\$.

If \$V_{CC}\$ is \$5V\$, then \$V_{IL}\$ is \$0.3 × 5 = 1.5V\$, and \$V_{IH}\$ is \$0.6 × 5 = 3V\$.

So any voltage seen on the GPIO pin that is below \$1.5V\$ is registered as a logic LOW, and any voltage see that is above \$3V\$ is registered as a logic HIGH.

\$\endgroup\$
  • \$\begingroup\$ Thank you for your detailed answer. It really helped my out. It was really strange that I looked over the fact that there basically was a short circuit taking place (in the GPIO part of the circuit) in the close pull-up case. It was never mentioned, in the tutorials I saw. Maybe they thought that everyone reading/watching the tutorials, would notice it automatically. That it would be obvious, I mean. \$\endgroup\$ – A_Person Nov 2 '14 at 8:26
1
\$\begingroup\$

Also a baseline explanation would not work seem to work, because in case of a the pull-up example how does it know what the high/low value is when you have pushed in the button before connecting the power supply. Even a threshold explanation doesn't seem to explain it entirely, the voltage on the circuit as a whole seems to me to be above 2.5 V in both pushed and non-pushed state, when a 5V Vcc and 10kOhm resistor is used or is there an error in my thinking about this?

There's a massive error in your thinking about this. When the button isn't pushed, the pin is connected to 5V through a 10k resistor, and to ground via the very large resistance of the chip's input circuitry. Since voltage drop is proportional to resistance, the chip is dropping ~5V, the 10k resistor is dropping ~0, and the voltage at the pin is very close to 5V.

When the button is pushed, the pin is connected to 5V through a 10k resistor, and to ground via the switch (which is ~0 ohms) in parallel with the chip (which doesn't make any difference). Now the 10k resistor is dropping ~5V, the switch is dropping ~0, and the voltage at the pin is very close to 0.

Pull-downs are the same, only in reverse. The pin is connected to ground through some resistance, and normally reads low, but if it's connected to 5V through a much lower-resistance path the voltage on the pin will be close to 5V and it will read high.

The thresholds are fixed. You can find them out in the datasheet for your chip, but a rough guide for CMOS is that an input over 70% of Vcc is high, an input under 30% of Vcc is low, and anything in between is unreliable. Some chips might have a smaller "unreliable" zone, but if you can design to 30% and 70% you shouldn't have a problem.

\$\endgroup\$
  • \$\begingroup\$ Thank you for answering my question. If I would write down calculations of what you wrote would it be like the following in the pull-up example? R1 = 10 k Ohm; R2 = 1 M Ohm; R3 = around 0 Ohm -- Calculating R total: R1 + (R2 * R3 / R2 + R3) = R total; 10 k + ~ 0 = ~10 k. Proportion of voltage across R1: (R1 / R1 + R2,3) * V ; (10 k / 10k + ~0) * 5; ~1 * 5 = ~5; Or don't these calculations apply here? \$\endgroup\$ – A_Person Nov 2 '14 at 8:54
  • \$\begingroup\$ To clarify R1 would be the pull up and R2 the internal resistor of the GPIO and R3 the path from button to ground. So there will be, - to add to what I said in previous comment -, Vcc - Voltage drop R1 = Voltage at the GPIO pin; 5 - ~5 = ~0 voltage at the GPIO pin. \$\endgroup\$ – A_Person Nov 2 '14 at 9:04
0
\$\begingroup\$

First, a GPIO pin works by sensing the voltage on the pin. They normally just sense logic "1" (usually high, close to the supply rail, these days) or logic "0" (usually low, close to ground, these days). Exact details of logic 1 and logic 0 depend on the actual microcontroller hardware and are specified in the microcontroller's data heet.

GPIO pins present a fairly high impedance to the outside world. Check the datasheet for the "exact" value. Tied to the positive supply rail through a current-limiting resistor, with a switch connected and open, what you have is essentially a very large resistance to ground. Very little current flows, meaning there is very little voltage drop across the current-limiting resistor, and the GPIO pin sees a high voltage (close to the positive rail).

Now, when the switch is closed, the switch short-circuits the cold end of the current-limiting resistor to ground. Current flows, and a voltage drop develops across the resistor. This puts the GPIO pin at ground potential.

Remember, Ohm's Law is your FRIEND.

\$\endgroup\$

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.