LED pin โ OUTPUT. Button pin (GPIO 11) โ INPUT.
Tell the hardware: on rising edge at GPIO 11, call interrupt_handler().
CPU enters while(true) โ doing nothing, or any other task. The interrupt is armed and watching.
Hardware detects LOWโHIGH transition on GPIO 11. Fires IRQ. CPU saves context, jumps to ISR.
interrupt_handler() runs: toggles LED 5ร at 1s intervals using busy_wait_ms().
ISR returns. CPU restores context. Main loop continues exactly where it paused.
#include <stdio.h> #include "pico/stdlib.h" #include "hardware/gpio.h" #define LED_OUT PICO_DEFAULT_LED_PIN #define EXT_INT_PIN 11 // ISR โ called automatically when button is pressed // CPU suspends main loop, runs this, then returns void interrupt_handler() { // Toggle LED 5 times โ visual proof interrupt fired for (int i = 0; i < 5; i++) { gpio_put(LED_OUT, 1); busy_wait_ms(1000); gpio_put(LED_OUT, 0); busy_wait_ms(1000); } } int main() { stdio_init_all(); // Setup LED as output gpio_init(LED_OUT); gpio_set_dir(LED_OUT, GPIO_OUT); // Setup interrupt pin as input gpio_init(EXT_INT_PIN); gpio_set_dir(EXT_INT_PIN, GPIO_IN); // Register ISR: fire on rising edge (button press) gpio_set_irq_enabled_with_callback( EXT_INT_PIN, GPIO_IRQ_EDGE_RISE, true, &interrupt_handler ); // Main loop โ CPU free to do other work while (true); return 0; }
Component GPIO Pin Direction โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ LED (onboard) GPIO 25 OUTPUT Button (EXT) GPIO 11 INPUT โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ // EXT_INT_PIN watches for rising edge // LED_OUT toggles 5ร on each interrupt