Pico W button switches an LED through an S8050
MicroPython on a Pico W: a button on GP14 drives an S8050 NPN transistor from GP15 through 1k, and the transistor lights an LED from 3.3 V. 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.
Beginner About 20 minutes to build and run. Runs in your browser. Free, and no account needed.
The parts retail for about $6.45. On Mokxi they're free.
What you'll learn
- A transistor as an electronic switch
- Base resistor to limit the control current
- Pull-down resistor on a button
- Switching a load that the pin does not drive directly
- Difficulty: Beginner
- Time: About 20 minutes
- Board: Raspberry Pi Pico W
- Parts: 9
How it works
A Pico pin can only supply a little current, so a transistor acts as a switch for bigger loads. Here an S8050 NPN transistor switches the LED. When its base gets current, the path from collector to emitter opens up and the LED lights.
The button is on GP14, with a 10k resistor down to ground, so it reads 0 until pressed. The program copies the button's value to GP15 every 10 ms. GP15 feeds the transistor's base through a 1k resistor. The LED hangs from 3.3 V through a 220 Ω resistor, so it lights while the button is held.
Parts list
9 parts, plus the jumper wires. They retail for about $6.45; on Mokxi they're free.
- 1 × Raspberry Pi Pico W
- 1 × PushbuttonIn Starter Kit, Christmas Lights Kit, Logic Lab Kit.
- 1 × Resistor, 10k ΩIn Starter Kit, Claw Machine Kit, Halloween Prop 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.
- 2 × Power, 3.3 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 |
|---|---|
| Pushbutton pin 2a | Raspberry Pi Pico W pin GP14 |
| Resistor, 10k Ω pin 1 | Raspberry Pi Pico W pin GP14 |
| Resistor, 1k Ω pin 2 | Raspberry Pi Pico W pin GP15 |
| Pushbutton pin 1a | 3.3 V |
| Resistor, 10k Ω pin 2 | Ground |
| LED, red pin C | Transistor (NPN or PNP) pin C |
| Resistor, 1k Ω pin 1 | Transistor (NPN or PNP) pin B |
| Transistor (NPN or PNP) pin E | Ground |
| Resistor, 220 Ω pin 1 | 3.3 V |
| LED, red pin A | Resistor, 220 Ω pin 2 |
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.
# Two kinds of transistors, the NPN one: the button on GP14 is copied to
# GP15, which drives an S8050's base through 1k. The LED hangs from 3.3 V
# into the collector, so it lights while the button is held.
from machine import Pin
import time
button = Pin(14, Pin.IN)
base = Pin(15, Pin.OUT)
while True:
base.value(button.value())
time.sleep_ms(10)
Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- The LED never lights This project
Check the transistor's pins: base to the 1k resistor, collector to the LED's cathode, emitter to ground. - The LED is on without a press This project
Check the 10k pull-down from GP14 to ground. - The input reads random HIGH and LOW when the button is not pressed. Pushbutton
A floating pin picks up noise. Add a 10 k pull-down or pull-up resistor, or use INPUT_PULLUP and wire the button between the pin and GND. Then pressed reads LOW. - The button seems always pressed, or never changes. Pushbutton
A 4-leg tactile switch has legs joined in pairs. Place it across the breadboard center gap so the pairs sit on different rows. Wired along one side, the legs are already connected. - The LED is dim, burns out, or the pin reads wrong because the resistor value is off. Resistor
Read the color bands again. A 220 ohm to 330 ohm resistor suits a typical LED on 5 V. 10k is a common pull-up or pull-down value. Check with a multimeter if unsure. - A pull-up or pull-down resistor is missing, so the input floats and reads random values. Resistor
Tie the input to 5 V or ground through about 10k. Or use INPUT_PULLUP and wire the switch to ground. - 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.