</>EmbeddedInterview

How do you calculate the current-limiting resistor for an LED?

EasyElectronics & Hardware Basics

Short answer: R = (V_supply − V_f) / I_f, where V_f is the LED's forward voltage and I_f the desired current. For a red LED (V_f ≈ 2.0 V) from 3.3 V at 5 mA: R = (3.3 − 2.0) / 0.005 = 260 Ω → use 270 Ω. Then check the GPIO's maximum output current and the total current for the port and package.

Typical forward voltages: red/yellow ≈ 1.8–2.2 V, green ≈ 2.0–3.2 V, blue/white ≈ 2.8–3.4 V. A white LED may barely light from 3.3 V without a resistor margin; drive it through a transistor from a higher supply or use a constant-current driver.

GPIO limits: many MCUs allow ~20 mA absolute per pin but specify output voltage levels only at much lower currents, and have a total limit per port or package (e.g. 100–150 mA); modern LEDs are bright enough at 1–5 mA. Above a few mA or for several LEDs, use a transistor or MOSFET switch.

Sink vs source: connecting the LED from VDD to the GPIO (active-low, the pin sinks current) is common because some pins sink current better than they source it; remember the inverted logic in firmware.

Brightness control: PWM at a frequency above flicker perception (> 200 Hz, often 1–20 kHz); perceived brightness is non-linear, so use a gamma-corrected table.

Resistor power: P = I² × R = 0.005² × 270 ≈ 7 mW, fine for any small resistor.