← all lessons
Components · #6 of 48

Diodes + LEDs

Current Limiting, Polarity, Brightness

Why it matters

LEDs are diodes that emit light. They have polarity (direction matters) and need current limiting (resistors) to prevent destruction.

The idea

What Is a Diode?

A diode is a one-way valve for electricity:

LEDs (Light Emitting Diodes)

LEDs are diodes that emit light when current flows:

Current Limiting

LEDs must have a current-limiting resistor:

Brightness Myths

Demo

LEDs are simple but critical. Review polarity and current limiting before connecting.

Key takeaways

Going deeper

LEDs have a forward voltage (V_f) that varies by color. Red: ~1.8V, Green: ~2.1V, Blue/White: ~3.0-3.5V. For PWM dimming, use frequencies >100Hz to avoid visible flicker. For battery-powered devices, use lower current (5-10mA) to save power.

Math details

Current-limiting resistor:
  R = (V_supply - V_LED) / I_desired

Example calculations:
  V_supply = 3.3V
  V_LED = 2V (red LED)
  I_desired = 15mA = 0.015A

  R = (3.3V - 2V) / 0.015A = 1.3V / 0.015A = 87Ω

  Use standard value: 100Ω (slightly safer, I = 13mA)

Power in resistor:
  P = I² × R = (0.013A)² × 100Ω = 0.017W (safe, <0.25W)

Power in LED:
  P = V_LED × I = 2V × 0.013A = 0.026W

Implementation

LLM Prompt: LED Resistor Calculator

Write a Rust function that calculates current-limiting resistor for LED.
Input: V_supply, V_LED, I_desired. Output: resistor value (round to nearest
standard value: 220Ω, 330Ω, 470Ω, 1kΩ, etc.). Include validation: warn if
calculated resistor < 100Ω (may be too low) or if I_desired > 20mA.

Lab Exercise

  1. Identify LED polarity (longer leg = anode)
  2. Calculate resistor: 3.3V supply, 2V LED, 15mA → R = 87Ω (use 100Ω)
  3. Build circuit: 3.3V → resistor → LED anode → LED cathode → GND
  4. Measure current with multimeter (should be ~13mA)
  5. Try reversing LED — it won’t light (proves polarity matters!)

full glossary →