Built-in project · Raspberry Pi Pico W

Pico W reed switch interrupt on GP14

MicroPython on a Pico W: a reed switch from 3.3 V to GP14 with a 10k pull-down. Bring the magnet close and a pin interrupt counts it. 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 15 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.

The circuit runs here once the page has loaded.

What you'll learn

  • Reed switches and magnets
  • Interrupts with irq on a rising edge
  • Debouncing contacts
  • Pull-down resistor for a switch to 3.3 V
  • Difficulty: Beginner
  • Time: About 15 minutes
  • Board: Raspberry Pi Pico W
  • Parts: 4

How it works

A reed switch closes when a magnet is near. This one joins 3.3 V to GP14, with a 10k pull-down so the pin reads 0 the rest of the time. When a magnet arrives the pin goes from 0 to 1, a rising edge, and an interrupt fires.

The interrupt handler only sets a flag. The main loop sees it, waits 50 ms for the contacts to stop bouncing, and checks the pin. If it is still 1 and a press was not already counted, it adds one and prints "magnet detected" with the total. It counts again only after the switch has been open for 50 ms.

Parts list

4 parts, plus the jumper wires. They retail for about $6.45; on Mokxi they're free.

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 pinConnects to
Reed switch pin 2Raspberry Pi Pico W pin GP14
Resistor, 10k Ω pin 1Raspberry Pi Pico W pin GP14
Reed switch pin 13.3 V
Resistor, 10k Ω pin 2Ground

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.

main.py · Raspberry Pi Pico W
# Feel the magnetism: the reed switch joins GP14 to 3.3 V while a magnet
# is near. An interrupt on the rising edge says it closed; the loop then
# waits for the contacts to stop bouncing before it counts.
from machine import Pin
import time

reed = Pin(14, Pin.IN)
flag = False
held = False
closed = 0

def near(pin):
    global flag
    flag = True                      # keep a handler short: set a flag

reed.irq(trigger=Pin.IRQ_RISING, handler=near)
while True:
    if flag:
        flag = False
        time.sleep_ms(50)            # let the contacts settle
        if reed.value() == 1 and not held:
            held = True
            closed += 1
            print("magnet detected", closed)
    if held and reed.value() == 0:
        time.sleep_ms(50)
        held = reed.value() == 1     # open for 50 ms: the magnet has gone
    time.sleep_ms(10)

Code walkthrough

The key functions of the sketch, and what each one does.

near main.py

This is the interrupt handler. It runs when GP14 rises from 0 to 1. All it does is set flag to True. The loop does the slow work later, because handlers should stay very short.

def near(pin):
    global flag
    flag = True                      # keep a handler short: set a flag

Troubleshooting

The usual mistakes with this circuit, and the fix for each.

  • One magnet pass is counted several times This project
    That is contact bounce. Keep the 50 ms waits that settle the reading.
  • Nothing is counted This project
    Check the reed switch joins 3.3 V to GP14, and the 10k resistor goes from GP14 to ground.
  • The input reads random values. Reed switch
    A reed switch is just two contacts. Use INPUT_PULLUP and wire one side to the pin and the other to GND, so closed reads LOW.
  • It triggers several times or never closes. Reed switch
    Contacts bounce, so debounce in code. The switch is most sensitive along its length, so move the magnet along the blades, not straight at the end.
  • 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.