🛠 Bouton et LED avec Adafruit_BBIO, la bibliothèque historique
Objectives
Control an output and read an input on the cape headers using Adafruit_BBIO, the historical Python library of the BeagleBone ecosystem: thousands of BBB tutorials use it and you will encounter it. You will also learn the mapping from header pin to GPIO line (P8_11 = gpio1_13, which is line 45 of the single gpiochip0 on the AM335x) and the equivalent reading using modern libgpiod.
Hardware
- 1 BeagleBone Black + power supply
- 1 breadboard, wires, 1 push button, 1 LED + 1 330 Ω resistor
- Wiring: button between P9_11 and GND (internal pull-up); LED + 330 Ω between P8_11 and GND
- Reminder: all logic is at 3.3 V — 5 V on a pin destroys the board.
Complete Code
#!/usr/bin/env python3
# pip3 install Adafruit_BBIO (images communautaires Debian)
import Adafruit_BBIO.GPIO as GPIO
LED, BTN = "P8_11", "P9_11"
GPIO.setup(LED, GPIO.OUT)
GPIO.setup(BTN, GPIO.IN, pull_up_down=GPIO.PUD_UP) # bouton vers GND
try:
while True:
# actif-à-l'état-bas : appuyé == LOW
GPIO.output(LED, GPIO.input(BTN) == GPIO.LOW)
finally:
GPIO.cleanup # libère les broches, sinon elles restent réclamées
# Équivalent moderne sans Python (libgpiod, images récentes) :
# P8_11 = gpio1_13 = ligne 45 du gpiochip0 ; les noms P8_xx viennent du DT
gpioset -t 200m gpiochip0 P8_11=1 # impulse 200 ms
gpioget gpiochip0 P9_11 # état du bouton
gpiodetect ; gpioinfo gpiochip0 | grep -E "P8_11|P9_11"
Expected Verification
LED off at rest, on when the button is pressed. gpioinfo shows both pins claimed by the script during execution, and released after GPIO.cleanup. With gpioset, you see the LED light up for 200 ms.
Common Errors
- Adafruit_BBIO is legacy heritage: on very recent Debian images it may be missing or unstable — prefer libgpiod for new projects, but know how to read old code.
- Forgetting PUD_UP: without a pull-up, the input floats and the LED flickers randomly.
- Inverting the logic: button wired to GND = pressed gives LOW.
- Skipping GPIO.cleanup in a script that is restarted: the pin remains occupied and the second launch fails.
- Confusing the numbering: P8_11 and P9_11 are pins on DIFFERENT headers; P9_11 is also called uart4_rxd in the device tree.
- Forgetting the LED resistor: the LED or the pin dies (respect the 3.3 V and pin current limits).
Other tutorials for this board
- Premier boot de la BeagleBone Black avec un seul câble mini-USB
- Comprendre les triggers en pilotant les 4 LED utilisateur
- Lire l'ADC 12 bits sans griller le SoC : la règle des 1,8 V
- Ajouter une horloge temps réel sur le bus I2C des capes
- Micro-HDMI : exploiter les limites d'affichage de la BBB
- Capes historiques : capemanager, EEPROM d'identification et pièges des broches
Code examples for this board
clignotante.pyPythonbouton-gpio.pyPythoncapteur-i2c.shBashsurveillance-temperature.shBashcapteur-dht11.pyPythonservo-pwm-sysfs.shBash