Pico W tilt switch on GP14
MicroPython on a Pico W: a ball tilt switch from 3.3 V to GP14 with a 10k pull-down. Tip it over and the program prints that it opened. The whole build, a Raspberry Pi Pico W and 2 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.35. On Mokxi they're free.
What you'll learn
- How a ball tilt switch works
- Wiring a switch with a pull-down resistor
- Reading a digital input with Pin.IN
- Printing only when the state changes
- Difficulty: Beginner
- Time: About 10 minutes
- Board: Raspberry Pi Pico W
- Parts: 4
How it works
A tilt switch has a small ball inside. Standing upright, the ball touches the contacts and joins GP14 to 3.3 V. Tip it over and the ball rolls away, the contact opens, and the 10k pull-down resistor pulls GP14 to 0 V.
The script checks the pin every 20 milliseconds. When the value changes it prints The switch works! when the pin drops to 0, or upright again when it returns to 1.
Parts list
4 parts, plus the jumper wires. They retail for about $6.35; on Mokxi they're free.
- 1 × Raspberry Pi Pico W
- 1 × Tilt switch
- 1 × Resistor, 10k ΩIn Starter Kit, Claw Machine Kit, Halloween Prop Kit.
- 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 |
|---|---|
| Tilt switch pin 2 | Raspberry Pi Pico W pin GP14 |
| Resistor, 10k Ω pin 1 | Raspberry Pi Pico W pin GP14 |
| Tilt switch pin 1 | 3.3 V |
| Resistor, 10k Ω 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.
# Tilt it: the tilt switch joins GP14 to 3.3 V while it stands up. Tip it
# over and the ball rolls off the contacts, the 10k pulls GP14 to 0 V,
# and the program says so.
from machine import Pin
import time
tilt = Pin(14, Pin.IN)
was = 1
while True:
now = tilt.value()
if now != was:
print("The switch works!" if now == 0 else "upright again")
was = now
time.sleep_ms(20)
Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- The value flickers or never changes This project
Check that the 10k resistor connects GP14 to ground. Without it the pin floats when the switch is open. - The reading flickers when the switch is moved. Tilt switch
The ball bounces on the contacts, giving many fast changes. Debounce in software by waiting 20 to 50 ms before accepting a new state, or average a few readings. - The input floats and never gives a clean reading. Tilt switch
A tilt switch is just a switch, so the pin floats when it is open. Use INPUT_PULLUP with the switch between the pin and GND, or add a 10 k pull-down resistor. - 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. - 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. - WiFi code fails to connect, or the on-board LED code does nothing. The LED on the Pico W is not on GP25 like on the plain Pico. Raspberry Pi Pico W
Use the board's LED_BUILTIN name so the library drives it through the WiFi chip. Join a 2.4 GHz network only, with the right SSID and password, and wait for the link before sending.
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.