Bird
0
0

Which of the following is the correct way to open an I2C bus safely using smbus2 in Python?

easy📝 Syntax Q12 of 15
Raspberry Pi - I2C Communication
Which of the following is the correct way to open an I2C bus safely using smbus2 in Python?
Aopen(SMBus(1)) # use bus
Bbus = SMBus(1) # use bus bus.close()
Cbus = SMBus() # use bus
Dwith SMBus(1) as bus: # use bus
Step-by-Step Solution
Solution:
  1. Step 1: Identify the recommended safe usage

    The recommended way is to use a context manager: with SMBus(1) as bus: which automatically opens and closes the bus safely.
  2. Step 2: Check other options for correctness

    bus = SMBus(1) # use bus bus.close() manually opens and closes but is less safe. bus = SMBus() # use bus misses the bus number. open(SMBus(1)) # use bus uses invalid syntax.
  3. Final Answer:

    with SMBus(1) as bus: # use bus -> Option D
  4. Quick Check:

    Use 'with' to open SMBus safely [OK]
Quick Trick: Use 'with SMBus(bus_number) as bus:' for safe bus handling [OK]
Common Mistakes:
MISTAKES
  • Not specifying the bus number when creating SMBus
  • Forgetting to close the bus after use
  • Using invalid syntax like open() with SMBus

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Raspberry Pi Quizzes