🛠 Lire un bouton poussoir sur GPIO
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
- 1 × Raspberry Pi Pico 2
- 1 × push button
- Jumper cables; Pico SDK ≥ 2.0.0
- Pin used: GP22 (digital only — pinout confirmed by the datasheet)
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
- Forgetting
gpio_pull_up: the input floats and the LED changes state randomly. - Wiring the button between GP22 and 3V3 without an external resistor: short circuit at rest if you also enable a pull-down.
- Erratum RP2350-E9 (stepping A2, boards purchased before mid-2025): Bank 0 internal pull-downs may leak (~120 µA); if a reliable pull-down is critical, add an external resistor ≤ 8.2 kΩ. Fixed in A3/A4 (announced on July 29, 2025).
- Not implementing debouncing: a single press is counted ten times in a counter.
Other tutorials for this board
Code examples for this board
clignotante.pyPythonbouton-interruption.pyPythonpwm-servo.pyPythonlecture-adc.pyPythonneopixel.pyPythonoled-i2c.pyPython