Bird
Raised Fist0
Raspberry Piprogramming~10 mins

Multiple I2C devices on same bus in Raspberry Pi - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Multiple I2C devices on same bus
Start I2C Bus
↓
Send START Condition
↓
Send Device Address
↓
Address matches Device 1?
Yes→Communicate with Device 1
↓
Send STOP
↓
End
↓
Address matches Device 2?
Yes→Communicate with Device 2
↓
Send STOP
↓
End
↓
Address matches Device N?
Yes→Communicate with Device N
↓
Send STOP
↓
End
The I2C bus starts and sends a start signal, then sends a device address. The device with matching address responds and communication happens. After communication, a stop signal ends the session. This repeats for each device on the same bus.
Execution Sample
Raspberry Pi
import smbus
bus = smbus.SMBus(1)
addr1 = 0x20
addr2 = 0x21
bus.write_byte(addr1, 0xFF)
data = bus.read_byte(addr2)
This code writes a byte to device at address 0x20 and reads a byte from device at address 0x21 on the same I2C bus.
Execution Table
StepActionDevice AddressBus StateResult
1Send START condition-START sentBus ready for address
2Send address 0x200x20Address sentDevice 0x20 ACKs
3Write byte 0xFF0x20Data sentDevice 0x20 receives data
4Send STOP condition-STOP sentCommunication with 0x20 ends
5Send START condition-START sentBus ready for address
6Send address 0x210x21Address sentDevice 0x21 ACKs
7Read byte0x21Data requestedDevice 0x21 sends data
8Send STOP condition-STOP sentCommunication with 0x21 ends
9End--All devices communicated successfully
💡 All devices on the bus have been communicated with; bus is free.
Variable Tracker
VariableStartAfter Step 3After Step 7Final
busInitializedOpen for writeOpen for readOpen (bus remains open; smbus does not explicitly close)
addr10x20Used for writeNo changeNo change
addr20x21No changeUsed for readNo change
dataNoneNoneByte read from 0x21Byte stored
Key Moments - 3 Insights
Why do we send a STOP condition after communicating with each device?
The STOP condition signals the end of communication with one device, freeing the bus for the next device. See execution_table rows 4 and 8 where STOP ends communication.
How does the bus know which device to talk to?
Each device has a unique address. The master sends the address, and only the matching device responds. See execution_table rows 2 and 6 where addresses 0x20 and 0x21 are sent.
Can two devices have the same address on the same bus?
No, because the bus would not know which device to communicate with. Each device must have a unique address to avoid conflicts.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what happens at Step 6?
ASend address 0x21 and device 0x21 acknowledges
BSend STOP condition to end communication
CWrite byte 0xFF to device 0x20
DSend START condition to begin communication
💡 Hint
Check execution_table row 6 under Action and Result columns.
At which step does the communication with device 0x20 end?
AStep 7
BStep 3
CStep 4
DStep 8
💡 Hint
Look for the STOP condition after device 0x20 communication in execution_table.
If device 0x21 did not acknowledge its address, what would change in the execution_table?
AStep 7 would still read data successfully
BStep 6 result would show no ACK and communication would stop
CStep 2 would show no ACK instead
DSTOP condition would be sent before Step 6
💡 Hint
Focus on the address acknowledgment step for device 0x21 in execution_table row 6.
Concept Snapshot
Multiple I2C devices share the same bus by having unique addresses.
Master sends START, then device address.
Only the matching device responds (ACK).
Data is sent or received.
STOP ends communication with that device.
Repeat for other devices on the bus.
Full Transcript
This visual execution shows how multiple I2C devices communicate on the same bus. The bus starts with a START condition, then the master sends a device address. The device with that address responds by acknowledging. The master then writes or reads data to/from that device. After communication, a STOP condition signals the end. This process repeats for each device with a unique address on the bus. Variables like bus state and device addresses change step by step. Key points include the importance of unique addresses and sending STOP after each device communication. The quiz tests understanding of these steps.

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