The idea: a pin is a small source with limits
A microcontroller pin set to output is not a wire to 3.3 V. It is a pair of small transistors inside the chip: one connects the pin to VDD for a 1, the other connects it to GND for a 0. Each has some resistance, and each may carry only a few milliamps. So every load on a pin is a small circuit problem: how much current does it want, can the pin give it, and if not, what switch takes over? The page uses one chip throughout, the STM32F103C8 of the Blue Pill board (the same chip as on MCU startup), at VDD = 3.3 V, and four loads: a red LED, a 12 V relay, a push button and a thermistor.
What one pin can do
The numbers come from ST's datasheet for the STM32F103x8/xB (DS5319). Section 5.3.13 says the pins can sink or source ±8 mA with their logic levels guaranteed, and ±20 mA with relaxed levels. Table 7 sets the absolute maximum: 25 mA into or out of any one pin, and 150 mA for all pins together. Going past an absolute maximum can damage the chip, even if the circuit seems to work.
| Limit | Value at VDD = 3.3 V | DS5319 |
|---|---|---|
| Standard current | ±8 mA: VOH ≥ 2.9 V, VOL ≤ 0.4 V | Table 35 |
| Relaxed current | ±20 mA: VOH ≥ 2.0 V, VOL ≤ 1.3 V | Table 35 |
| Absolute maximum | 25 mA per pin, 150 mA for the whole chip | Table 7 |
| Input levels | reads 0 below 1.16 V, 1 above 1.83 V | Table 34 |
| Internal pull-up / pull-down | 30–50 kΩ, typically 40 kΩ | Table 34 |
VOH ≥ VDD − 0.4 V at 8 mA means the pin loses at most 0.4 V when it gives 8 mA. The page turns that into a resistor inside the pin, Rout = 0.4 V / 8 mA = 50 Ω: a 3.3 V source behind 50 Ω. That is a Thévenin model of the pin (see Thévenin and Norton equivalents). It is a worst-case straight line through one datasheet point; a real pin is usually a little stronger.
Sizing an LED resistor
An LED is a diode. Once it conducts, its voltage stays near its forward voltage VF, about 2.0 V for a red LED, whatever the current. So the LED itself cannot limit the current: a resistor in series must do it. Kirchhoff's voltage law around the loop (see Ohm's and Kirchhoff's laws) gives the resistor:
R = (VOH − VF) / IF
The textbook shortcut uses 3.3 V for VOH: (3.3 − 2.0) V / 10 mA = 130 Ω. With the pin's 50 Ω the loop really has 180 Ω, so the current is 1.3 V / 180 Ω = 7.22 mA and the pin sits at 2.94 V (Demo: LED 130 Ω, source then sink). That is under 8 mA, so the pin is still inside its rating. The safe rule uses the guaranteed level: R ≥ (2.9 − 2.0) V / 8 mA = 113 Ω.
The pin can feed the LED two ways. It sources current when the LED goes from the pin to GND: PA5 = 1 lights it. It sinks current when the LED hangs from 3.3 V and goes into the pin: PA5 = 0 lights it (active low), and the pin sits at 7.22 mA × 50 Ω = 0.36 V. On the STM32F103 both ways have the same limits. The Blue Pill's own LED on PC13 is wired the sink way, so it turns on when the firmware writes 0.
A resistor that is too small pushes the pin past its rating (Demo: too small a resistor):
| R | I = 1.3 V / (R + 50 Ω) | Vpin | Verdict |
|---|---|---|---|
| 330 Ω | 3.42 mA | 3.13 V | ✓ fine, dimmer |
| 130 Ω | 7.22 mA | 2.94 V | ✓ under 8 mA |
| 47 Ω | 13.40 mA | 2.63 V | ! over 8 mA: only the relaxed levels hold |
| none | 26.00 mA | 2.00 V | ✗ over the 25 mA absolute maximum |
Switching a relay with a transistor
A relay is a coil that pulls a switch contact closed. The page's relay has a 12 V coil of 150 Ω and 150 mH: it needs 12 V / 150 Ω = 80 mA and pulls in at 9 V or more. On the pin alone it gets nowhere (Demo: relay straight on the pin): 3.3 V / (150 + 50) Ω = 16.5 mA, twice the pin's rating, and the coil sees only 2.48 V.
The fix is a low-side switch: the 12 V supply feeds the coil, a transistor connects the coil's bottom end to GND, and the pin only drives the transistor. With an NPN 2N2222 the pin must give base current. For a sure switch the transistor must saturate, so the base gets about IC / 20 (a forced β of 20, well under the 2N2222's β of 100):
- IC = (12 − 0.2) V / 150 Ω = 78.67 mA, so IB ≈ 4 mA.
- RB = (3.3 − 0.7) V / 4 mA = 650 Ω; take 680 Ω.
- Then IB = 2.6 V / (680 + 50) Ω = 3.56 mA from the pin, under 8 mA, and β · IB = 356 mA is far more than the coil needs: saturated, VCE = 0.2 V, 15.7 mW of heat (Demo: 2N2222 with flyback diode).
Choose RB = 10 kΩ instead and IB = 0.259 mA lets through only β · IB = 25.9 mA. The transistor is in its active region: the coil gets 3.88 V (no pull-in), the transistor keeps 8.12 V and burns 210 mW. Pick "2N2222, R_B = 10 kΩ" in the menu to see it. Saturation and the active region are explained on the transistor as a switch and the BJT amplifier.
A logic-level MOSFET such as the IRLML2502 is even easier (Demo: logic-level MOSFET). Its gate threshold is 0.6–1.2 V and it is fully on, 80 mΩ, at VGS = 2.5 V, so 3.3 V is plenty. The gate is insulated, so once it is charged the pin gives no current; only the 100 kΩ pull-down takes 32.95 µA. The coil gets 12 V / 150.08 Ω = 79.96 mA, and the MOSFET drops only 6.4 mV. The 100 Ω gate resistor limits the short charging spike, and the pull-down keeps the relay off while the MCU is in reset and its pins float. A common MOSFET such as the 2N7000 is not logic level: its threshold can be 3 V, and its resistance is specified at VGS = 10 V.
The flyback diode
A coil resists any change of its current: v = L · di/dt. When the transistor turns off in about 1 µs, the 78.67 mA in the coil wants to keep flowing. If there is no path, the coil raises its own voltage until it finds one: 0.15 H × 78.67 mA / 1 µs ≈ 11.8 kV. Long before that, the collector passes the 2N2222's VCEO = 40 V and the junction breaks down (Demo: no flyback diode). Repeated kicks kill the transistor, and the spike disturbs the MCU.
A flyback diode (here a 1N4007) across the coil, pointing to +12 V, does nothing while the coil is on: it is reverse biased. At turn-off the coil's current flows round through the diode, so the collector rises only to 12 + 0.7 = 12.7 V. The coil's own 150 Ω burns the stored energy, and the current dies out with the time constant τ = L / R = 150 mH / 150 Ω = 1 ms. Diodes as one-way valves are on the diode rectifier.
Pull-up and pull-down resistors
An input pin draws almost nothing (at most 1 µA of leakage). That is why it cannot decide its own level. A button to GND gives PA0 a clear 0 when it is pressed, but when it is open nothing is connected: the pin floats, and stray charge or a nearby finger decide whether it reads 0 or 1 (Demo: floating, pull-up, pull-down). A pull-up resistor to 3.3 V fixes it:
- Released: the leakage drops only 1 µA × 10 kΩ = 10 mV, so PA0 sits at 3.29 V and reads 1.
- Pressed: the button ties PA0 to GND, so it reads 0, and 3.3 V / 10 kΩ = 330 µA flows through the resistor. A larger resistor wastes less but picks up more noise.
- The STM32F103 has its own pull-up of about 40 kΩ (82.5 µA when pressed). The firmware turns it on with CNF0 = 10 in GPIOA_CRL and bit 0 of GPIOA_ODR = 1.
- A pull-down to GND with the button to 3.3 V does the mirror image: pressed reads 1.
A real contact also bounces for a few milliseconds; how firmware cleans that up is on button debouncing.
Reading a thermistor with a divider and the ADC
An NTC thermistor is a resistor whose value falls as it warms: 10 kΩ at 25 °C and, with B = 3950 K, R = 10 kΩ · eB(1/T − 1/298.15 K). The ADC measures voltage, not resistance, so a fixed 10 kΩ on top and the NTC below make a voltage divider: Vout = 3.3 V · RNTC / (10 kΩ + RNTC). The 12-bit ADC on PA1 splits 0–3.3 V into 4096 steps of 0.81 mV (Demo: ADC at 25 °C and 50 °C):
| T | RNTC | Vout | ADC code |
|---|---|---|---|
| 0 °C | 33621 Ω | 2.543 V | 3156 |
| 25 °C | 10000 Ω | 1.650 V | 2048 |
| 50 °C | 3588 Ω | 0.871 V | 1081 |
| 80 °C | 1270 Ω | 0.372 V | 461 |
The firmware runs the formulas backwards: R = 10 kΩ · code / (4096 − code), then T from the B equation (the page uses the middle of the code's step, code + ½). Because the divider and the ADC both use the same 3.3 V, a drifting supply cancels out: the code depends only on the ratio of the resistors. How the ADC finds its code bit by bit is on ADC and PWM; the GPIO registers themselves are on embedded I/O.
See also PID control of a motor's speed, where firmware sets a new PWM duty every 10 ms to hold a measured speed.
What the page leaves out
A real pin is not a straight 50 Ω line: its current rises less than linearly near a short, and VF of an LED grows a little with current. The page also leaves out the weaker pins PC13–PC15 (3 mA, one at a time), the 5 V-tolerant pins and open-drain mode, the total budget of 150 mA, relay must-release voltage and contact ratings, snubbers and Zener clamps that release a relay faster than a plain diode, MOSFET gate charge in detail, thermistor self-heating, ADC sampling time and source impedance, calibration, and the Steinhart–Hart equation that is more exact than the B equation.