Bird
0
0
Raspberry Piprogramming~10 mins

Reading analog sensors through ADC in Raspberry Pi - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Reading analog sensors through ADC
Start
Initialize ADC
Read analog value
Convert to digital
Use digital value
Repeat or Stop
The Raspberry Pi reads an analog sensor by initializing an ADC, reading the sensor's analog voltage, converting it to a digital value, then using that value in the program.
Execution Sample
Raspberry Pi
import spidev
spi = spidev.SpiDev()
spi.open(0,0)
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)
This code reads an analog value from channel 0 of an MCP3008 ADC connected via SPI and prints the digital value.
Execution Table
StepActionSPI Data SentSPI Data ReceivedDigital ValueOutput
1Open SPI bus 0, device 0----
2Call read_adc(0)[1, 128, 0][1, 2, 150]662-
3Calculate digital value--((2 & 3) << 8) + 150 = 662-
4Return digital value--662-
5Print value--662662
💡 Finished reading and printing the analog sensor value from ADC channel 0
Variable Tracker
VariableStartAfter read_adc callAfter calculationFinal
spiNot openedOpened SPI bus 0,0Opened SPI bus 0,0Opened SPI bus 0,0
channelN/A000
adcN/A[1,2,150][1,2,150][1,2,150]
dataN/AN/A662662
valueN/AN/AN/A662
Key Moments - 3 Insights
Why do we send [1, (8+channel)<<4, 0] to the SPI device?
This command tells the MCP3008 which channel to read. The first byte '1' starts communication, the second byte selects the channel with a start bit, and the third byte is a placeholder. See execution_table row 2.
How is the digital value calculated from the SPI response?
The MCP3008 sends 3 bytes. We mask and shift bits from the second and third bytes to get a 10-bit digital value. See execution_table row 3 for the bit operations.
Why do we need an ADC with Raspberry Pi to read analog sensors?
Raspberry Pi GPIO pins read digital signals only. An ADC converts analog voltages from sensors into digital numbers the Pi can understand.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the digital value calculated at step 3?
A662
B150
C2
D128
💡 Hint
Check the 'Digital Value' column in execution_table row 3.
At which step is the SPI bus opened?
AStep 2
BStep 4
CStep 1
DStep 5
💡 Hint
Look at the 'Action' column in execution_table row 1.
If we change the channel to 1 in read_adc(1), how would the SPI data sent change at step 2?
A[1, 136, 0]
B[1, 144, 0]
C[1, 128, 0]
D[1, 8, 0]
💡 Hint
The second byte is (8 + channel) << 4. For channel 1, calculate (8+1)<<4.
Concept Snapshot
Reading analog sensors with Raspberry Pi requires an ADC like MCP3008.
Use SPI to communicate: send command bytes to select channel.
Receive 3 bytes, extract 10-bit digital value.
Convert analog voltage to digital number.
Use this value in your program for sensor data.
Full Transcript
This visual execution shows how Raspberry Pi reads analog sensors using an ADC chip like MCP3008. First, the SPI bus is opened to communicate with the ADC. Then, a command is sent to select the sensor channel. The ADC responds with 3 bytes containing the analog reading converted to a digital value. We extract the 10-bit number by masking and shifting bits from the response. Finally, the digital value is printed or used in the program. This process repeats to continuously read sensor data. The key is that Raspberry Pi cannot read analog signals directly, so it uses an ADC to convert analog voltages to digital numbers it can understand.