Bird
0
0

What will be the output of the following Python code snippet on Raspberry Pi when writing to an I2C device?

medium📝 Predict Output Q13 of 15
Raspberry Pi - I2C Communication
What will be the output of the following Python code snippet on Raspberry Pi when writing to an I2C device?
from smbus2 import SMBus
bus = SMBus(1)
try:
    bus.write_byte_data(0x50, 0x01, 0xFF)
    print("Write successful")
except Exception as e:
    print(f"Error: {e}")
bus.close()
AError: [Errno 121] Remote I/O error
BNo output
CSyntaxError
DWrite successful
Step-by-Step Solution
Solution:
  1. Step 1: Analyze the code flow

    The code opens I2C bus 1, writes byte 0xFF to register 0x01 at device 0x50, then prints success if no error.
  2. Step 2: Consider normal operation

    If the device is connected and address is correct, write succeeds and prints "Write successful".
  3. Final Answer:

    Write successful -> Option D
  4. Quick Check:

    Successful write prints message [OK]
Quick Trick: If no error, print shows success message [OK]
Common Mistakes:
MISTAKES
  • Assuming error without device connected
  • Confusing syntax error with runtime error
  • Ignoring bus.close() importance

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Raspberry Pi Quizzes