Pico W slide switch on GP14
MicroPython on a Pico W: a slide switch puts GP14 on 3.3 V or on ground, with a 10k and a 100 nF to steady it, and the program prints each side. The whole build, a Raspberry Pi Pico W and 3 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 10 minutes to build and run. Runs in your browser. Free, and no account needed.
The parts retail for about $6.30. On Mokxi they're free.
What you'll learn
- A two position switch choosing between 3.3 V and ground
- Reading a digital input with Pin.IN
- Printing only on a change
- What a capacitor and resistor do to steady a signal
- Difficulty: Beginner
- Time: About 10 minutes
- Board: Raspberry Pi Pico W
- Parts: 5
How it works
The slide switch's middle leg goes to GP14. Slide one way and GP14 connects to 3.3 V. Slide the other way and it connects to ground. So the pin always has a clear high or low.
A 10k resistor and a 100 nF capacitor from GP14 to ground hold the pin steady while the contacts move. The script checks every 20 milliseconds and prints a message only when the state changes.
Parts list
5 parts, plus the jumper wires. They retail for about $6.30; on Mokxi they're free.
- 1 × Raspberry Pi Pico W
- 1 × Slide switchIn Logic Lab Kit, Spaceship Panel Kit.
- 1 × Resistor, 10k ΩIn Starter Kit, Claw Machine Kit, Halloween Prop Kit.
- 1 × Capacitor, 100 nF
- 1 × Power, 3.3 V
Kits that can build it
This starter kit has 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 |
|---|---|
| Slide switch pin 2 | Raspberry Pi Pico W pin GP14 |
| Resistor, 10k Ω pin 1 | Raspberry Pi Pico W pin GP14 |
| Capacitor, 100 nF pin 1 | Raspberry Pi Pico W pin GP14 |
| Slide switch pin 1 | 3.3 V |
| Slide switch pin 3 | Ground |
| Resistor, 10k Ω pin 2 | Ground |
| Capacitor, 100 nF pin 2 | 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.
# Toggle left and right: the slide switch's middle leg is on GP14, one
# end on 3.3 V and the other on ground. The 10k and the 100 nF capacitor
# from GP14 to ground hold it still while the contacts move.
from machine import Pin
import time
switch = Pin(14, Pin.IN)
was = None
while True:
now = switch.value()
if now != was:
print("switched to 3.3 V" if now else "switched to ground")
was = now
time.sleep_ms(20)
Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- Nothing prints when you slide the switch This project
Check that the middle leg goes to GP14, one end to 3.3 V and the other end to ground. - The input reads random values in one position. Slide switch
A switch only connects or disconnects. Add a pull-up or pull-down resistor, or use INPUT_PULLUP, so the pin has a defined level when the switch is open. - The load never turns on when the switch is flipped. Slide switch
Use the center (common) leg and one outer leg. The unused outer leg goes nowhere. Check that the switch is in series with the load. - 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. - An electrolytic capacitor heats up, bulges or pops. Capacitor
Electrolytics have a polarity. The stripe marks the negative leg, which goes toward ground. Also keep the voltage under the rated value, such as 10 V or 16 V. - An RC circuit charges much slower or faster than expected. Capacitor
The time constant is R times C. A 10 k resistor with a 100 uF capacitor gives 1 second, and about 5 seconds to charge fully. Check that you read the value on the part right. - Pins are powered from 5 V, or a 5 V signal goes into a GPIO. The RP2040 pins are 3.3 V only and are not 5 V tolerant. Raspberry Pi Pico W
Power the board from VBUS or VSYS at 5 V, but keep GPIO signals at 3.3 V. Use a level shifter or a divider for 5 V parts.
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.