Bird
0
0
Raspberry Piprogramming~10 mins

Reading analog sensors through ADC 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 library needed to read from the ADC.

Raspberry Pi
import [1]
Drag options to blanks, or click blank then click option'
Aspidev
Btime
Cmath
Drandom
Attempts:
3 left
💡 Hint
Common Mistakes
Importing unrelated libraries like random or math.
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
Bconnect
Cstart
Dinit
Attempts:
3 left
💡 Hint
Common Mistakes
Using methods like start or connect which do not exist.
Forgetting to open the SPI device before use.
3fill in blank
hard

Fix the error in the function that reads data from channel 0 of the MCP3008 ADC.

Raspberry Pi
def read_adc(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'
A1
B0
Cspi
Dchannel
Attempts:
3 left
💡 Hint
Common Mistakes
Hardcoding the channel number instead of using the parameter.
Using an undefined variable.
4fill in blank
hard

Fill both blanks to create a dictionary comprehension that reads ADC values for channels 0 to 3.

Raspberry Pi
adc_values = {ch: read_adc(ch) for ch in [1] if ch [2] 3}
Drag options to blanks, or click blank then click option'
Arange(4)
Brange(3)
C<=
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using range(3) which excludes channel 3.
Using < which excludes channel 3.
5fill in blank
hard

Fill all three blanks to convert the raw ADC value to voltage assuming a 3.3V reference and 10-bit ADC.

Raspberry Pi
voltage = ([1] / [2]) * [3]
Drag options to blanks, or click blank then click option'
Araw_value
B1023
C3.3
D10
Attempts:
3 left
💡 Hint
Common Mistakes
Using 1024 instead of 1023 as max ADC value.
Multiplying before dividing.
Using wrong reference voltage.