Bird
0
0

Identify the error in the following code snippet that writes to an I2C device and suggest the fix:

medium📝 Debug Q14 of 15
Raspberry Pi - I2C Communication
Identify the error in the following code snippet that writes to an I2C device and suggest the fix:
from smbus2 import SMBus
bus = SMBus(1)
bus.write_byte_data(0x40, 0x02)
bus.close()
AWrong device address format; use string instead of integer
BMissing the data byte argument in write_byte_data; add the data byte
CIncorrect bus number; change SMBus(1) to SMBus(0)
Dbus.close() should be called before write_byte_data
Step-by-Step Solution
Solution:
  1. Step 1: Check write_byte_data parameters

    The function requires three arguments: device address, register, and data byte.
  2. Step 2: Identify missing argument

    The code only passes two arguments, missing the data byte to write.
  3. Final Answer:

    Missing the data byte argument in write_byte_data; add the data byte -> Option B
  4. Quick Check:

    write_byte_data needs 3 args [OK]
Quick Trick: write_byte_data always needs 3 arguments [OK]
Common Mistakes:
MISTAKES
  • Forgetting the data byte argument
  • Assuming bus.close() order matters here
  • Changing bus number without reason

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Raspberry Pi Quizzes