I2C Sensors on a Raspberry Pi: the short answer
The Pi's main I2C bus uses GPIO2 (SDA, physical pin 3) and GPIO3 (SCL, physical pin 5). 3 V on these pins, so most breakout boards work without extra resistors.
- Level
- Intermediate
- Time
- About 6 min
- Topic
- Raspberry Pi
Enable I2C, find a sensor on the bus and read a BME280 from Python inside a virtual environment.
Before this: I²C Addressing & Shared Buses, Raspberry Pi GPIO with Python
The Pi's main I2C bus uses GPIO2 (SDA, physical pin 3) and GPIO3 (SCL, physical pin 5). The Pi already has 1.8 kΩ pull-up resistors to 3.3 V on these pins, so most breakout boards work without extra resistors. I2C is switched off by default: turn it on with sudo raspi-config, Interface Options, I2C, then reboot.
Before writing any code, check the wiring with i2cdetect -y 1. It prints a grid of addresses, and your sensor should appear at its address, for example 76 or 77 for a BME280. If nothing appears, the problem is wiring or power, not Python. If the address is different from the one in your code, change the code or the board's address jumper.
Current Raspberry Pi OS does not let pip install packages into the system Python. Create a virtual environment once with python3 -m venv --system-site-packages ~/env, activate it with source ~/env/bin/activate, and install libraries there, for example pip install adafruit-circuitpython-bme280. The --system-site-packages option keeps access to the libraries that ship with the OS, such as gpiozero and picamera2. Run your scripts with ~/env/bin/python so services use the same environment.
Enable the bus and find the device
On Raspberry Pi OS, turn on I²C in raspi-config under Interface Options. Wire SDA and SCL to the Pi's pins 3 and 5 with 3.3 V power and a shared ground. Run i2cdetect -y 1 and you should see the sensor's address, such as 0x76 or 0x68, in the grid.
If nothing appears, check the wiring and power first, then whether the address pins on the module are set as you expect. The Pi already has pull-up resistors on those pins, so a module with its own is usually still fine.
Reading in Python
The smbus2 library reads and writes registers. A sensor's datasheet lists register addresses; you write the register you want, then read bytes back and combine them. Many sensors have ready-made libraries from the maker, which save you from working out calibration formulas by hand.
Read in a loop with a short sleep, and handle errors: a loose wire raises an OSError, and a program that crashes on it is no use as a logger. Catch it, wait and try again.
Reading reliably
Keep wires short, since long I²C leads pick up noise and slow the edges. Do not mix 5 V devices on the bus, because the Pi pins are 3.3 V only. Two devices with the same address need a multiplexer such as the TCA9548A, or a module that lets you change the address.
- Connecting a 5 V module's SDA and SCL straight to the Pi pins.
- Not enabling I²C in raspi-config and wondering why i2cdetect shows nothing.
- Letting a single read error crash a logging script.
Practise this lesson with Maker
The explanation above is free for everyone. The practice for Intermediate lessons in every domain are part of the Maker plan at ₹499 a month, and Pro includes them too.
With Maker you also get: a worked example, a hands-on task, a 3-question check.
