Multiple I2C devices on same bus in Raspberry Pi - Deep Dive
┌───────────────┐
│ Raspberry Pi │
│ (Master) │
└──────┬────────┘
│ SDA, SCL (shared wires)
│
┌──────┴───────┬───────┬───────┐
│ Device 1 │Device 2│Device 3│
│ Address 0x20 │0x30 │0x40 │
└──────────────┴────────┴────────┘┌───────────────┐
│ Master │
│ (Raspberry Pi)│
└──────┬────────┘
│
│ SDA, SCL (open-drain lines with pull-ups)
│
┌──────┴─────────────┐
│ Multiple Devices │
│ (each with address) │
│ ┌─────┐ ┌─────┐ │
│ │Dev1 │ │Dev2 │ ...│
│ └─────┘ └─────┘ │
└────────────────────┘Practice
What is the main requirement to connect multiple I2C devices on the same bus?
Choose the best answer.
Solution
Step 1: Understand I2C bus addressing
I2C devices share the same two wires but must have unique addresses to avoid conflicts.Step 2: Identify the correct requirement
Since devices share the bus, unique addresses allow the master to select each device individually.Final Answer:
Each device must have a unique I2C address. -> Option AQuick Check:
Unique addresses = multiple devices on one bus [OK]
- Thinking devices need separate wires
- Assuming devices can share the same address
- Believing each device needs its own bus
Which Python code snippet correctly initializes communication with two I2C devices at addresses 0x20 and 0x21 on the same bus?
import smbus2 bus = smbus2.SMBus(1) # What comes next?
Solution
Step 1: Understand bus and device addressing
The bus object is created once; devices are identified by their addresses.Step 2: Assign device addresses as variables
Assigning device addresses to variables allows easy communication calls later.Final Answer:
device1 = 0x20\ndevice2 = 0x21 -> Option CQuick Check:
Bus once, devices by address [OK]
- Trying to create multiple bus objects for each device
- Calling read_byte immediately without setup
- Assigning bus object directly to devices
Given this Python code snippet on a Raspberry Pi with two I2C devices at addresses 0x20 and 0x21:
import smbus2 bus = smbus2.SMBus(1) value1 = bus.read_byte(0x20) value2 = bus.read_byte(0x21) print(value1, value2)
What will be the output if device 0x20 returns 10 and device 0x21 returns 20?
Solution
Step 1: Read bytes from each device address
The code reads one byte from address 0x20 and one from 0x21.Step 2: Print the values in order
Since value1 reads from 0x20 (returns 10) and value2 from 0x21 (returns 20), print outputs "10 20".Final Answer:
10 20 -> Option AQuick Check:
Read from 0x20 then 0x21 = 10 20 [OK]
- Mixing up device addresses and values
- Assuming output is reversed
- Expecting error without checking device presence
Consider this Python code snippet intended to read from two I2C devices on the same bus:
import smbus2 bus = smbus2.SMBus(1) value1 = bus.read_byte(0x20) value2 = bus.read_byte(0x20) print(value1, value2)
Why might this code cause a problem when trying to read two different devices?
Solution
Step 1: Check device addresses used in reads
Both read_byte calls use address 0x20, so only one device is accessed twice.Step 2: Understand impact on multiple devices
To read two different devices, their unique addresses must be used; otherwise, the second device is ignored.Final Answer:
Both reads use the same address, so the second device is never accessed. -> Option BQuick Check:
Same address twice = one device only [OK]
- Assuming bus object creation causes error
- Thinking addresses 0x20 are invalid
- Confusing hardware wiring issues with code errors
You have three I2C sensors connected to the same Raspberry Pi bus with addresses 0x20, 0x21, and 0x20. You want to read data from all three devices. What is the best way to fix this setup?
Solution
Step 1: Identify address conflict
Two devices share address 0x20, causing bus conflicts and communication errors.Step 2: Resolve conflict by changing address
Most I2C devices allow changing their address via hardware pins or software configuration to ensure uniqueness.Step 3: Alternative solutions
Using separate buses or GPIO pins is complex; ignoring conflicts causes errors.Final Answer:
Change the address of one device so all three have unique addresses. -> Option DQuick Check:
Unique addresses fix conflicts [OK]
- Trying to read devices with duplicate addresses
- Assuming software can fix hardware address conflicts
- Using separate GPIO pins without separate buses
Ready to prove it?
Pass the test and this pattern moves unknown
