Challenge - 5 Problems
MCP3008 SPI Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Reading MCP3008 Channel Output
What is the output of this Python code snippet that reads from channel 0 of the MCP3008 ADC using SPI on a Raspberry Pi?
Raspberry Pi
import spidev spi = spidev.SpiDev() spi.open(0, 0) spi.max_speed_hz = 1350000 # Function to read channel 0 adc = spi.xfer2([1, (8 + 0) << 4, 0]) data = ((adc[1] & 3) << 8) + adc[2] print(data)
Attempts:
2 left
💡 Hint
The MCP3008 returns a 10-bit value from 0 to 1023 for each channel read.
✗ Incorrect
The code sends the correct SPI command to read channel 0 and processes the returned bytes to get a 10-bit integer value representing the analog input.
🧠 Conceptual
intermediate1:30remaining
Understanding MCP3008 SPI Command Structure
Which part of the SPI command byte sequence selects the MCP3008 channel to read?
Attempts:
2 left
💡 Hint
Look at how the channel number is shifted and combined in the command bytes.
✗ Incorrect
The MCP3008 expects the channel number in bits 4 to 6 of the second byte sent over SPI, shifted left by 4 bits.
🔧 Debug
advanced2:30remaining
Fixing Incorrect MCP3008 SPI Read Output
This code attempts to read channel 3 from MCP3008 but always returns 0. What is the cause?
Raspberry Pi
import spidev spi = spidev.SpiDev() spi.open(0, 0) spi.max_speed_hz = 1350000 adc = spi.xfer2([1, (3 << 4), 0]) data = ((adc[1] & 3) << 8) + adc[2] print(data)
Attempts:
2 left
💡 Hint
Check how the channel number is combined with the start bit in the command byte.
✗ Incorrect
The MCP3008 command requires adding 8 to the channel number before shifting left by 4 to set the start bit and single-ended mode correctly.
📝 Syntax
advanced1:30remaining
Identify Syntax Error in MCP3008 SPI Read Code
Which option contains a syntax error that prevents the MCP3008 SPI read code from running?
Attempts:
2 left
💡 Hint
Look carefully at the commas separating list elements.
✗ Incorrect
Option B is missing a comma between '(8 + 2) << 4' and '0' in the list, causing a syntax error.
🚀 Application
expert2:00remaining
Calculate Voltage from MCP3008 ADC Reading
Given a 3.3V reference and a 10-bit MCP3008 ADC reading stored in variable
data, which formula correctly converts data to voltage?Attempts:
2 left
💡 Hint
The MCP3008 outputs values from 0 to 1023 for 10 bits, and the reference voltage is 3.3V.
✗ Incorrect
The maximum ADC value is 1023 (2^10 - 1), so dividing by 1023 and multiplying by 3.3V gives the correct voltage.
