🛠 Lire un capteur I2C (température/humidité) sur le bus 1
Objectives
Enable the I2C bus on the BCM2835 (only one hardware bus is exposed on P1: I2C0 on GPIO0/1), scan the bus with i2cdetect, and read a digital sensor in Python. Early revisions do not have wired I2C pull-up resistors — a topic discussed on official forums — so we add external 4.7 kΩ pull-ups for safety.
Hardware
- Raspberry Pi Model B Rev 1, legacy 32-bit image (Buster)
- 3.3 V I2C sensor (e.g., BMP180, address 0x77)
- 2 pull-up resistors 4.7 kΩ to 3.3 V (SDA and SCL)
sudo apt install -y i2c-tools python3-smbus- Reminders: GPIO0 = pin 3 (SDA0), GPIO1 = pin 5 (SCL0), 3.3 V = pin 1, GND = pin 6.
Complete Code
# 1) Activer I2C : sudo raspi-config > Interfacing > I2C
# ou manuellement dans /boot/config.txt :
# dtparam=i2c_arm=on
sudo reboot
# 2) Scanner le bus (le capteur doit apparaître, ici 0x77)
sudo i2cdetect -y 1
#!/usr/bin/env python3
"""Lecture brute d'un registre capteur via SMBus (adaptez l'adresse)."""
import smbus
BUS = smbus.SMBus(1) # bus I2C1 cote linux (peripherique i2c-bcm2708)
ADDR = 0x77 # sortie de i2cdetect -y 1
val = BUS.read_byte_data(ADDR, 0x10) # exemple de registre
print("registre 0x10 =", hex(val))
Expected Verification
i2cdetect -y 1 displays a grid with the sensor's address (never 00, nor reserved addresses). The Python script prints a non-zero value that changes if you warm the sensor between your fingers.
Common Errors
- Forgetting
dtparam=i2c_arm=onand rebooting:/dev/i2c-1does not exist. - Bus 0 vs bus 1: on the BCM2835, the bus exposed on P1 is bus 1 from Linux's perspective;
i2cdetect -y 0often returns empty. - 5 V sensor on a 3.3 V bus: choose a 3.3 V module or a level shifter.
- Missing pull-ups: the bus remains stuck in the low state,
i2cdetectdisplays nothing orUU. Permission deniedon/dev/i2c-1: add the user to thei2cgroup or run with sudo.
Other tutorials for this board
- Allumer une LED via le GPIO avec libgpiod
- Lire un bouton poussoir et afficher l'état dans la console
- Premier script Python : faire clignoter la LED
- Variation d'intensité LED avec le PWM matériel (et son piège)
- Configurer l'Ethernet 10/100 et piloter le Pi en SSH
- Boot sur SD, partitions et procédure de recouvrement
Code examples for this board
led_blink.pyPythonbutton_led.pyPythonservo_sweep.pyPythondht_temp.pyPythonoled_i2c.pyPythonmcp3008_read.pyPython