Most micro-controllers and logic families have output stages which can connect the output to the positive supply or to ground. This allows the micro to put a definite ‘high’ (+5 V in this example) onto the output or a definite ‘low’ (ground) rather than let it float which could cause problems if the output is feeding into another device. Naturally, only one of the switches should be closed at any time. In this article we look at high-side versus low-side switching.
Most micros can now source 10 to 20 mA safely on their GPIO1 pins. This is ample current to light an LED brightly. All that is require is to decide whether to source current for the LED or sink current.
Some more detail
The switches in real devices will be constructed with transistors. These are imperfect and have various limits that we have to be aware of, such as maximum current and source resistance. For example, the ATmega328P used in the Arduino Uno states that the IO pins can handle up to 40 mA subject to a maximum total of 200 mA for the whole chip.
Note on Figure 5 the \(V_{OL}\) voltage – the actual voltage measurable on the output pin (red) – when switched low and sinking 20 mA that the voltage won’t be zero as might be expected but could be as much as 0.9 V above ground. This is due to the voltage drop across the low-side transistor switch in the output.
Similarly, when the output is switched high and sourcing 20 mA the output won’t be +5 V (the test condition power supply voltage) but could be as much as 0.8 V below that.
It’s worth being aware of this as it could be significant in some low-voltage applications where there isn’t much “headroom” between the voltage required by the LED and that of the power supply.
Example 1 – 5 V supply
Figure 6 shows that the LED at 20 mA will have a forward voltage drop of about 2.2 V. If the supply voltage is 5 V then the resistor has to drop \(5 – 2.2 = 2.8\,\mathrm V \). The required value is \( R = \frac {V}{I} = \frac {2.8}{0.02} = 140\,\Omega \). The nearest standard value of 150 Ω will do fine.
The resistance calculation will be the same for the current sinking option of Figure 2.
Example 2 – 3.3 V supply
When running from a 3.3 V supply the headroom becomes substantially limited.
Here we can see that the LED is still requiring 2.2 V leaving 3.3 – 2.2 = 1.1 V for the resistor. If this 3.3 V chip’s output were to droop by, say, 0.5 V then that leaves us with only 0.6 V for the resistor: \( R = \frac {V}{I} = \frac {0.6}{0.02} = 30\,\Omega \).
The problem with this is that a low-value resistor is not a very good constant current source. If the forward voltage, \( V_F \) of the LED decreased by 0.1 V due to temperature or batch to batch variation then the voltage would go to 0.7 V and I would increase from 20 mA to \( I = \frac {V}{R} = \frac {0.7}{30} = 23\,\mathrm mA \). This might push the device a little closer to the absolute maximum ratings.
See all GPIO tricks.