KevsRobots Learning Platform
21% Percent Complete
By Kevin McAleer, 7 Minutes
I2C (Inter-Integrated Circuit, pronounced “I-squared-C” or “I-two-C”) is a communication protocol that allows your Raspberry Pi Pico to talk to multiple devices using just two wires. It’s incredibly popular in electronics because it’s simple, reliable, and efficient.
Think of I2C like a conference call where everyone shares the same phone line. One person (the “controller” or “master”) asks questions, and the others (the “peripherals” or “slaves”) respond when called by their unique phone number (address).
I2C uses only two wires for all communication:
This is much simpler than other protocols that might need 4, 6, or even more wires. On the Raspberry Pi Pico, you’ll typically use:
Note: The Pico has two I2C buses (I2C0 and I2C1), giving you flexibility in pin selection. We’ll use I2C0 in this course.
In any I2C system, there’s always:
The controller is always in charge. It decides when to communicate and with which peripheral.
Each I2C device on the bus has a unique 7-bit address (like a house number on a street). Common addresses look like:
0x77 - BME280 sensor (hexadecimal notation)0x3C - OLED display0x68 - MPU6050 motion sensorImportant: Two devices cannot share the same address on the same bus. Some devices let you change their address using jumpers or solder pads.
Let’s see I2C in action by initializing it on your Pico:
from machine import I2C, Pin
# Create an I2C object on bus 0
# SDA on GP8, SCL on GP9, running at 400kHz
i2c = I2C(0, scl=Pin(9), sda=Pin(8), freq=400000)
print("I2C initialized successfully")
print(f"I2C configuration: {i2c}")
What this code does:
machine moduleWhy this matters:
This simple initialization unlocks communication with hundreds of different sensors and devices. Once I2C is set up, you can read temperature sensors, control LED matrices, communicate with real-time clocks, and much more - all through the same two wires.
I2C supports different communication speeds:
| Mode | Speed | Use Case |
|---|---|---|
| Standard | 100 kHz | Basic sensors, simple devices |
| Fast | 400 kHz | Most common - good balance of speed and reliability |
| Fast Plus | 1 MHz | High-speed applications |
| High Speed | 3.4 MHz | Rare, specialized use |
For most projects with sensors like the BME280, use Fast mode (400 kHz). It’s reliable and supported by virtually all I2C devices.
# Standard mode - slower but maximum compatibility
i2c = I2C(0, scl=Pin(9), sda=Pin(8), freq=100000)
# Fast mode - best for most projects
i2c = I2C(0, scl=Pin(9), sda=Pin(8), freq=400000)
Simple Wiring
Multiple Devices
Standardized
Built-in Addressing
Speed
Distance
Pull-up Resistors Required
I2C is everywhere in electronics. Here are common devices you’ll encounter:
Sensors
Displays
Expansion
You’ll encounter these terms when working with I2C:
Note: Modern documentation uses “Controller” and “Peripheral” instead of “Master” and “Slave”, though you’ll still see the old terminology in older datasheets.
Let’s experiment with I2C initialization:
freq parameter and see what your Pico accepts:from machine import I2C, Pin
# Try these different frequencies
for speed in [100000, 400000, 1000000]:
i2c = I2C(0, scl=Pin(9), sda=Pin(8), freq=speed)
print(f"I2C running at {speed} Hz")
# I2C on alternate pins
i2c = I2C(0, scl=Pin(5), sda=Pin(4), freq=400000)
print("I2C on alternate pins")
# I2C1 instead of I2C0
i2c1 = I2C(1, scl=Pin(15), sda=Pin(14), freq=400000)
print("Using second I2C bus")
Problem: Code runs but no devices respond Solution: Check that you’ve connected SDA and SCL to the correct pins Why: I2C won’t work if the pins don’t match your code
Problem: “I2C bus error” message Solution: Make sure you have pull-up resistors on SDA and SCL (covered in next lesson) Why: I2C requires these resistors to function properly
Problem: Devices work individually but not together Solution: Check that no two devices share the same address Why: Address conflicts prevent proper communication
In this lesson, you discovered:
Now that you understand what I2C is, it’s time to wire up a real sensor. In the next lesson, we’ll connect a BME280 sensor to your Pico, covering proper wiring, pull-up resistors, and the physical connections that make I2C work.
You can use the arrows ← → on your keyboard to navigate between lessons.
Comments