Challenge - 5 Problems
ADC Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Reading ADC value with MCP3008
What is the output of this code snippet that reads channel 0 from an MCP3008 ADC connected to a Raspberry Pi?
Raspberry Pi
import spidev spi = spidev.SpiDev() spi.open(0, 0) spi.max_speed_hz = 1350000 def read_adc(channel): adc = spi.xfer2([1, (8 + channel) << 4, 0]) data = ((adc[1] & 3) << 8) + adc[2] return data value = read_adc(0) print(value)
Attempts:
2 left
💡 Hint
The MCP3008 ADC returns a 10-bit value from 0 to 1023 for each channel.
✗ Incorrect
The code reads the analog value from channel 0 using SPI communication with MCP3008. The returned value is a 10-bit integer between 0 and 1023.
🧠 Conceptual
intermediate1:00remaining
Understanding ADC resolution
If an ADC has a 12-bit resolution, what is the maximum integer value it can output?
Attempts:
2 left
💡 Hint
The maximum value is 2^bits - 1.
✗ Incorrect
A 12-bit ADC can output values from 0 to 2^12 - 1 = 4095.
🔧 Debug
advanced2:00remaining
Fixing incorrect SPI data reading
What error does this code produce when trying to read from MCP3008?
import spidev
spi = spidev.SpiDev()
spi.open(0, 0)
spi.max_speed_hz = 1350000
def read_adc(channel):
adc = spi.xfer2([1, 8 + channel << 4, 0])
data = ((adc[1] & 3) << 8) + adc[2]
return data
print(read_adc(0))
Attempts:
2 left
💡 Hint
Check how the bit shift and addition are grouped in the list argument.
✗ Incorrect
The expression 8 + channel << 4 is evaluated as 8 + (channel << 4) due to operator precedence, which is incorrect. It should be (8 + channel) << 4.
📝 Syntax
advanced1:30remaining
Identify the syntax error in ADC reading function
Which option contains a syntax error in this ADC reading function?
Raspberry Pi
def read_adc(channel): adc = spi.xfer2([1, (8 + channel) << 4, 0]) data = ((adc[1] & 3) << 8) + adc[2] return data
Attempts:
2 left
💡 Hint
Check the function definition line for missing colon.
✗ Incorrect
Option C is missing a colon ':' after the function definition, causing a syntax error.
🚀 Application
expert2:00remaining
Calculating voltage from ADC reading
Given a 10-bit ADC (0-1023) with a 3.3V reference, what is the voltage value for an ADC reading of 512?
Attempts:
2 left
💡 Hint
Voltage = (ADC reading / max ADC value) * reference voltage.
✗ Incorrect
Voltage = (512 / 1023) * 3.3 ≈ 1.65 V.
