Bird
0
0
Raspberry Piprogramming~20 mins

Multiple I2C devices on same bus in Raspberry Pi - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
I2C Bus Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output when reading from two I2C devices on the same bus?

Consider two I2C devices connected to the same Raspberry Pi I2C bus with addresses 0x20 and 0x21. The code reads one byte from each device. What will be printed?

Raspberry Pi
import smbus
bus = smbus.SMBus(1)

addr1 = 0x20
addr2 = 0x21

byte1 = bus.read_byte(addr1)
byte2 = bus.read_byte(addr2)

print(f"Device 1 returned: {byte1}")
print(f"Device 2 returned: {byte2}")
ADevice 1 returned: 20\nDevice 2 returned: 10
BRuntimeError: Remote I/O error
CDevice 1 returned: 10\nDevice 2 returned: 20
DDevice 1 returned: 0\nDevice 2 returned: 0
Attempts:
2 left
💡 Hint

Each device has a unique address, so reading from each returns its own data.

🧠 Conceptual
intermediate
1:30remaining
Why must each I2C device have a unique address on the same bus?

On a Raspberry Pi I2C bus, why is it important that each connected device has a unique address?

ATo prevent data collisions and ensure the master communicates with the correct device
BTo allow devices to share the same memory space
CTo enable devices to send data simultaneously without interference
DTo reduce the power consumption of the bus
Attempts:
2 left
💡 Hint

Think about how the Raspberry Pi knows which device to talk to.

🔧 Debug
advanced
2:00remaining
Identify the error when two devices share the same I2C address

The following code tries to read from two devices on the same I2C bus, but both devices have the address 0x20. What error will most likely occur?

Raspberry Pi
import smbus
bus = smbus.SMBus(1)

addr1 = 0x20
addr2 = 0x20

byte1 = bus.read_byte(addr1)
byte2 = bus.read_byte(addr2)

print(byte1, byte2)
ANo error, prints two bytes
BValueError: Invalid address
CTypeError: unsupported operand type(s)
DRuntimeError: Remote I/O error
Attempts:
2 left
💡 Hint

Think about what happens when two devices respond to the same address.

📝 Syntax
advanced
2:30remaining
Which code snippet correctly scans all devices on the I2C bus?

Choose the code that correctly scans the I2C bus for connected devices and prints their addresses.

A
for addr in range(0x03, 0x78):
    bus.read_byte(addr)
    print(f"Device at {addr}")
B
for addr in range(0x03, 0x78):
    try:
        bus.read_byte(addr)
        print(f"Device found at 0x{addr:02X}")
    except OSError:
        pass
C
for addr in range(0x03, 0x78):
    try:
        bus.write_byte(addr, 0)
        print(f"Found device at {addr}")
    except ValueError:
        continue
D
for addr in range(0x00, 0x7F):
    if bus.read_byte(addr):
        print(f"Device at {addr}")
Attempts:
2 left
💡 Hint

Scanning requires catching errors when no device responds.

🚀 Application
expert
3:00remaining
How to handle two I2C devices with the same fixed address on one bus?

You have two I2C sensors that both use the fixed address 0x40 and cannot be changed. You want to connect both to the same Raspberry Pi I2C bus. What is the best approach to read data from both devices?

AUse an I2C multiplexer to switch between devices, selecting one at a time
BConnect both devices directly and read data simultaneously from address 0x40
CChange the Raspberry Pi I2C bus speed to avoid address conflicts
DUse software to merge data from both devices at the same address
Attempts:
2 left
💡 Hint

Think about hardware solutions to address conflicts.