Try Embedded

Pico W in MicroPython: a call button debounced in an interrupt, with timers

MicroPython on a Raspberry Pi Pico W catches a bouncing button with Pin.irq, confirms each press with a one-shot machine.Timer, and blinks a call lamp from a…

Input · Intermediate

In a real product: Andon call button (Industrial): An operator presses the button on a machine to call for help; the lamp flashes until the next press cancels the call.

On a factory floor, each machine has an andon button: the operator presses it to call a technician, and a lamp flashes until the call is cancelled. A button's metal contacts bounce: one press closes and opens them many times in about a millisecond. This program catches every edge in an interrupt, but only counts a press once the pin has been quiet for 20 ms. A periodic timer flashes the lamp on GP15 and blinks the Pico W's own LED to show the program is alive. The main loop does no timing at all: it only prints what changed.

What you'll learn

  • Pin.irq(): a function that runs on a pin's edge, whatever the main loop is doing
  • Debouncing with a one-shot machine.Timer that every edge restarts, so it fires only after the bouncing stops
  • A periodic machine.Timer that does steady work (flashing a lamp) without sleep in the main loop
  • Keeping callbacks short: they change a few variables, and the main loop does the printing

How it works

The button joins GP14 to GND, and Pin.PULL_UP keeps the pin at 1 until it is pressed. button.irq(edge, ...) asks for edge() to run on every falling and rising edge. During a bounce that is 10 to 30 calls in a millisecond.

edge() does two small things: it counts the edge, and it (re)starts the one-shot timer settle for 20 ms. Each new edge starts the 20 ms again, so settled() runs only once the contacts have been still for 20 ms. Then it reads the pin. If the pin is now 0 and was 1 before, that is one real press, and it toggles calling.

The periodic timer flasher runs flash() four times a second. While a call is on, it toggles the lamp; otherwise it keeps the lamp off. Every fourth tick it toggles Pin("LED"), the LED on the Pico W's radio, as a heartbeat.

On the RP2040, MicroPython runs these callbacks "soft" by default: the interrupt only queues the function, and it runs between two lines of the main program. That is why they may use global variables safely. The main loop sleeps 100 ms at a time and prints a line when the number of presses has changed.

When it starts, the Serial Monitor shows:

andon ready: press the button to call

Press the button and it prints a line such as press 1: calling (edges so far: 22). The edge count shows how many times the contacts bounced.

Try this

  • Click the button and set its bounce to 0: each press now gives exactly two edges, one down and one up. Real buttons are not that kind.
  • Make edge() toggle calling itself and remove the timer: one press now toggles the call many times, and it ends up on or off by chance.
  • Cancel a call only with a long press: in settled(), remember time.ticks_ms() when the button goes down, and on release ignore presses shorter than one second.

main.py

# An andon call button on a Raspberry Pi Pico W: GP14 is the button (to GND), GP15 the call lamp.
# Each press toggles the call. Edges are caught in an interrupt, and a press counts once the pin is quiet for 20 ms.
from machine import Pin, Timer
import time

DEBOUNCE_MS = 20

button = Pin(14, Pin.IN, Pin.PULL_UP)   # 1 when released, 0 when pressed
lamp = Pin(15, Pin.OUT)                 # the call lamp on the machine
alive = Pin("LED", Pin.OUT)             # the Pico W's own LED: a heartbeat

calling = False     # is a call on?
presses = 0         # real presses, after debouncing
edges = 0           # every edge the interrupt saw, bounces included
stable = 1          # the button's last settled level
ticks = 0

settle = Timer()    # one-shot: fires 20 ms after the last edge
flasher = Timer()   # periodic: flashes the lamp


def settled(t):
    # The pin has been still for 20 ms: read it and act on a real change.
    global stable, presses, calling
    level = button.value()
    if level == 0 and stable == 1:      # released -> pressed
        presses += 1
        calling = not calling
    stable = level


def edge(pin):
    # Every edge, bounces too: count it and start the 20 ms again.
    global edges
    edges += 1
    settle.init(mode=Timer.ONE_SHOT, period=DEBOUNCE_MS, callback=settled)


def flash(t):
    # Four times a second: flash the lamp during a call, and blink the heartbeat once a second.
    global ticks
    ticks += 1
    lamp.value(not lamp.value() if calling else 0)
    if ticks % 4 == 0:
        alive.toggle()


button.irq(edge, Pin.IRQ_FALLING | Pin.IRQ_RISING)
flasher.init(mode=Timer.PERIODIC, freq=4, callback=flash)
print("andon ready: press the button to call")

reported = 0
while True:
    if presses != reported:
        reported = presses
        print("press {}: {} (edges so far: {})".format(presses, "calling" if calling else "call cancelled", edges))
    time.sleep_ms(100)

Lessons behind it