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?
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}")
Each device has a unique address, so reading from each returns its own data.
When multiple I2C devices share the same bus, each device is accessed by its unique address. Reading from address 0x20 returns data from device 1, and reading from 0x21 returns data from device 2. The output shows the values read from each device.
On a Raspberry Pi I2C bus, why is it important that each connected device has a unique address?
Think about how the Raspberry Pi knows which device to talk to.
Each I2C device must have a unique address so the master (Raspberry Pi) can select and communicate with the correct device. If two devices share the same address, the bus cannot distinguish between them, causing data collisions or communication errors.
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?
import smbus bus = smbus.SMBus(1) addr1 = 0x20 addr2 = 0x20 byte1 = bus.read_byte(addr1) byte2 = bus.read_byte(addr2) print(byte1, byte2)
Think about what happens when two devices respond to the same address.
If two devices share the same I2C address on the bus, the master cannot distinguish between them. This usually causes a communication failure, resulting in a 'Remote I/O error' when trying to read data.
Choose the code that correctly scans the I2C bus for connected devices and prints their addresses.
Scanning requires catching errors when no device responds.
Option B tries to read a byte from each possible address and catches OSError if no device responds. This is the correct way to scan the I2C bus. Other options either do not handle exceptions or use incorrect methods.
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?
Think about hardware solutions to address conflicts.
When two devices share the same fixed I2C address, the bus cannot distinguish them. Using an I2C multiplexer allows the Raspberry Pi to select which device is connected to the bus at a time, avoiding address conflicts and enabling communication with both devices.
