Lights and buttons · Lesson 2 of 5· 15 min
Wiring an LED
Why this matters
The built-in light is useful, but robots light up things you choose: a warning lamp, a status light. To do that you wire parts to the board's pins.
The parts
- An LED has two legs. The longer one is the anode (A, the plus side). The shorter is the cathode (C, the minus side).
- A resistor limits the current. Without one, an LED can burn out on a real board. We use 220 ohms.
- A GPIO pin (General Purpose Input Output) is a pin you control from code. On the Pico they are named GP0, GP1 and so on.
Open a tool
Wokwi simulator
Free, nothing to install. Works best on a laptop; on a phone you can still read the steps and the code below.
Opens wokwi.com in a new tab. Come back here when you are done.
Step by step
-
In Wokwi, press the blue plus button and add an LED and a resistor.
-
Click the resistor and set it to 220 ohms.
-
Wire GP15 to one end of the resistor, the other end of the resistor to the LED's anode (A), and the LED's cathode (C) to a GND pin.
from machine import Pin
import time
lamp = Pin(15, Pin.OUT)
while True:
lamp.value(1) # on
time.sleep(1)
lamp.value(0) # off
time.sleep(1)
How the circuit works
Current flows out of GP15, through the resistor, through the LED, and back to GND. If the LED is in backwards, nothing lights: an LED only lets current flow one way. Try turning it round in Wokwi to see.
Checklist
0 of 3