capteur-i2c.sh

Radxa ZERO 3W · 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

A Radxa Zero 3W (Rockchip RK3566 quad Cortex-A55, 1-4 GB) in a stick form factor, with Wi-Fi 5, Bluetooth and a 40-pin header. 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.

Running it

On the Radxa image: apt install gpiod python3-gpiod; gpiodetect/gpioinfo to identify the GPIO chip of the 40-pin header, then run the script.

Variants

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