capteur-i2c.sh

BeagleBone AI-64 (TI TDA4VM / J721E) · File : capteur-i2c.sh · Language : Bash · Use case : Détecter et lire un capteur sur le bus I2C en ligne de commande

The Linux reflex for any I2C sensor: scan the bus with i2cdetect, confirm the chip is there, then read a few registers with i2cget. Direct terminal output without writing a line of application code.

Hardware required

A BeagleBone AI-64 (TI J721E, 2 GHz) on the official Debian image, powered by 5 V/4 A USB-C, with its two 3.3 V P8/P9 cape headers. 1 I2C sensor (BMP280, MPU6050, SSD1306...) — power it at 3.3 V.

Wiring

SDA/SCL to the connector I2C pins (locate them in the board docs), VCC 3.3 V, GND. Enable the bus in the firmware/device tree.

How it works

i2cdetect -y probes every address and prints the answers; i2cget reads a register. The script checks the address before reading. On Debian: sudo apt install gpiod python3-gpiod; gpiodetect lists the chips (main one is 600000.gpio). The P8/P9 pins are named in gpioinfo.

Going further

Chain into a Python conversion (py-smbus), or script periodic reads in a cron job to log pressure/temperature.

capteur-i2c.sh

#!/bin/bash
# Detection et lecture d'un capteur I2C (ex. BMP280 en 0x76) avec i2c-tools.
# Installation : sudo apt install i2c-tools
# Activer le bus I2C (raspi-config / device tree overlay / dtb selon la carte).

set -u
BUS="${1:-1}"   # numero de bus (sortie de i2cdetect -l)

echo "== Bus I2C disponibles =="
i2cdetect -l || { echo "noyau sans I2C ? activer le bus"; exit 1; }

echo
echo "== Balayage du bus ${BUS} =="
i2cdetect -y "${BUS}" || exit 1

ADRESSE="${2:-0x76}"
if i2cdetect -y "${BUS}" | grep -q "${ADRESSE#0x}"; then
  echo
  echo "== Lecture de ${ADRESSE} (16 octets) =="
  i2cget -f -y "${BUS}" "${ADRESSE}" 0x00    # ID de puce (0x58/0x5D BMP280)
  i2cget -y "${BUS}" "${ADRESSE}" 0xfa       # LSB pression (registre brut)
  i2cget -y "${BUS}" "${ADRESSE}" 0xfb
  i2cget -y "${BUS}" "${ADRESSE}" 0xfc
  echo "(brut a convertir avec les registres de calibration de la doc)"
else
  echo "Aucune puce a ${ADRESSE} : verifier cable/alimentation/adresse"
  exit 2
fi

← Back to the board