Pico W micro switch on GP14
MicroPython on a Pico W: a lever micro switch with its common on GP14 reads 1 while pressed and 0 at rest, and prints each change. 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 15 minutes to build and run. Runs in your browser. Free, and no account needed.
The parts retail for about $6.55. On Mokxi they're free.
What you'll learn
- Common, normally closed and normally open contacts
- Reading a switch that drives the pin both ways
- Smoothing noise with a capacitor
- Reacting only to a change in state
- Difficulty: Beginner
- Time: About 15 minutes
- Board: Raspberry Pi Pico W
- Parts: 5
How it works
The lever microswitch has a common leg on GP14. At rest, the common leg touches the NC leg, which goes to ground, so GP14 reads 0. When the lever is pressed, the common leg touches the NO leg, which goes to 3.3 V, and GP14 reads 1.
A 10k resistor to ground and a 100 nF capacitor on the same pin keep the signal steady. The program checks every 20 ms and prints a message only when the state changes.
Parts list
5 parts, plus the jumper wires. They retail for about $6.55; on Mokxi they're free.
- 1 × Raspberry Pi Pico W
- 1 × Limit switch, lever microswitchIn Claw Machine Kit, Vending Machine 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 |
|---|---|
| Limit switch, lever microswitch pin COM | 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 |
| Limit switch, lever microswitch pin NC | Ground |
| Limit switch, lever microswitch pin NO | 3.3 V |
| 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.
# Press gently: the micro switch's common leg is on GP14. Resting, it
# touches the ground leg; pressed, the 3.3 V one, so GP14 reads 1 while
# the lever is down.
from machine import Pin
import time
lever = Pin(14, Pin.IN)
was = 0
while True:
now = lever.value()
if now != was:
print("lever pressed" if now else "lever released")
was = now
time.sleep_ms(20)
Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- Pressed and released are the wrong way around. This project
Check that NC goes to ground and NO goes to 3.3 V. Swapping them flips the reading. - Nothing is printed. This project
Make sure the common leg (COM) is on GP14, along with the resistor and the capacitor. - The input reads the wrong level, or floats when released. Limit switch
Use COM and one contact. NC is closed at rest, NO closes when pressed. Add a pull-up or INPUT_PULLUP so the pin never floats. - Readings chatter when the lever moves. Limit switch
Contacts bounce for a few milliseconds. Debounce in software with a 10 to 20 ms delay, or add a small capacitor across the switch. - 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.
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.