Inside a GPIO: the push-pull output stage
An output pin is a P-MOSFET to VDD and an N-MOSFET to GND. They are never on together.
- HIGH: the upper P-MOSFET is on, the pin sources current.
- LOW: the lower N-MOSFET is on, the pin sinks current.
- Input: both are off, the pin is high-impedance (Hi-Z).
Push-pull: sources current when HIGH, sinks when LOW
Output impedance: the pin is not an ideal source
The more current a pin sources, the lower its voltage. The on output MOSFET has a typical RDS(on) of 25–50 Ω.
At high current the pin behaves like a current source. Into a short it limits at ≈ 50–100 mA, but the pin and chip heat up.
VOH / VOL vs output current
Typical limits and the total current budget
| MCU | Per pin (abs. max) | Recommended / spec’d | Total | Note |
|---|---|---|---|---|
| ATmega328P (Uno, 5 V) | 40 mA | 20 mA | 200 mA (VCC/GND) | Port groups have their own 100–150 mA limits |
| STM32F1/F4 (3.3 V) | ±25 mA | ±8 mA (±20 mA relaxed VOL) | ≈ 80–150 mA | Series-dependent; check ΣIIO |
| ESP32 (3.3 V) | ≈ 40 mA | drive strength 5/10/20/40 mA (default 20) | see datasheet | Some pins are boot strapping pins |
| RP2040 (3.3 V) | — | drive strength 2/4/8/12 mA (default 4) | 50 mA (IOVDD) | Very low total: 6 pins × 8 mA is already 48 mA |
Approximate values; they vary by chip series, package and revision. Check your datasheet’s “Absolute Maximum Ratings” and “DC characteristics” tables. Absolute maximum is a damage threshold, not a target.
Even with every pin within its own limit, the total can exceed the chip’s supply pins.
- Total pin currentΣI = 10 · 20 mA200 mA
Why you cannot drive a motor or relay directly
Current
A 5 V relay coil wants ≈ 70 mA; a small motor wants 0.5–2 A at start. A pin gives 20 mA.
Inductive kick
A coil’s turn-off spike is pushed through the clamp diodes into VDD. The MCU resets or is permanently damaged.
Voltage
If the load needs 12 V, the pin cannot provide it. The transistor separates the low-voltage control side from the power side.
Noise and ground
If load current returns through the MCU ground, ground bounces. Give load current its own return path.
3.3 V and 5 V logic levels
A 3.3 V output drives TTL-compatible inputs easily, but not 5 V CMOS inputs. VIH is the lowest voltage an input reads as “1”; 3.5 V for 74HC.
| Input family | VIH (min) | VIL (max) | Driven by 3.3 V output? |
|---|---|---|---|
| TTL / 74LS / 74HCT / 74AHCT (5 V) | 2.0 V | 0.8 V | yes |
| 74HC (5 V CMOS, 0.7·VDD) | 3.5 V | 1.5 V | no |
| ATmega328P @ 5 V (0.6·VCC) | 3.0 V | 1.5 V | marginal |
| LVCMOS 3.3 V | 2.0 V | 0.8 V | yes |
- 5 V → 3.3 V pin is the dangerous direction: unless the pin is 5 V tolerant (FT on STM32), current flows through the clamp diode.
- 3.3 V MCU + 12 V load: the pin sees only the gate/base; 12 V must never reach it.
- The simplest fix is a low-side N-MOSFET with RDS(on) guaranteed at 3.3 V, or a BJT.
- A 12 V P-MOSFET cannot be driven directly; it needs a level shifter (Chapter 07).
Pins float during reset and boot
During reset and until your first lines of code run, pins are inputs (Hi-Z). With a bootloader that lasts hundreds of ms, and seconds during programming.
Meanwhile the gate floats and picks up noise from wires, the drain (via CGD) and your hand. Near threshold the MOSFET sits half-on: the load twitches and the MOSFET heats in its linear region.
Floating gate: with and without a pull-down
- Fraction reaching the gate100 k / (100 k + 100)99.9 %
Open-drain outputs
In open-drain mode only the lower N-MOSFET works. The pin either pulls to GND or lets go; an external pull-up provides HIGH.
- The pull-up can go to another voltage; 5 V if the pin is 5 V tolerant.
- Several outputs can safely share one line (I²C).
- The pin never sources current.
- The cost: the rising edge is as slow as the pull-up × line capacitance RC.
A series resistor on the GPIO: limiting gate charging current
Put 47–220 Ω in series with the gate. An empty gate capacitance (Ciss) looks like a short at first; only the pin resistance limits the current.
- It lowers the peak current.
- It damps ringing on the gate trace.
- It protects the pin if the gate fails.
- The cost: the MOSFET switches more slowly (Chapter 09).
- Peak without RsI = 3.3 / 40≈ 83 mAhits the pin’s current limit
- Peak with RsI = 3.3 / (100 + 40)≈ 24 mA
- Time constantτ = 140 Ω · 1.7 nF≈ 240 ns
Series resistor and gate capacitance: peak current and charging curve
Clamp diodes, ESD and back-feeding
Drive a signal into a chip whose supply is off and it flows through that chip’s clamp diode into its VDD rail. This is back-feeding.
- Every GPIO has two ESD clamp diodes: pin to VDD and GND to pin.
- Above VDD + 0.3 V the upper diode conducts.
- The “off” chip half-runs, may latch up, or loads your pin continuously.
Powering an unpowered peripheral through a GPIO
Worked example: 8 LEDs on an Arduino Uno
- LED currentI = (5 − 2.0 − 0.3) / 330≈ 8 mApin recommendation 20 mA
- TotalΣI = 8 · 8 mA64 mAlimit 200 mA
Worked example: a 5 V relay on an Arduino Uno
- Coil currentI = 5 / 7071 mAabove even the pin’s absolute limit
- 2N7002 dropV = 71 mA · 3 Ω≈ 0.2 V
- 2N7002 lossP = 0.071² · 3≈ 15 mW