Bird
0
0
Raspberry Piprogramming~20 mins

Reading analog sensors through ADC in Raspberry Pi - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
ADC Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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)
AA list of three integers representing raw SPI bytes
BAn integer between 0 and 1023 representing the analog value
CA string describing the ADC channel
DA runtime error due to incorrect SPI usage
Attempts:
2 left
💡 Hint
The MCP3008 ADC returns a 10-bit value from 0 to 1023 for each channel.
🧠 Conceptual
intermediate
1:00remaining
Understanding ADC resolution
If an ADC has a 12-bit resolution, what is the maximum integer value it can output?
A1023
B8191
C2047
D4095
Attempts:
2 left
💡 Hint
The maximum value is 2^bits - 1.
🔧 Debug
advanced
2: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))
AWrong output because of operator precedence error
BTypeError because of wrong data type in SPI transfer
CSyntaxError due to missing parentheses
DNo error, correct output
Attempts:
2 left
💡 Hint
Check how the bit shift and addition are grouped in the list argument.
📝 Syntax
advanced
1: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
A
atad nruter    
]2[cda + )8 &lt;&lt; )3 &amp; ]1[cda(( = atad    
)]0 ,4 &lt;&lt; )lennahc + 8( ,1[(2refx.ips = cda    
:)lennahc(cda_daer fed
B
def read_adc(channel):
    adc = spi.xfer2([1, (8 + channel) &lt;&lt; 4, 0])
    data = ((adc[1] &amp; 3) &lt;&lt; 8) + adc[2]
    return data
C
def read_adc(channel)
    adc = spi.xfer2([1, (8 + channel) &lt;&lt; 4, 0])
    data = ((adc[1] &amp; 3) &lt;&lt; 8) + adc[2]
    return data
D
ef read_adc(channel):
    adc = spi.xfer2([1, (8 + channel) &lt;&lt; 4, 0])
    data = ((adc[1] &amp; 3) &lt;&lt; 8) + adc[2]
    return data
Attempts:
2 left
💡 Hint
Check the function definition line for missing colon.
🚀 Application
expert
2: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?
A1.65 V
B3.3 V
C2.56 V
D0.66 V
Attempts:
2 left
💡 Hint
Voltage = (ADC reading / max ADC value) * reference voltage.