A. spidev has no open() function; create SpiDev object first
B. xfer2() requires a string, not a list
C. spi.close() should be called before xfer2()
D. Bus number 0 is invalid on Raspberry Pi
Solution
Step 1: Identify AttributeError cause
spidev module does not have open() function; open() is method of SpiDev object.
Step 2: Correct usage of spidev
Create spi = spidev.SpiDev() first, then call spi.open(0, 0) to fix error.
Final Answer:
spidev has no open() function; create SpiDev object first -> Option A
Quick Check:
Use SpiDev() object before open() [OK]
Hint: Always create SpiDev() object before open() [OK]
Common Mistakes:
Calling spidev.open() directly
Passing wrong data type to xfer2()
Closing SPI before transfer
5. You want to send a command byte 0xA5 followed by data bytes [0x01, 0x02, 0x03] to an SPI display using spidev. Which code correctly sends the command first, then the data, assuming spi is already opened?
hard
A. spi.writebytes([0xA5, 0x01, 0x02, 0x03])
B. spi.xfer2([0xA5, 0x01, 0x02, 0x03])
C. spi.xfer2([0xA5]); spi.xfer2([0x01, 0x02, 0x03])
D. spi.writebytes([0xA5]); spi.writebytes([0x01, 0x02, 0x03])
Solution
Step 1: Understand command and data separation
Many SPI displays require sending command and data separately to distinguish them.
Step 2: Check code options
spi.xfer2([0xA5]); spi.xfer2([0x01, 0x02, 0x03]) sends command byte first, then data bytes in separate calls, matching typical display protocol.
Final Answer:
spi.xfer2([0xA5]); spi.xfer2([0x01, 0x02, 0x03]) -> Option C
Quick Check:
Separate command and data transfers [OK]
Hint: Send command and data in separate SPI transfers [OK]
Common Mistakes:
Sending command and data in one transfer
Using writebytes() which may not return response
Not separating command from data
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.