🛠 Lire un bouton poussoir et afficher dans la console
Objectives
Complete the first tutorial by adding a digital input: a push button read by the embedded ATmega32U4 microcontroller, with the state displayed in the x86 PC console via pinpong. You will learn how to wire an input (pull-up resistor), how to use read_digital, and about hardware debouncing. This is the foundation of any human-machine interaction with pins.
Hardware
- LattePanda 3 Delta properly powered (12 V ≥ 2 A or USB-C PD ≥ 24 W)
- 1 push button with 2 or 4 pins
- 1 10 kΩ resistor (pull-up)
- Dupont cables
- Installed library:
pip install pinpong
Complete code
Wiring: one leg of the button → D2, the other → GND; 10 kΩ resistor between D2 and 3.3V (pull-up). Pressing the button connects D2 to GND = level 0.
import time
from pinpong.board import Board, Pin
Board("leonardo", "COM5").begin # Linux : "/dev/ttyACM0"
btn = Pin(Pin.D2, Pin.IN) # D2 en entrée numérique
etat_precedent = None
while True:
etat = btn.read_digital # 0 quand le bouton est pressé (pull-up)
if etat != etat_precedent: # n'afficher qu'aux changements
if etat == 0:
print("Bouton PRESSÉ")
else:
print("Bouton relâché")
etat_precedent = etat
time.sleep(0.05) # anti-rebond logiciel (50 ms)
Expected verification
Each time the button is pressed, the console displays "Button PRESSED", then "Button released" when it is released, without spurious signals thanks to the 50 ms delay. Stop the program with Ctrl+C.
Common errors
- Floating input: without a pull-up (or pull-down), the pin floats and displays random states. The 10 kΩ resistor to 3.3 V is mandatory with this wiring.
- Inverted logic: with a pull-up, pressing the button gives 0 instead of 1. If the display seems inverted, this is normal: it is active-low wiring.
- Wrong port: once again, it is the Leonardo's virtual serial port (
/dev/ttyACM0on Linux) that must be passed toBoard. - Logic level: >
- Occupied pin: D2 is not a PWM pin marked
~(PWM pins are D3, D5, D6, D9, D10, D11, D13), but it is perfectly usable as a digital input.
Other tutorials for this board
Code examples for this board
clignotante.pyPythonbouton.pyPythonservo.pyPythonanalogique.pyPythonrespiration.pyPythonneopixel.pyPython