🛠 Allumer une LED avec un bouton poussoir sur le CM3

Raspberry Pi Compute Module 3 (CM3 / CM3L) · debutant · Schéma : bouton poussoir entre GPIO4 et GND, LED avec résistance 330 Ω entre GPIO17 et GND, les deux reliés à la carte CMIO3 portant un module CM3.

Objectives

Combine a GPIO input and output: read a push button and light up an LED while pressed, with mechanical bounce handling integrated into gpiozero. You will also learn about the role of the CM3's weak internal pulls (50-65 kΩ, datasheet RP-008161-DS §6), present on all pins except GPIO28/29/44/45.

Hardware

Complete code

#!/usr/bin/env python3
# bouton_led.py — bouton sur GPIO4 (vers GND), LED sur GPIO17
from gpiozero import Button, LED
from signal import pause

btn = Button(4, pull_up=True, bounce_time=0.05)  # rebond ignoré 50 ms
led = LED(17)

btn.when_pressed = led.on
btn.when_released = led.off

pause   # boucle d'événements gpiozero (gère le SIGINT)
python3 bouton_led.py

Expected verification

Pressing the button turns on the LED, releasing it turns it off, without parasitic flickering thanks to bounce_time. Ctrl-C terminates the program cleanly. Variant without wiring: placing a finger on GPIO4 sometimes triggers the input — a sign that the pin is indeed in a sensitive pull-up state.

Common errors

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials