PyRaspBeta

LED

An LED (light-emitting diode) is a two-lead part that glows when current flows through it the right way. The shop stocks 5 mm through-hole LEDs in red, yellow, green, blue and white for $1 each; they are the cheapest way to show state — a status light, a blink, a PWM fading lamp. The catch is that an LED has almost no resistance of its own, so it always needs a series resistor.

LED

Pins

PinWhat it isType
AAnode (+, longer leg)passive
KCathode (−, flat side)passive
SettingTypeRangeDefault
colorenumred, yellow, green, blue, whitered

Current flows from the anode A (the longer leg) to the cathode K (the flat side). That makes the LED a natural link in a chain: GPIO17 >> Resistor(330) >> LED("red") >> GND reads left to right as current flow.

Wiring it in circuit.py

circuit.py
from wiring import *

pi = Pi()

pi.GPIO17 >> Resistor(330) >> LED("red") >> pi.GND

Blue and white LEDs need about 3 V, so from a 3.3 V GPIO they only glow faintly — on a bench they want 5 V through a bigger resistor, or a transistor. In the simulator LED("blue") still lights, just dimly, because brightness follows the current the model computes.

Driving it in main.py

main.py
from gpiozero import LED
from time import sleep

led = LED(17)

while True:
    led.on()
    sleep(0.5)
    led.off()
    sleep(0.5)

For dimming, use PWMLED and set .value from 0 to 1:

from gpiozero import PWMLED

lamp = PWMLED(17)
lamp.pulse()          # fades up and down

Electrical notes

  • Forward voltage at 20 mA, per the datasheet: red 2.0 V, yellow 2.1 V, green 2.2 V, blue and white 3.0 V. A small slope resistance (10 Ω) models the curve above the knee, so more voltage means more current.
  • The series resistor sets the current: I = (Vsupply − Vf) / R. From 3.3 V a red LED with 330 Ω runs at (3.3 − 2.0) / 330 ≈ 3.9 mA; 220 Ω gives about 6 mA. Nominal current is 20 mA, absolute maximum 30 mA.
  • Above 30 mA the simulator raises an overcurrent warning, which fails the judge's damage check. Switch the datafeed's "on overcurrent" knob to burn out and it also latches burnt: the diode becomes an open circuit and the LED stays dark for good.
  • Reverse breakdown is 5 V, so a backwards LED across a rail can be damaged.
  • PWM dims by average current: brightness = duty × current ÷ 20 mA (clamped at 1.5). At 50 % duty a 4 mA LED reads like 2 mA.
  • The Pi's GPIO can source 16 mA safely; with a resistor the LED's share stays far below that, which is exactly why the resistor is there.

Simulator vs real hardware

  • The model is a piecewise-linear diode: a knee voltage plus a slope resistance. Real LEDs are steeper and vary more, and the model's colours all glow with the same spectral response — there is no perception curve.
  • Each LED draws one fixed forward-voltage offset within a ±0.1 V band (datafeed knob), reproducing the part to part spread rather than random flicker.
  • Overcurrent warns by default; only the burn out feed setting actually damages the part. In the Workspace judge, overcurrent counts as damage either way.
  • Brightness above 1 (overdriven) is allowed up to 1.5, so an overdriven LED can look "too bright" as well as warm about it.

Going deeper

Further reading