🛠 Lire un bouton poussoir sur GPIO

Raspberry Pi Pico 2 · debutant · Schéma : IMG-pico-2-debutant-2-pico2-bouton-gpio.svg — Pico 2 avec un bouton poussoir entre GP22 et la masse, résistance de pull-up interne représentée sur la broche

Objectives

Master the RP2350 GPIO inputs: input configuration, internal pull-up resistor, software debouncing, and reacting an output (LED GP25) to human action. You will also discover the RP2350-E9 erratum concerning Bank 0 pull-downs on A2 boards from before mid-2025.

Hardware

Complete code

/* bouton.c — appui sur GP22 -> LED GP25 s'allume */
#include "pico/stdlib.h"

#define BTN_PIN 22
#define LED_PIN 25

int main(void) {
    gpio_init(BTN_PIN);
    gpio_set_dir(BTN_PIN, GPIO_IN);
    gpio_pull_up(BTN_PIN);          /* pull-up interne : repos = 1 */
    gpio_init(LED_PIN);
    gpio_set_dir(LED_PIN, GPIO_OUT);

    uint pressed = 0;
    while (true) {
        uint now = !gpio_get(BTN_PIN);      /* actif à l'état bas */
        if (now != pressed) {               /* anti-rebond simple */
            sleep_ms(20);
            pressed = !gpio_get(BTN_PIN);
            gpio_put(LED_PIN, pressed);
        }
        sleep_ms(5);
    }
}

The button connects GP22 to GND; no external resistor is needed thanks to the internal pull-up.

Expected verification

LED off at rest, on while the button is pressed, without spurious blinking during the press. With a serial terminal (UART0 GP0/GP1) you can add a printf to count stable presses.

Common errors

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials