Bird
0
0
Raspberry Piprogramming~10 mins

MCP3008 ADC over SPI in Raspberry Pi - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import the SPI library needed to communicate with MCP3008.

Raspberry Pi
import [1]
Drag options to blanks, or click blank then click option'
Agpiozero
Bspidev
CRPi.GPIO
Dtime
Attempts:
3 left
💡 Hint
Common Mistakes
Importing gpiozero or RPi.GPIO instead of spidev.
Forgetting to import the SPI library.
2fill in blank
medium

Complete the code to open SPI bus 0, device 0 for communication.

Raspberry Pi
spi = spidev.SpiDev()
spi.[1](0, 0)
Drag options to blanks, or click blank then click option'
Aopen
Bstart
Cconnect
Dinit
Attempts:
3 left
💡 Hint
Common Mistakes
Using connect or init instead of open.
Not opening the SPI device before use.
3fill in blank
hard

Fix the error in the code to read channel 0 from MCP3008 correctly.

Raspberry Pi
def read_channel(channel):
    adc = spi.xfer2([1, (8 + [1]) << 4, 0])
    data = ((adc[1] & 3) << 8) + adc[2]
    return data
Drag options to blanks, or click blank then click option'
Aspi
B0
C1
Dchannel
Attempts:
3 left
💡 Hint
Common Mistakes
Using a fixed number like 0 or 1 instead of the channel variable.
Using an undefined variable.
4fill in blank
hard

Fill both blanks to set SPI speed and mode correctly for MCP3008.

Raspberry Pi
spi.max_speed_hz = [1]
spi.mode = [2]
Drag options to blanks, or click blank then click option'
A1350000
B500000
C0
D1
Attempts:
3 left
💡 Hint
Common Mistakes
Using too high or too low SPI speed.
Setting wrong SPI mode causing communication errors.
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension that reads channels 0 to 3 and stores their values.

Raspberry Pi
adc_values = {ch: read_channel([1]) for ch in range([2], [3])}
Drag options to blanks, or click blank then click option'
Ach
B0
C4
D1
Attempts:
3 left
💡 Hint
Common Mistakes
Using a fixed number instead of the loop variable for reading channels.
Incorrect range boundaries causing missing or extra channels.