Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize SPI communication on Raspberry Pi.
Raspberry Pi
import spidev spi = spidev.SpiDev() spi.open(0, [1])
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using device number instead of bus number
Confusing bus and device numbers
✗ Incorrect
The SPI device on Raspberry Pi is usually opened with bus 0 and device 0.
2fill in blank
mediumComplete the code to set SPI speed to 1 MHz.
Raspberry Pi
spi.max_speed_hz = [1] Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 100000 instead of 1000000
Confusing kHz and MHz
✗ Incorrect
Setting max_speed_hz to 1000000 sets SPI speed to 1 MHz, suitable for fast peripherals.
3fill in blank
hardFix the error in the code to send data over SPI.
Raspberry Pi
data = [0x01, 0x02, 0x03] response = spi.[1](data)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using write which only sends data
Using read which only reads data
✗ Incorrect
The xfer method sends data and receives response simultaneously in SPI communication.
4fill in blank
hardFill both blanks to create a dictionary comprehension that maps pin numbers to their SPI modes.
Raspberry Pi
spi_modes = {pin: [1] for pin in range(1, 5) if pin [2] 3} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong comparison operator
Assigning wrong SPI mode
✗ Incorrect
This comprehension assigns SPI mode 0 to pins less than 3.
5fill in blank
hardFill all three blanks to filter and map SPI devices with speed over 500 kHz.
Raspberry Pi
fast_devices = [1]: [2] for [3] in devices if devices[[3]] > 500000}
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong variable names
Incorrect dictionary comprehension syntax
✗ Incorrect
This comprehension creates a dictionary of devices with speed over 500 kHz, mapping device names to their speeds.
