Bird
0
0

How can you modify this code to continuously read sensor data every second and print it?

hard🚀 Application Q9 of 15
Raspberry Pi - I2C Communication
How can you modify this code to continuously read sensor data every second and print it?
import smbus
import time
bus = smbus.SMBus(1)
address = 0x48
value = bus.read_byte_data(address, 0x00)
print(value)
APut reading and print inside a while True loop with time.sleep(1)
BUse a for loop with range(1) and no sleep
CCall read_byte_data once and print multiple times
DAdd time.sleep(1) before reading once
Step-by-Step Solution
Solution:
  1. Step 1: Use a loop to repeat reading

    while True loop allows continuous execution.
  2. Step 2: Add delay between reads

    time.sleep(1) pauses for 1 second between readings.
  3. Final Answer:

    Put reading and print inside a while True loop with time.sleep(1) -> Option A
  4. Quick Check:

    Loop + sleep for continuous periodic reading [OK]
Quick Trick: Use while True and sleep(1) for repeated reads [OK]
Common Mistakes:
MISTAKES
  • Using for loop with one iteration
  • Not reading sensor each time
  • Sleeping only once before loop

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Raspberry Pi Quizzes