Complete the code to import the SPI library needed to communicate with MCP3008.
import [1]
The spidev library is used to communicate over SPI with devices like the MCP3008 ADC.
Complete the code to open SPI bus 0, device 0 for communication.
spi = spidev.SpiDev() spi.[1](0, 0)
The open method opens the SPI bus and device for communication.
Fix the error in the code to read channel 0 from MCP3008 correctly.
def read_channel(channel): adc = spi.xfer2([1, (8 + [1]) << 4, 0]) data = ((adc[1] & 3) << 8) + adc[2] return data
The variable channel must be used to select the ADC channel dynamically.
Fill both blanks to set SPI speed and mode correctly for MCP3008.
spi.max_speed_hz = [1] spi.mode = [2]
The MCP3008 works well with SPI speed of 1.35 MHz and SPI mode 0.
Fill all three blanks to create a dictionary comprehension that reads channels 0 to 3 and stores their values.
adc_values = {ch: read_channel([1]) for ch in range([2], [3])}The comprehension uses ch as the argument to read_channel and loops from 0 to 4 (exclusive) to read channels 0,1,2,3.
