I2C is used with Raspberry Pi to easily connect and communicate with many small devices using just two wires.
0
0
Why I2C is used with Raspberry Pi
Introduction
When you want to connect sensors like temperature or light sensors to your Raspberry Pi.
When you need to control small displays or LCD screens with few wires.
When you want to connect multiple devices without using many pins on the Raspberry Pi.
When you want a simple way to send and receive data between Raspberry Pi and other chips.
Syntax
Raspberry Pi
I2C uses two wires: - SDA (data line) - SCL (clock line) Devices have addresses to talk to each other on the same bus.
The Raspberry Pi has built-in support for I2C on specific pins.
Each device on the I2C bus has a unique address to avoid confusion.
Examples
This shows how to turn on I2C support on your Raspberry Pi.
Raspberry Pi
# Example: Enable I2C on Raspberry Pi sudo raspi-config # Then select Interface Options > I2C > Enable
This code scans the I2C bus for connected devices and prints their addresses.
Raspberry Pi
# Example: Python code to scan I2C devices import smbus2 bus = smbus2.SMBus(1) for address in range(0x03, 0x78): try: bus.read_byte(address) print(f"Device found at address: {hex(address)}") except OSError: pass
Sample Program
This program tries to read a byte from a device connected via I2C and prints the result or an error.
Raspberry Pi
import smbus2 import time bus = smbus2.SMBus(1) # Use I2C bus 1 on Raspberry Pi DEVICE_ADDRESS = 0x48 # Example device address try: # Read a byte from the device data = bus.read_byte(DEVICE_ADDRESS) print(f"Data read from device at address {hex(DEVICE_ADDRESS)}: {data}") except Exception as e: print(f"Failed to read from device: {e}")
OutputSuccess
Important Notes
Make sure I2C is enabled on your Raspberry Pi before running I2C programs.
Devices must have unique addresses on the I2C bus to avoid conflicts.
Use pull-up resistors on SDA and SCL lines if your devices do not have them built-in.
Summary
I2C lets Raspberry Pi talk to many small devices using only two wires.
It saves pins and wiring complexity when connecting sensors or displays.
Each device has a unique address so Raspberry Pi knows who to talk to.
