Bird
0
0
Raspberry Piprogramming~10 mins

MCP3008 ADC over SPI in Raspberry Pi - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - MCP3008 ADC over SPI
Start SPI Communication
Send Start Bit + Channel Selection
MCP3008 Converts Analog to Digital
Receive 10-bit Digital Data
Process and Use Digital Value
End SPI Communication
This flow shows how the Raspberry Pi talks to the MCP3008 chip over SPI to get analog sensor data as digital values.
Execution Sample
Raspberry Pi
import spidev
spi = spidev.SpiDev()
spi.open(0,0)
channel = 0
resp = spi.xfer2([1,(8+channel)<<4,0])
data = ((resp[1]&3)<<8) + resp[2]
This code opens SPI, sends commands to MCP3008 to read a channel, and gets the 10-bit digital result.
Execution Table
StepSPI Data SentSPI Data ReceivedActionResult
1[1, (8+0)<<4, 0][0, 2, 150]Send start + channel 0, receive 3 bytesRaw response bytes received
2N/AN/AExtract 10-bit value((2 & 3) << 8) + 150 = 662
3N/AN/AUse digital valueValue 662 represents analog input voltage
4N/AN/AEnd communicationSPI transaction complete
💡 All bytes sent and received; 10-bit ADC value extracted successfully
Variable Tracker
VariableStartAfter Step 1After Step 2Final
spiNot openedOpened SPI bus 0, device 0Opened SPI bus 0, device 0Opened SPI bus 0, device 0
respNone[0, 2, 150][0, 2, 150][0, 2, 150]
dataNoneNone662662
Key Moments - 3 Insights
Why do we send three bytes [1, (8+channel)<<4, 0] to MCP3008?
The first byte (1) is the start bit, the second byte encodes single-ended mode and channel number, and the third byte is a dummy to clock out the response. See execution_table row 1.
How is the 10-bit ADC value extracted from the received bytes?
The MCP3008 sends 3 bytes; the last 10 bits are split across resp[1] and resp[2]. We mask resp[1] with 3 (binary 11), shift left 8 bits, then add resp[2]. See execution_table row 2.
What does the digital value represent?
It represents the analog voltage level on the selected channel scaled to 0-1023 (10 bits). See execution_table row 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table row 1, what is the purpose of the first byte sent (value 1)?
AStart bit to begin communication
BChannel number
CDummy byte for clocking
DEnd of transmission
💡 Hint
Refer to execution_table row 1 under 'SPI Data Sent' and 'Action'
At which step is the 10-bit ADC value calculated?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Check execution_table row 2 where 'Extract 10-bit value' happens
If channel is changed from 0 to 3, how does the SPI data sent change in step 1?
A[1, (3)<<4, 0]
B[1, (8+0)<<4, 0]
C[1, (8+3)<<4, 0]
D[1, (8)<<4, 3]
💡 Hint
Look at how channel is added in the second byte in execution_table row 1
Concept Snapshot
MCP3008 ADC over SPI:
- Open SPI bus on Raspberry Pi
- Send 3 bytes: start bit, channel + mode, dummy
- Receive 3 bytes response
- Extract 10-bit ADC value from response
- Use value to read analog sensors
Full Transcript
This visual trace shows how the Raspberry Pi communicates with the MCP3008 analog-to-digital converter chip over SPI. First, SPI communication starts by opening the bus. Then, three bytes are sent: a start bit (1), a byte encoding single-ended mode and the channel number shifted left 4 bits, and a dummy byte to clock out the response. The MCP3008 returns three bytes, from which the 10-bit digital value is extracted by masking and shifting bits. This value represents the analog voltage on the selected channel scaled from 0 to 1023. Finally, SPI communication ends after reading the value. Variables like spi, resp, and data change as the code runs, showing the process step-by-step. Common confusions include why three bytes are sent, how the 10-bit value is extracted, and what the digital value means. Quizzes test understanding of these steps.