🛠 Premier capteur I2C sur GPIO0/GPIO1
Objectives
- Enable the I2C0 bus (GPIO0 = SDA pin 3, GPIO1 = SCL pin 5) via raspi-config or config.txt.
- Scan the bus with i2cdetect to find the sensor's address.
- Read a measurement (temperature/pressure) in Python.
Hardware
- Raspberry Pi Model A + common I2C sensor (BME280, BMP280, MPU6050...). >
- The module must operate at 3.3 V; the I2C bus requires pull-up resistors (often already present on the module).
Complete code
# 1) Activer I2C : sudo raspi-config > Interface I2C
# ou à la main dans /boot/config.txt :
echo "dtparam=i2c_arm=on" | sudo tee -a /boot/config.txt
echo "i2c-dev" | sudo tee /etc/modules-load.d/i2c.conf
sudo apt-get install -y i2c-tools python3-smbus && sudo reboot
# 2) Balayer le bus (bus 1)
sudo i2cdetect -y 1
#!/usr/bin/env python3
import smbus, time
bus = smbus.SMBus(1)
ADDR = 0x76 # adresse vue avec i2cdetect
WHO = bus.read_byte_data(ADDR, 0xD0)
print("chip id:", hex(WHO))
while True:
t = bus.read_word_data(ADDR, 0x22) # registre d'exemple
print("brut température:", t, flush=True)
time.sleep(1)
Expected verification
i2cdetect -y 1 displays the sensor's address (e.g., 76) in the grid; the Python script reads values that change when you warm the sensor with your finger. >
Common errors
- Forgetting to reboot after enabling I2C:
/dev/i2c-1does not exist, "Permission denied". - Swapping SDA/SCL: nothing appears in i2cdetect, phantom address 0x00.
- Powering the module with 5 V while the bus is read at 3.3 V: risk of damaging the pins.
- Wrong address (0x77 instead of 0x76): the module's ADDR selector was not checked against the datasheet.
Other tutorials for this board
Code examples for this board
led_blink.pyPythonbutton_led.pyPythonservo_sweep.pyPythondht_temp.pyPythonoled_i2c.pyPythonmcp3008_read.pyPython