🛠 Lire un bouton poussoir et afficher dans la console

LattePanda 3 Delta (id: lattepanda-3) · debutant · Schéma : IMG-lattepanda-3-debutant-2-bouton-console.svg — Bouton poussoir entre la broche D2 et le GND avec résistance de pull-up 10 kΩ vers 3,3 V, lecture de l'état 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

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

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials