Skip to content
Tactiverge

School/△ Builder/Week 4

Rearrange the formula, then compute: an LED resistor

An LED needs 20 mA from a 5 V supply. Ohm's law says R = V/I. Which V, and why does the answer come out 1000 times too small?

About 40 min · 3 goals · Checked by automated tests

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.

Derivation: From Ohm's law to the LED resistor formula
  1. Ohm's law for the resistor: V_R = I · R, with V_R in volts, I in amps and R in ohms.

  2. Divide both sides by I to make R the subject: R = V_R / I.

  3. 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.

  4. Substitute: R = (V_supply − V_forward) / I.

  5. 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?

W

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