Introduction
As your robotic projects grow in complexity, you’ll inevitably hit a point where one microcontroller or computer isn’t enough. You might have a central “brain,” like a Raspberry Pi or NVIDIA Jetson, that needs to communicate with specialized sensor boards, motor drivers, or even supplementary microcontrollers like Arduinos.
This is where the I2C (Inter-Integrated Circuit) protocol becomes incredibly valuable. I2C allows multiple “slave” devices to communicate with one or more “master” devices on a shared bus using just two wires: SDA (Serial Data) and SCL (Serial Clock).
SMBus (System Management Bus) is a protocol derived from I2C that provides stricter timing rules and standardized commands. In the context of Raspberry Pi, the smbus (or smbus2) Python library is the standard way to interface with I2C devices.
In this article, we’ll explore how to use the smbus library to enable your Raspberry Pi to talk to other controllers and peripherals.
Enabling I2C on the Raspberry Pi
Before you can write any code, you need to ensure that the I2C interface is enabled on your Raspberry Pi:
- Open a terminal on your Pi and run
sudo raspi-config. - Navigate to Interfacing Options > I2C.
- Select Yes when asked if you want the ARM I2C interface to be enabled.
- Restart your Raspberry Pi.
You will also need the smbus Python library. You can usually install it via:
sudo apt-get install python3-smbus i2c-tools
Note: You can use i2cdetect -y 1 in the terminal to scan the bus and see the connected I2C addresses!
Using the smbus Library
The smbus library makes it remarkably simple to handle communication over the I2C wires. Let’s look at the basic operations: initializing the bus, writing data, and reading data.
1. Initializing the Bus
To start communicating, you first need to establish a connection to the correct I2C bus. On modern Raspberry Pi models (like Pi 3 and Pi 4), this is usually Bus 1.
import smbus
import time
# Initialize I2C Bus 1
bus = smbus.SMBus(1)
# Alternatively, if you are using the 'smbus2' library:
# from smbus2 import SMBus
# bus = SMBus(1)
2. Communicating with a Peripheral
Every I2C device has a unique address (a hexadecimal number, often appearing as something like 0x08 or 0x48).
Let’s assume you have an Arduino acting as an I2C slave controller at address 0x08. Let’s tell the Arduino to turn on an LED or start a motor by writing a specific byte to a specific register.
Sending Data (Write)
SLAVE_ADDRESS = 0x08
def send_command(command_byte):
try:
# Write a single byte to the device (no specific register needed)
bus.write_byte(SLAVE_ADDRESS, command_byte)
print(f"Sent command {hex(command_byte)} to device {hex(SLAVE_ADDRESS)}")
except IOError:
print("Error: Could not communicate with device. Is it connected properly?")
# Send command '1'
send_command(0x01)
For more complex devices, you typically write to a specific register on the device:
REGISTER_ADDRESS = 0x20
DATA_TO_SEND = 0xFF
# write_byte_data(Device Address, Register/Command Address, Parameter)
bus.write_byte_data(SLAVE_ADDRESS, REGISTER_ADDRESS, DATA_TO_SEND)
Receiving Data (Read)
If you need to read sensor data or status updates from the secondary controller, you can request a read:
def read_sensor_data(register):
try:
# Read a single byte from a specific register
data = bus.read_byte_data(SLAVE_ADDRESS, register)
return data
except IOError:
return None
SENSOR_REGISTER = 0x10
data = read_sensor_data(SENSOR_REGISTER)
if data is not None:
print(f"Sensor returned: {data}")
3. A Complete I2C Master Script
Here is a full example script for your Rasperry Pi “Brain”. This loop continuously requests data from an attached controller and periodically sends a “heartbeat” command back to let the slave know it’s still alive.
import smbus
import time
I2C_BUS = 1
DEVICE_ADDRESS = 0x08
HEARTBEAT_CMD = 0x99
STATUS_REG = 0x01
# Initialize the I2C bus
try:
bus = smbus.SMBus(I2C_BUS)
except Exception as e:
print(f"Failed to initialize I2C bus: {e}")
exit()
def send_heartbeat():
try:
bus.write_byte(DEVICE_ADDRESS, HEARTBEAT_CMD)
print("Heartbeat sent.")
except IOError:
print("Failed to send heartbeat.")
def read_status():
try:
status = bus.read_byte_data(DEVICE_ADDRESS, STATUS_REG)
print(f"Controller Status: {status}")
except IOError:
print("Failed to read status.")
if __name__ == "__main__":
print("Starting I2C Master Node...")
counter = 0
while True:
# Read status every loop
read_status()
# Send a heartbeat every 5 iterations
if counter % 5 == 0:
send_heartbeat()
counter += 1
time.sleep(1) # wait 1 second
Summary
The smbus library is an essential tool for any robotics pipeline. By mastering I2C communication, you can build distributed, modular robotic systems where a single Raspberry Pi or Jetson delegates precise tasks—like sensor reading or motor controlling—to dedicated microcontrollers via a simple two-wire interface.
In future articles, we’ll look at how to implement the corresponding “slave” code on a microcontroller, and how to adapt these scripts for the NVIDIA Jetson platform!