🛠 Lire un bouton sur une broche GPIO libre

Raspberry Pi Pico 2 W · debutant · Schéma : IMG-pico-2-w-debutant-2-pico2w-bouton-gpio.svg — Pico 2 W sur breadboard, bouton poussoir entre GP15 et GND, zone GP23 à GP25 et GP29 hachurée réservée à la radio.

Objectives

Wire a push button to a truly free pin (GP0-GP22), read it using an internal pull-up, detect presses with software debouncing, and note which pins are forbidden on the Pico 2 W: GP23-GP25 and GP29 carry the power supply, CS, IRQ, and SPI clock for the CYW43.

Hardware

Complete code

// main.c - bouton sur GP15, pull-up interne, appui = niveau bas
#include "pico/stdlib.h"

#define BOUTON 15

int main(void) {
    stdio_init_all;
    gpio_init(BOUTON);
    gpio_set_dir(BOUTON, GPIO_IN);
    gpio_pull_up(BOUTON);            // pas de resistance externe necessaire
    uint32_t dernier = 0;
    bool etat_prec = true;
    while (true) {
        bool relache = gpio_get(BOUTON);
        uint32_t maintenant = to_ms_since_boot(get_absolute_time);
        if (relache != etat_prec && maintenant - dernier >= 20) {
            dernier = maintenant;
            etat_prec = relache;
            if (!relache) {
                printf("Appui !\n");   // anti-rebond logiciel 20 ms
            }
        }
        sleep_ms(1);
    }
}

Expected verification

Each press prints exactly one line Appui ! on the serial output, without duplicates. At startup, with no press, the pin remains high thanks to the internal pull-up; releasing the button does not trigger any spurious messages.

Common errors

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials