Bird
0
0

You want to read 4 bytes from an SPI sensor repeatedly every second. Which code snippet correctly implements this using spidev?

hard🚀 Application Q8 of 15
Raspberry Pi - SPI Communication
You want to read 4 bytes from an SPI sensor repeatedly every second. Which code snippet correctly implements this using spidev?
Aimport spidev spi = spidev.SpiDev() spi.open(0, 0) spi.write([0, 0, 0, 0])
Bimport spidev spi = spidev.SpiDev() spi.open(0, 0) for i in range(4): print(spi.read())
Cimport spidev spi = spidev.SpiDev() spi.open(0, 0) data = spi.xfer2([1, 2, 3, 4]) print(data)
Dimport spidev import time spi = spidev.SpiDev() spi.open(0, 0) while True: data = spi.xfer2([0, 0, 0, 0]) print(data) time.sleep(1)
Step-by-Step Solution
Solution:
  1. Step 1: Identify continuous reading with delay

    import spidev import time spi = spidev.SpiDev() spi.open(0, 0) while True: data = spi.xfer2([0, 0, 0, 0]) print(data) time.sleep(1) uses a while loop with time.sleep(1) to read 4 bytes repeatedly every second.
  2. Step 2: Verify correct SPI transfer method

    import spidev import time spi = spidev.SpiDev() spi.open(0, 0) while True: data = spi.xfer2([0, 0, 0, 0]) print(data) time.sleep(1) uses spi.xfer2() with 4 zero bytes to read data, which is correct. Other options use invalid methods or do not loop.
  3. Final Answer:

    The code with the while True loop, spi.xfer2([0, 0, 0, 0]), and time.sleep(1) -> Option D
  4. Quick Check:

    Use while loop + spi.xfer2() + sleep for repeated reads [OK]
Quick Trick: Use while loop with spi.xfer2() and time.sleep() for repeated reads [OK]
Common Mistakes:
MISTAKES
  • Using spi.read() which does not exist
  • Not looping for repeated reads
  • Using spi.write() instead of xfer2()

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Raspberry Pi Quizzes