🛠 Lire un capteur I²C (BME280) sur le bus i2c-1 du connecteur Pi-2
Objectives
On mainline PINE A64 images, the i2c-1 bus is enabled by default in the device tree (&i1c1 with the i2c1_pins pin group on PH2/PH3 of the Pi-2 connector, pull-ups included): an I²C sensor works without an overlay. You will wire a BME280 (temperature/pressure/humidity), detect it with i2cdetect, and read it in Python via smbus2 — while understanding the numbering: PH2 = (H−A)×32 + 2 = 226, PH3 = 227, if you need to replay the pinctrl configuration manually.
Hardware
- 1 × PINE A64/A64+ running Armbian mainline
- 1 × BME280 module (default address 0x76, 0x77 if CSB is high)
- Sensor power supply at 3.3 V (connector logic is 3.3 V)
sudo apt install i2c-tools python3-smbus2(packagepython3-smbusdepending on versions)
Complete code
# Lister les bus puis scanner le bus 1 (&i2c1 activé dans le DTS) :
i2cdetect -l
i2cdetect -r 1 # doit afficher 76 (ou 77)
#!/usr/bin/env python3
"""Lecture brute d'un BME280 sur i2c-1 (broches PH2/PH3 du connecteur Pi-2)."""
import smbus2, time
BUS = 1 # &i2c1 est activé dans sun50i-a64-pine64.dts
ADDR = 0x76 # 0x77 si la broche CSB est tirée à 1
bus = smbus2.SMBus(BUS)
assert bus.read_byte_data(ADDR, 0xD0) in (0x52, 0x53), "pas de BME280 (id?)"
bus.write_byte_data(ADDR, 0xF4, 0x27) # temp x1, press x1, mode normal
bus.write_byte_data(ADDR, 0xF5, 0x00) # veille 1 Hz
time.sleep(0.1)
print("octets bruts pression:", bus.read_i2c_block_data(ADDR, 0xF7, 6))
# Pour des unités lisibles, utiliser un driver (adafruit-blinka, bme280-i2c) ;
# les formules de compensation complètes sont dans la datasheet BME280.
Expected verification
i2cdetect -r 1 displays 76: the i2c-1 bus exists, the DTS pull-ups are doing their job, and the sensor responds. The Python script prints the 6 raw pressure bytes (registers 0xF7–0xFC) which change when you press the sensor with your finger. The identity test (0xD0 = 0x52) validates the link before any measurement — the assertion fails loudly if the wiring is incorrect.
Common errors
- Wiring to the "I2C" pins of a Raspberry Pi cheat sheet: the Pi-2 connector does not have the RPi pinout — on the PINE A64, I2C1 is on PH2/PH3 (offsets 226/227), and UART2 on the same connector is on PB0/PB1.
- Scanning the wrong bus: run
i2cdetect -lfirst; it is bus 1 that the device tree enables, not necessarily bus 0. - Supplying 5 V to a 3.3 V BME280 module: the connector logic is 3.3 V — power and communicate at 3.3 V.
- Address not found: check 0x77 (CSB pulled high), SDA/SCL inversion, and common ground; a module without proper pull-ups on a long breadboard will not respond.
- Expecting an ADC on the connector: the A64 has a TPADC/LRADC but it is not routed to the 40-pin connector — for analog signals, use an I²C ADC (PCF8591, ADS1115).
- In C SDK or Python, forgetting that the bus might be busy: a daemon (i2c-mux, kernel drivers) occupying the address causes
read_byte_datato fail without a clear message.
Other tutorials for this board
- Premier boot d'Armbian sur la PINE A64 (microSD, console série, rootfs)
- PINE A64 ou PINE A64+ ? Le pièg du réseau gigabit dès le premier regard
- Faire clignoter une LED avec libgpiod (numérotation GPIO du A64 : PB0 = 32)
- Benchmark Ethernet iperf3 : mesurer le piège 10/100 vs gigabit
- Un serveur maison qui consomme 2,5 W : Docker léger sur la PINE A64
- Construire sa propre image Armbian pour la PINE A64 (armbian/build)
Code examples for this board
clignotante.pyPythonbouton-gpio.pyPythoncapteur-i2c.shBashsurveillance-temperature.shBashcapteur-dht11.pyPythonservo-pwm-sysfs.shBash