Bird
Raised Fist0
Raspberry Piprogramming~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1.

What is the main requirement to connect multiple I2C devices on the same bus?

Choose the best answer.

easy
A. Each device must have a unique I2C address.
B. Each device must use a different bus.
C. Devices must be connected with separate wires.
D. Devices must have the same address to communicate.

Solution

  1. Step 1: Understand I2C bus addressing

    I2C devices share the same two wires but must have unique addresses to avoid conflicts.
  2. Step 2: Identify the correct requirement

    Since devices share the bus, unique addresses allow the master to select each device individually.
  3. Final Answer:

    Each device must have a unique I2C address. -> Option A
  4. Quick Check:

    Unique addresses = multiple devices on one bus [OK]
Hint: Remember: unique addresses prevent device conflicts on I2C [OK]
Common Mistakes:
  • Thinking devices need separate wires
  • Assuming devices can share the same address
  • Believing each device needs its own bus
2.

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?
easy
A. device1 = smbus2.SMBus(0x20) device2 = smbus2.SMBus(0x21)
B. device1 = bus.read_byte(0x20) device2 = bus.read_byte(0x21)
C. device1 = 0x20 device2 = 0x21
D. device1 = bus device2 = bus

Solution

  1. Step 1: Understand bus and device addressing

    The bus object is created once; devices are identified by their addresses.
  2. Step 2: Assign device addresses as variables

    Assigning device addresses to variables allows easy communication calls later.
  3. Final Answer:

    device1 = 0x20\ndevice2 = 0x21 -> Option C
  4. Quick Check:

    Bus once, devices by address [OK]
Hint: Create one bus object; use addresses to identify devices [OK]
Common Mistakes:
  • Trying to create multiple bus objects for each device
  • Calling read_byte immediately without setup
  • Assigning bus object directly to devices
3.

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?

medium
A. 10 20
B. 20 10
C. 0 0
D. Error: Address not found

Solution

  1. Step 1: Read bytes from each device address

    The code reads one byte from address 0x20 and one from 0x21.
  2. Step 2: Print the values in order

    Since value1 reads from 0x20 (returns 10) and value2 from 0x21 (returns 20), print outputs "10 20".
  3. Final Answer:

    10 20 -> Option A
  4. Quick Check:

    Read from 0x20 then 0x21 = 10 20 [OK]
Hint: Read bytes by address order matches output order [OK]
Common Mistakes:
  • Mixing up device addresses and values
  • Assuming output is reversed
  • Expecting error without checking device presence
4.

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?

medium
A. The code is missing pull-up resistors on the bus.
B. Both reads use the same address, so the second device is never accessed.
C. The addresses 0x20 and 0x20 are invalid on the bus.
D. The bus object is created twice, causing conflict.

Solution

  1. Step 1: Check device addresses used in reads

    Both read_byte calls use address 0x20, so only one device is accessed twice.
  2. Step 2: Understand impact on multiple devices

    To read two different devices, their unique addresses must be used; otherwise, the second device is ignored.
  3. Final Answer:

    Both reads use the same address, so the second device is never accessed. -> Option B
  4. Quick Check:

    Same address twice = one device only [OK]
Hint: Use different addresses to access multiple devices [OK]
Common Mistakes:
  • Assuming bus object creation causes error
  • Thinking addresses 0x20 are invalid
  • Confusing hardware wiring issues with code errors
5.

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?

hard
A. Use separate I2C buses for devices with the same address.
B. Use software to ignore address conflicts and read anyway.
C. Connect devices with the same address to different GPIO pins.
D. Change the address of one device so all three have unique addresses.

Solution

  1. Step 1: Identify address conflict

    Two devices share address 0x20, causing bus conflicts and communication errors.
  2. Step 2: Resolve conflict by changing address

    Most I2C devices allow changing their address via hardware pins or software configuration to ensure uniqueness.
  3. Step 3: Alternative solutions

    Using separate buses or GPIO pins is complex; ignoring conflicts causes errors.
  4. Final Answer:

    Change the address of one device so all three have unique addresses. -> Option D
  5. Quick Check:

    Unique addresses fix conflicts [OK]
Hint: Ensure all devices have unique addresses to avoid conflicts [OK]
Common Mistakes:
  • Trying to read devices with duplicate addresses
  • Assuming software can fix hardware address conflicts
  • Using separate GPIO pins without separate buses
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard