🛠 Lire l'ADC 12 bits sans griller le SoC : la règle des 1,8 V
Objectives
Use the AM335x 8-channel 12-bit ADC via the mainline IIO subsystem, and above all, avoid the number one pitfall on the BeagleBone Black regarding analog inputs: the AIN inputs accept 1.8 V AT MOST (analog supply VDD_ADC = 1.8 V on P9_32), while the rest of the board operates at 3.3 V. Exceeding 1.8 V damages the SoC.
Hardware
- 1 BeagleBone Black
- Channels available on P9: AIN0=P9_39, AIN1=P9_40, AIN2=P9_37, AIN3=P9_38, AIN4=P9_33, AIN5=P9_36, AIN6=P9_35 ; VDD_ADC (1.8 V) = P9_32 ; AGND = P9_34
- To measure a 0-3.3 V signal: voltage divider (e.g., R1=10 kΩ in series, R2=4.7 kΩ to GND → factor 0.32, i.e., max ~1.05 V)
- 1 potentiometer powered at 1.8 V (P9_32) for risk-free testing
Complete Code
# Repérer le device IIO et les canaux activés
ls /sys/bus/iio/devices/iio:device0/
cat /sys/bus/iio/devices/iio:device0/in_voltage0_label # vérifier quel AIN
#!/usr/bin/env python3
"""Lit AIN4 (P9_33) sur une BBB mainline. Entrée MAX : 1,8 V !"""
import glob, time
dev = glob.glob("/sys/bus/iio/devices/iio:device*")[0] # "ti_am335x_adc"
# L'index N dépend des canaux activés dans le DT : vérifier les labels !
CHAN = "in_voltage0_raw"
while True:
raw = int(open(f"{dev}/{CHAN}").read) # code 12 bits : 0..4095
mv = raw * 1800 / 4095 # référence pleine échelle 1,8 V
print(f"raw={raw} {mv:.0f} mV (entrée directe ; diviseur ?)")
time.sleep(0.2)
Expected Verification
With the potentiometer wiper connected between P9_32 (1.8 V) and P9_34 (AGND) to AIN4, the raw value follows 0 to 4095 and the calculated millivolts follow 0 to 1800. Touching the pin with a finger: the value drifts (floating input) — proof that the measurement is live.
Common Errors
- Injecting 3.3 V (or even 5 V) directly onto an AIN: this is the classic failure documented in the SRM ("AIN (1.8V MAX)"); above 1.8 V, damage occurs to the SoC. Always use a voltage divider.
- Forgetting to connect AGND (P9_34) to your circuit's ground: measurements fluctuate by several hundred codes.
- Wrong index in_voltageN_raw: the index depends on the channels enabled in the device tree; check in_voltageN_label before wiring a real signal.
- Assuming a 3.3 V ADC like on other boards: here the analog rail is 1.8 V, and the usable resolution is 12 bits over this range.
- Expecting metrological precision: noise of a few codes, averaging is necessary for stability (average over 16 readings).
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
- Bouton et LED avec Adafruit_BBIO, la bibliothèque historique
- 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