Bird
0
0
Raspberry Piprogramming~20 mins

Writing commands to I2C device in Raspberry Pi - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
I2C Command Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this I2C write command code?
Consider the following Python code snippet running on a Raspberry Pi to write a byte to an I2C device. What will be printed when this code runs successfully?
Raspberry Pi
import smbus2
bus = smbus2.SMBus(1)
address = 0x20
try:
    bus.write_byte(address, 0xFF)
    print("Write successful")
except Exception as e:
    print(f"Error: {e}")
ANo output
BError: [Errno 121] Remote I/O error
CWrite failed
DWrite successful
Attempts:
2 left
💡 Hint
The code tries to write a byte and prints a success message if no error occurs.
🧠 Conceptual
intermediate
1:30remaining
Which I2C address format is correct for smbus2 write commands?
When using smbus2 on Raspberry Pi to write to an I2C device, which address format should you use?
A8-bit address including the read/write bit, e.g., 0x40
B7-bit address without the read/write bit, e.g., 0x20
CDecimal address only, e.g., 32
DAny address format works as smbus2 auto-corrects
Attempts:
2 left
💡 Hint
I2C addresses are usually 7 bits; the library handles the read/write bit.
🔧 Debug
advanced
2:30remaining
Why does this I2C write command raise an error?
This code snippet raises an error when trying to write a byte to an I2C device. What is the most likely cause?
Raspberry Pi
import smbus2
bus = smbus2.SMBus(1)
address = 0x20
bus.write_byte_data(address, 0x01)
AAddress 0x20 is invalid for I2C devices
BUsing wrong bus number; should be SMBus(0)
CMissing the data byte argument in write_byte_data call
Dsmbus2 does not support write_byte_data method
Attempts:
2 left
💡 Hint
Check the method signature for write_byte_data.
📝 Syntax
advanced
2:00remaining
Which option correctly writes a block of bytes to an I2C device?
You want to write the bytes [0x01, 0x02, 0x03] starting at register 0x10 to an I2C device at address 0x20 using smbus2. Which code snippet is syntactically correct and will run without error?
Abus.write_i2c_block_data(0x20, 0x10, [0x01, 0x02, 0x03])
Bbus.write_block(0x20, 0x10, [0x01, 0x02, 0x03])
Cbus.write_i2c_block(0x20, 0x10, [0x01, 0x02, 0x03])
Dbus.write_block_data(0x20, 0x10, [0x01, 0x02, 0x03])
Attempts:
2 left
💡 Hint
Check the exact method name in smbus2 for block writes.
🚀 Application
expert
3:00remaining
What is the value of 'result' after this I2C write and read sequence?
Given the following code that writes a byte to register 0x05 and then reads a byte back from the same register, what is the value of 'result' if the device echoes the written byte?
Raspberry Pi
import smbus2
bus = smbus2.SMBus(1)
address = 0x20
bus.write_byte_data(address, 0x05, 0xAB)
result = bus.read_byte_data(address, 0x05)
print(result)
A171
B0
CError: Remote I/O error
DNone
Attempts:
2 left
💡 Hint
0xAB in decimal is 171. The device echoes the byte written.