ABC of Electronics cosycom.com
RASPBERRY PI 13 · Communication

I2C on the Raspberry Pi: Talking to a Sensor

Enable and use I2C on the Pi to read a real sensor — the same two-wire bus from Lesson 33, now with Linux's own tools for finding devices on it.

You'll need: an I2C sensor breakout board (many temperature, humidity, and accelerometer modules use I2C — check the specific board's documentation for its address), jumper wires, and your Pi.

I2C isn't enabled by default on Raspberry Pi OS. Enable it once with sudo raspi-config, navigating to Interface Options → I2C → Enable, then reboot. Wire your sensor's SDA and SCL pins to the Pi's dedicated I2C pins (physical pins 3 and 5), plus power and ground.

terminal
sudo apt install i2c-tools -y
i2cdetect -y 1

i2cdetect -y 1 scans the I2C bus and prints a grid showing which addresses have a device responding — exactly the address-matching concept from Lesson 33, made visible. A number appearing in that grid confirms your sensor is wired correctly and responding, before you've written a single line of Python; a completely empty grid points straight back to a wiring or enable-I2C issue, not your code.

Once confirmed, reading the sensor in Python typically uses either a library specific to that sensor model (check its documentation first — most popular I2C sensors have one) or the general-purpose smbus2 library for sensors without a dedicated one. Either way, the address you saw in i2cdetect's output is what you'll reference in code.

Pi (SDA/SCL) I2C sensor $ i2cdetect -y 1 -- 0x48 -- ...abcofelectronics
i2cdetect scans the bus and reports which address responds — confirming wiring before any Python code is involved.
TRY THIS
Run i2cdetect -y 1 before writing any code — it confirms your I2C wiring and the sensor's address independently of your script, isolating hardware problems from code problems.