1.Why should I care?
Datasheets give you most quantities; the design gives you the one you need to find. Rearranging the formula, converting units and checking the result is the core loop of engineering calculation, and a program makes it repeatable for every LED on the board.
- ◇ Rearrange a formula so the unknown is on its own before coding it.
- ◇ Convert inputs to consistent SI units (mA to A) inside the program.
- ◇ Write a short calculator program that prints a result with its unit and a power check.
2.Discover
Open the Code Lab and type the LED resistor calculator from this lesson. Run it and check you get 150 Ω and 0.060 W. Then change the supply to 3.3 V and the LED to a 3.0 V blue one at 10 mA: what resistor do you need now?
Can't use the interactive? Read a description instead
The Code Lab exercise can be done by hand: R = (5.0 − 2.0) / 0.020 = 150 Ω and P = 3.0 × 0.020 = 0.060 W; with 3.3 V, a 3.0 V LED and 10 mA, R = 0.3 / 0.010 = 30 Ω (choose the next standard value up, such as 33 Ω) and P = 0.003 W.
3.Checkpoint: guided practice
Check what you just discovered before going on. Hints appear after a wrong answer.
Practice 1 · a fresh version every time
An LED with a forward voltage of 1.8 V should carry 15 mA from a 9 V supply. Using r_ohm = (v_supply - v_forward) / (current_ma / 1000), what resistance is needed, in ohms?
Practice 2
Rearrange V = I · R to find I.
4.Apply it
Turn the calculator into a table: for supplies of 3.3, 5, 9 and 12 V, print the resistor and power for a red LED (2.0 V, 15 mA). Mark any case where the power is above 0.125 W. Then build one case in the circuits lab to confirm the current.
5.Understand
The calculator
v_supply = 5.0 # V
v_forward = 2.0 # V, from the LED datasheet
current_ma = 20 # mA, wanted LED current
current_a = current_ma / 1000
r_ohm = (v_supply - v_forward) / current_a
power_w = (v_supply - v_forward) * current_a
print(f"R = {r_ohm:.0f} ohm, P = {power_w:.3f} W")Output: R = 150 ohm, P = 0.060 W
Convert first, in one place
Datasheets mix units: mA, kΩ, mV. Convert everything to base SI units (A, Ω, V) once, near the top, in a clearly named variable such as current_a. Then every formula below can be written exactly as it appears in a textbook.
Pick a real part and check it
Resistors come in standard values, such as 120, 150 and 180 Ω. If the calculation gives 140 Ω, choose the next value up (150 Ω), so the current is slightly lower, never higher. Then check the power against the resistor's rating. This lesson is calculation only: any real circuit is built in the circuit simulator first.
Explain it another way
The supply pushes with 5 V, the LED takes 2 V of that, and the resistor must 'use up' the other 3 V while letting just the right flow of current through.
Rearranging keeps an equation balanced: whatever you do to one side, do to the other. Coding the rearranged form, with inputs converted to SI units, makes the program a direct, checkable copy of the derivation.
R = (V_s − V_f)/I. Sensitivity: if V_f is 0.2 V higher than expected, the current with R fixed is I = (V_s − V_f)/R, which drops by 0.2 / 150 ≈ 1.3 mA. That is small, which is why one resistor works across LEDs of the same type.
Indicator LEDs on every control panel use this calculation. Too small a resistor overheats or burns out the LED; too large and it is too dim to see in daylight.
Derivation: From Ohm's law to the LED resistor formula
Ohm's law for the resistor: V_R = I · R, with V_R in volts, I in amps and R in ohms.
Divide both sides by I to make R the subject: R = V_R / I.
The LED and the resistor are in series across the supply, so their voltages add: V_supply = V_forward + V_R, giving V_R = V_supply − V_forward.
Substitute: R = (V_supply − V_forward) / I.
Example: (5.0 V − 2.0 V) / 0.020 A = 3.0 / 0.020 = 150 Ω. Power in the resistor: P = V_R · I = 3.0 × 0.020 = 0.060 W, well under a common 0.25 W rating.
Worked example 1
A 12 V supply drives a white LED (3.0 V) at 15 mA. Find R and P.
V_R = 12 − 3.0 = 9.0 V. I = 15 / 1000 = 0.015 A. R = 9.0 / 0.015 = 600 Ω, so choose the next standard value up, such as 620 Ω. P = 9.0 × 0.015 = 0.135 W: under 0.25 W, but closer than you might like, so a 0.5 W part gives more margin.
Common mix-ups
- Use the supply voltage in R = V / I for an LED resistor.
✓ Use the voltage across the resistor, V_supply − V_forward. - Unit conversions can be done in your head while reading the code.
✓ Do them in code, once, in named variables, so every reader and every rerun gets them right.
6.Practice on your own
Practice, not a test. Wrong answers get a hint and another try.
Practice 3
A program does r = (5 - 2) / 20 with the current in mA. What goes wrong?
Practice 4
A resistor drops 4.5 V at 0.030 A. What power does it dissipate, in watts?
7.Predict
A 5 V supply drives an LED that drops 2.0 V, at 20 mA. What resistor is needed?
Guess first. Being wrong here is how the surprise works.
8.Reflect
Concepts: Engineering formulas as code · Voltage, current and Ohm's law