Bird
0
0
Raspberry Piprogramming~10 mins

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

Choose your learning style9 modes available
Concept Flow - Multiple I2C devices on same bus
Start I2C Bus
Send START Condition
Send Device Address
Address matches Device 1?
YesCommunicate with Device 1
Send STOP
End
Address matches Device 2?
YesCommunicate with Device 2
Send STOP
End
Address matches Device N?
YesCommunicate 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.