Use RP2040 GPIO Interrupts With gpio_set_irq_enabled_with_callback
Handle a Pico button press with an RP2040 GPIO interrupt: the event mask, the callback signature, one shared callback for several pins, and a flag for main.

Polling a button in a loop works until the loop has other work to do or the CPU should be asleep. A GPIO interrupt lets the hardware call a function the moment the pin changes. The Pico SDK wraps the RP2040's interrupt hardware in one call, gpio_set_irq_enabled_with_callback, and the main trap in using it is a non-obvious one: the SDK has a single callback for all GPIO interrupts on a core, so the call registers it once and every other pin uses a different function. This post sets up a falling-edge interrupt on a button with Pico SDK 2.0, shows a shared callback for two pins, and explains what the callback may do. It builds on the button reading post of this cluster and uses the same wiring: a button between GPIO 15 and ground.
The event mask
The RP2040 can raise a GPIO interrupt on four conditions per pin. The SDK names them in hardware/gpio.h:
| Constant | Value | Triggers when |
|---|---|---|
GPIO_IRQ_LEVEL_LOW | 0x1 | The pin is low (continuously) |
GPIO_IRQ_LEVEL_HIGH | 0x2 | The pin is high (continuously) |
GPIO_IRQ_EDGE_FALL | 0x4 | The pin goes from high to low |
GPIO_IRQ_EDGE_RISE | 0x8 | The pin goes from low to high |
The values can be combined with |. For a button with a pull-up, a press is a falling edge and a release is a rising edge. Level triggers keep firing as long as the condition holds, so they are used for signals that stay asserted until acknowledged, not for buttons.
The callback receives the pin number and a mask of the events that occurred, and has the type gpio_irq_callback_t, which is void (*)(uint gpio, uint32_t event_mask).
The call
#include <stdio.h>
#include "pico/stdlib.h"
#include "hardware/gpio.h"
#define BUTTON_PIN 15
static volatile bool pressed;
static void gpio_callback(uint gpio, uint32_t events)
{
if (gpio == BUTTON_PIN && (events & GPIO_IRQ_EDGE_FALL))
pressed = true;
}
int main(void)
{
stdio_init_all();
gpio_init(BUTTON_PIN);
gpio_set_dir(BUTTON_PIN, GPIO_IN);
gpio_pull_up(BUTTON_PIN);
gpio_set_irq_enabled_with_callback(BUTTON_PIN, GPIO_IRQ_EDGE_FALL, true, &gpio_callback);
for (;;) {
if (pressed) {
pressed = false;
printf("button pressed\n");
}
__wfi();
}
}gpio_set_irq_enabled_with_callback(gpio, event_mask, enabled, callback) does two things: it enables the interrupt for that pin and those events (enabled true), and it installs callback as the handler for the GPIO interrupt on the calling core, enabling the interrupt in the NVIC (IO_IRQ_BANK0). The SDK documentation states that this is a convenience for the first pin and that the callback is shared by all pins on that core.
__wfi() is the ARM "wait for interrupt" instruction, exposed by the SDK. The core sleeps until any interrupt arrives; the callback sets pressed, the core wakes, the loop prints and goes back to sleep. If a press arrives between the check and the __wfi, __wfi returns immediately because the pending interrupt wakes it, so the event is not lost; the loop will see the flag on the next pass.
A press now prints:
button pressedwith no polling, and the CPU is asleep between events. Contact bounce will print several lines for one press, which is the debounce topic.
Why a flag and not a printf in the callback
The callback runs in interrupt context. The rules:
Keep it short; other interrupts are delayed while it runs.
Do not call functions that block or take locks that a thread might hold.
printfover USB or UART uses locks and buffers and can deadlock if the interrupted code was in the middle of its ownprintf.Anything shared with the main loop must be
volatile(so the compiler re-reads it) and, if it is larger than one machine word or needs a consistent multi-field read, protected. A singleboolwritten in the callback and read in the loop needs nothing more on this core.
The pattern "set a flag in the interrupt, act on it in the main loop" is the simplest correct structure. For more events, use a queue: the SDK provides queue_t in pico/util/queue.h, which is safe to add to from an interrupt (queue_try_add).
Several pins, one callback
The SDK keeps one callback per core for the whole GPIO bank. A second call to gpio_set_irq_enabled_with_callback for another pin replaces the callback, so the second call's function takes over for both pins. For several pins, install the callback once with the first call, then enable others with gpio_set_irq_enabled, which does not touch the callback:
#define BUTTON_A 15
#define BUTTON_B 14
static volatile uint32_t events_seen;
static void gpio_callback(uint gpio, uint32_t events)
{
if (events & GPIO_IRQ_EDGE_FALL)
events_seen |= (1u << gpio);
}
void irq_setup(void)
{
gpio_init(BUTTON_A);
gpio_pull_up(BUTTON_A);
gpio_init(BUTTON_B);
gpio_pull_up(BUTTON_B);
gpio_set_irq_enabled_with_callback(BUTTON_A, GPIO_IRQ_EDGE_FALL, true, &gpio_callback);
gpio_set_irq_enabled(BUTTON_B, GPIO_IRQ_EDGE_FALL, true);
}The events_seen bit mask records which pins fired, by pin number. The main loop reads and clears it. Clearing needs care because the callback can set a bit between the read and the clear. The simple fix is to disable interrupts around the pair, using the SDK's save_and_disable_interrupts and restore_interrupts from hardware/sync.h:
uint32_t take_events(void)
{
uint32_t saved = save_and_disable_interrupts();
uint32_t e = events_seen;
events_seen = 0;
restore_interrupts(saved);
return e;
}The Cortex-M0+ in the RP2040 has no atomic read-modify-write instructions, so a short interrupt-disabled section is the right tool for a flag word that both the callback and the main loop change.
What the SDK does for you, and the acknowledgement
For edge interrupts the RP2040 latches the event in a raw status register until software clears it. The SDK's internal dispatcher, the function behind the registered callback, reads the status, calls your callback for each pin with events and then acknowledges (clears) the edge events with gpio_acknowledge_irq. You do not clear them yourself in the callback. If you write your own handler with irq_set_exclusive_handler(IO_IRQ_BANK0, ...), you must clear each edge event with gpio_acknowledge_irq(gpio, event_mask) or the interrupt fires again forever. Level events cannot be cleared this way: they stay asserted while the condition holds, which is why they need the source to be cleared elsewhere.
Interrupts on the second core
The RP2040 has two cores, and each core has its own set of GPIO interrupt enables for the same pins. gpio_set_irq_enabled_with_callback acts on the core that calls it, which is core 0 by default. To receive the interrupt on core 1, call it from code running on core 1. A project that uses both cores and registers the same pin on both gets two callbacks for one edge.
Debouncing inside the callback
The callback can ignore edges that arrive too soon after a previous one, using the SDK's free-running microsecond timer:
static volatile uint32_t last_us;
static void gpio_callback(uint gpio, uint32_t events)
{
uint32_t now = time_us_32();
if (now - last_us < 20000u) /* 20 ms lockout */
return;
last_us = now;
pressed = true;
}time_us_32() reads the low 32 bits of the 64-bit system timer, which counts microseconds from boot and wraps after about 71 minutes. The unsigned subtraction now - last_us stays correct across the wrap. The first edge is accepted at once and later edges within 20 ms are dropped, which is the lockout filter from the debounce post of this cluster.


