11–12. Time and events
A program does not have to do everything in one loop. Sometimes you want a function to run later or every so often, while the main program keeps doing something else. MicroPython gives you Timer, pin interrupts and the Signal output wrapper.
There is one rule that explains every surprise in this module: callbacks here are cooperative. They run at safe points — right after a sleep — not in the middle of your code.

This is different from a real Pico, where a timer interrupt can run at almost any instant. It makes the simulation reproducible, and it makes a class of bugs visible instead of mysterious.
Lesson 11 — Timers
You will be able to: blink without sleeping between toggles, run a one-shot action, and stop a timer cleanly.
The idea
A periodic timer calls a function every period, forever. A one-shot timer calls it once. The main program still has to sleep, because that is when callbacks get their turn.
from machine import Pin, Timer
import utime
led = Pin(13, Pin.OUT)
def flip(timer):
led.toggle()
timer = Timer()
timer.init(period=500, mode=Timer.PERIODIC, callback=flip)
while True:
utime.sleep_ms(10)
period is in milliseconds. 500 ms per toggle is one full on/off cycle per second.
The exact tools
| What | MicroPython |
|---|---|
| Create a timer | timer = Timer() |
| Periodic | timer.init(period=500, mode=Timer.PERIODIC, callback=fn) |
| One-shot | timer.init(mode=Timer.ONE_SHOT, period=1000, callback=fn) |
| Stop and release | timer.deinit() |
| The callback's argument | the timer object itself (fn(timer)) |
Build it
Blink with a timer. Keep the LED on GP13, let the timer flip it every 500 ms, and leave the main loop at sleep_ms(10). Compare it with lesson 2: the same blink, with no sleep between toggles.
One-second glow. Light the LED, then start a one-shot timer to turn it off after 1000 ms:
led.on()
def off(timer):
led.off()
timer = Timer()
timer.init(mode=Timer.ONE_SHOT, period=1000, callback=off)
while True:
utime.sleep_ms(10)
Fun fact. A 500 ms period means the timer fires twice per second, and the LED completes one on/off cycle per second. “Period” is the time between callbacks, not the blink rate. Read the units every time.
What you should see
- Periodic: a clean 1 Hz blink, with the main loop doing nothing but sleeping.
- One-shot: on for the first second, off afterwards, and it stays off.
Common mistakes
| What you see | Why | Fix |
|---|---|---|
| Nothing happens | The main loop never sleeps | Add utime.sleep_ms(10) |
| The LED flickers too fast | period=50 instead of 500 | Milliseconds, not seconds |
| Several flips at once | A long sleep let the timer catch up | Sleep in small steps, or accept the catch-up |
| The callback crashes the run | An exception inside a callback | Keep callbacks short and safe |
NameError for Timer | The import is missing | from machine import Timer |
Check yourself
- What is the difference between
PERIODICandONE_SHOT? - Why does the main loop need a sleep?
- Where should the heavy work live: in the callback or the main loop?
Answers: periodic repeats, one-shot fires once · callbacks run after a sleep · the main loop, so the callback stays quick.
Remember
Timer.init(period=…, mode=…, callback=…);deinit()releases it.- Callbacks are cooperative: no sleep, no tick.
- Keep callbacks short; let the main loop do the thinking.
Problems: Blink with a timer · One-second glow
Lesson 12 — Pin interrupts
You will be able to: run a function when a pin changes, debounce it, and use Signal for an active-low output.
The idea
Pin.irq() attaches a function to a pin edge. On a real Pico the CPU stops what it is doing and runs the handler; here the handler runs at the next cooperative tick (during a sleep). The API is the same, so the code moves to hardware unchanged.
from machine import Pin
import utime
button = Pin(12, Pin.IN, Pin.PULL_DOWN)
presses = 0
def changed(pin):
global presses
presses += 1
irq = button.irq(trigger=Pin.IRQ_FALLING, handler=changed)
while True:
print("presses", presses)
utime.sleep_ms(100)
trigger is a bitmask: Pin.IRQ_RISING, Pin.IRQ_FALLING, or both. The handler receives the Pin that changed. irq.off() stops it; irq.on() starts it again.
Signal is a different tool: it wraps a pin as a logical output, with optional inversion.
from machine import Pin, Signal
led = Signal(Pin(15, Pin.OUT), invert=True)
led.on() # drives the physical pin LOW
led.off() # drives it HIGH
print(led.value())
That is exactly how an active-low LED or relay is wired on a real board.
The exact tools
| What | MicroPython |
|---|---|
| Watch a pin edge | irq = pin.irq(trigger=Pin.IRQ_FALLING, handler=fn) |
| Stop / restart | irq.off() / irq.on() |
| Invert an output | Signal(pin, invert=True) |
| Set logical level | signal.on(), signal.off(), signal.value(1) |
Build it
- Count button presses with
Pin.IRQ_FALLING; print the count. - Add debouncing: after the handler fires, wait 20 ms and check the pin again before counting.
- Use
Pin.IRQ_RISING | Pin.IRQ_FALLINGand print both edges. - Wire an active-low LED through
Signal(..., invert=True)and prove thaton()drives the pin low.
Careful. The simulator checks edges during sleeps. If a pulse is shorter than your longest sleep, it can be missed. For a human button that is fine; for a fast sensor, poll or use a bus device.
Common mistakes
| What you see | Why | Fix |
|---|---|---|
| The handler never runs | The main loop never sleeps | Add a small sleep |
| One press counts many times | Contact bounce | Debounce inside the handler |
| A press is missed | The pulse is shorter than the sleep | Sleep in smaller steps |
Signal does not watch edges | It is an output wrapper, not an interrupt | Use Pin.irq() for edges |
signal.off() stops the pin | Signal is an output wrapper, not an interrupt | It drives the logical output low; use irq.off() to stop edges |
Check yourself
- What triggers a
Pin.irqhandler? - Why can a short pulse be missed?
- What does
Signal(pin, invert=True).on()do to the physical pin?
Answers: a configured rising/falling edge · edges are checked during sleeps · it drives the physical pin low.
Remember
- Use
Pin.irq()for edges, notSignal. - Cooperative means “during a sleep”, not “instantly”.
- Debounce human inputs; do not use an interrupt for very fast pulses without checking timing.
Signalis for logical, possibly inverted, outputs.
Problems: use the debounce and edge patterns from Press to toggle and Short or long?.
Choosing between a loop, a timer and a signal
| You need | Use | Why |
|---|---|---|
| Read a sensor every 100 ms | main loop + sleep_ms(100) | Simple, predictable |
| Blink while doing something else | periodic Timer | Independent of loop timing |
| Turn something off once after a delay | one-shot Timer | No state machine needed |
| React to a human press | Pin.irq() or polling | Both are fine; debounce either way |
| Catch a 1 ms pulse | polling, or a bus device | A cooperative interrupt may miss it |
The design question. “What needs to happen while the program is sleeping?” If the answer is “nothing”, a plain loop is clearer. If the answer is “a blink” or “a timeout”, reach for a timer.
Next
- Real loads — switch a lamp, a fan or a motor.
- The course map — where you are.
- The machine reference — the exact timer and signal API.