Pico W relay through an S8050 switches an LED
MicroPython on a Pico W: GP15 drives an S8050 that pulls in a 5 V relay with a flyback diode, and the relay contact lights an LED, two seconds on, two off. The whole build, a Raspberry Pi Pico W and 6 more parts, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.
Teaching? Assign it to your class in one click.
Intermediate About 25 minutes to build and run. Runs in your browser. Free, and no account needed.
The parts retail for about $7.85. On Mokxi they're free.
What you'll learn
- How a relay separates a control circuit from a load
- Driving a coil through a transistor
- Flyback diode across a coil
- Switching one circuit with another
- Difficulty: Intermediate
- Time: About 25 minutes
- Board: Raspberry Pi Pico W
- Parts: 10
How it works
A relay is a switch moved by a coil. The coil needs more current than a Pico pin gives, so GP15 drives an S8050 transistor through a 1k resistor, and the transistor switches the coil on 5 V. A diode across the coil absorbs the voltage spike when the coil turns off.
The relay's contacts are a separate circuit. When the coil pulls in, they connect 5 V through a 220 Ω resistor to the LED. The program turns GP15 on for two seconds and off for two seconds, printing "relay on" and "relay off", so the relay clicks the other way every two seconds.
Parts list
10 parts, plus the jumper wires. They retail for about $7.85; on Mokxi they're free.
- 1 × Raspberry Pi Pico W
- 1 × DPDT relay
- 1 × DiodeIn Pinball Table Kit.
- 1 × Transistor (NPN or PNP)In LED Cube Kit.
- 1 × Resistor, 1k ΩIn Starter Kit, Claw Machine Kit, Halloween Prop Kit.
- 1 × Resistor, 220 ΩIn Starter Kit, Claw Machine Kit, Halloween Prop Kit.
- 1 × LED, redIn Starter Kit, Halloween Prop Kit, Logic Lab Kit.
- 3 × Power, 5 V
Kits that can build it
These 2 starter kits have every part in the circuit. Each kit's page lists what else it can build.
Wiring table
Every connection, pin by pin, read from the circuit itself. Each row joins the pin on the left to the pin on the right, by a jumper wire or a breadboard strip.
| Part and pin | Connects to |
|---|---|
| Resistor, 1k Ω pin 2 | Raspberry Pi Pico W pin GP15 |
| DPDT relay pin COIL1 | 5 V |
| Diode pin A | DPDT relay pin COIL2 |
| Transistor (NPN or PNP) pin C | DPDT relay pin COIL2 |
| DPDT relay pin COM1 | 5 V |
| Resistor, 220 Ω pin 1 | DPDT relay pin NO1 |
| DPDT relay pin COM2 | DPDT relay pin NC1 |
| DPDT relay pin NO2 | DPDT relay pin NC1 |
| DPDT relay pin NC2 | DPDT relay pin NC1 |
| Diode pin C | 5 V |
| Resistor, 1k Ω pin 1 | Transistor (NPN or PNP) pin B |
| Transistor (NPN or PNP) pin E | Ground |
| LED, red pin A | Resistor, 220 Ω pin 2 |
| LED, red pin C | Ground |
The code
The MicroPython code exactly as the editor opens it. Change a line there and press Run: the board runs it on the official MicroPython firmware, with nothing to compile.
# Control another circuit: GP15 switches an S8050, the S8050 switches the
# relay's coil, and the relay's contact switches the LED circuit. Every
# two seconds the relay clicks the other way.
from machine import Pin
import time
relay = Pin(15, Pin.OUT)
while True:
relay.value(1)
print("relay on")
time.sleep(2)
relay.value(0)
print("relay off")
time.sleep(2)
Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- The relay never clicks This project
Check the transistor: base through 1k to GP15, collector to the coil, emitter to ground. The coil's other side goes to 5 V. - The diode seems to do nothing, or things get hot This project
The diode's cathode goes to 5 V and its anode to the transistor side of the coil, so it blocks in normal use. - The relay coil does not click, or the Arduino resets when it switches. DPDT relay
A pin cannot drive a coil directly. Use a transistor or MOSFET with a base resistor, and power the coil from its rated voltage (5 V or 12 V). - The transistor dies or the circuit glitches at turn off. DPDT relay
Put a flyback diode across the coil, cathode to the positive side. The coil kicks back a high voltage spike when it is switched off. - The diode is in backwards, so nothing flows, or a flyback diode conducts all the time. Current flows from the anode to the cathode, the end with the stripe. Diode
Put the striped end toward the more positive side for a series diode. For flyback across a coil, the stripe goes to the positive supply side of the coil. - A diode in series drops about 0.7 V (silicon) and 0.3 V (Schottky), so the output is lower than expected. A 1N4148 is also too small for big currents. Diode
Count the drop in your voltage budget. Use a 1N4007 or similar for amps, and a Schottky when you need a small drop. - The load never turns on, or is always on, because the legs are in the wrong order. Transistor (NPN or PNP)
Check the pinout in the datasheet for your part. A 2N2222 or BC547 can have emitter, base, collector in a different order. Flat side orientation is easy to get wrong. - The base is wired straight to the pin with no resistor, or the motor has no flyback diode. Transistor (NPN or PNP)
Put about 1k between the pin and the base. For motors and relays, add a diode across the coil with the stripe toward the positive side.
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.