Connect Analog Sensors to Raspberry Pi with MCP3008 – Wiring, SPI Setup, and Sample Python Code
Ready to read analog signals with your Raspberry Pi? This guide walks you through wiring an MCP3008 ADC, enabling SPI, installing the required libraries, and running a basic Python script to fetch sensor data.
Step 1: Wiring the MCP3008
- Power (3.3 V) – VDD & VREF to Raspberry Pi 3.3 V pin (red).
- Ground – AGND & DGND to Raspberry Pi GND (black).
- SPI Connections –
- CLK → Pi pin 23 (GPIO11) – orange.
- DOUT → Pi pin 21 (GPIO9) – yellow.
- DIN → Pi pin 19 (GPIO10) – blue.
- CS → Pi pin 24 (GPIO8) – violet.
- Analog Inputs – Connect your sensor’s signal wire to one of the MCP3008’s A0–A7 pins (left side of the chip). Use 3.3 V for V+ and GND for G–.
Make sure the chip’s semi‑circular notch faces the same direction as shown in the diagram. For a MCP3004 the pin layout is identical except the chip has only four analog inputs.
Step 2: Enable SPI on the Raspberry Pi
- Open a terminal and run:
sudo raspi-config
- Navigate to Advanced Options → SPI and enable it.
- Reboot the Pi:
sudo reboot
- After reboot, verify SPI devices exist:
ls /dev/
You should seespidev0.0andspidev0.1. - If they’re missing, update your system:
sudo apt-get update && sudo apt-get upgrade
then install the latest firmware:sudo wget https://goo.gl/1BOfJ -O /usr/bin/rpi-update sudo chmod +x /usr/bin/rpi-update sudo rpi-update sudo reboot
Unblacklist I²C (if necessary)
Sometimes I²C is blacklisted, preventing SPI from working. Open the blacklist file:
sudo nano /etc/modprobe.d/raspi-blacklist.conf
Remove or comment out any line containing blacklist i2c-bcm2708, then reboot.
Step 3: Install Python SPI Library
- Install prerequisites:
sudo apt-get install python-dev git
- Clone the py-spidev repository:
git clone https://github.com/doceme/py-spidev
- Install the library:
cd py-spidev sudo python setup.py install
Step 4: Sample Python Code
Create mcp3008.py in your home directory:
nano ~/mcp3008.py
Paste the following script:
# mcp3008.py
import spidev
import time
# Open SPI bus 0, device 0
spi = spidev.SpiDev()
spi.open(0, 0)
spi.max_speed_hz = 1350000
def read_channel(channel):
if channel < 0 or channel > 7:
return -1
adc = spi.xfer2([1, (8 + channel) << 4, 0])
data = ((adc[1] & 3) << 8) + adc[2]
return data
try:
while True:
value = read_channel(0) # Read from channel 0
print(f"Channel 0: {value}")
time.sleep(1)
except KeyboardInterrupt:
spi.close()
print("Program terminated")
Run the script with:
python3 ~/mcp3008.py
You should see a continuous stream of raw ADC values (0–1023). Convert these to voltage or sensor units using the MCP3008 datasheet.
References
Manufacturing process
- C# Fundamentals: Input and Output Essentials
- TMP006 Infrared Temperature Sensor with Raspberry Pi: Python Library & Setup Guide
- Build a Remote Temperature Sensor with Raspberry Pi and Python – Step‑by‑Step Guide
- Control an LED with a PIR Motion Sensor on Raspberry Pi
- Reading a Linear Hall Sensor with Raspberry Pi: Java Guide & ADC0832 Insights
- Reading Analog Sensors with Raspberry Pi and Zabbix: A Step‑by‑Step Guide
- Raspberry Pi–Based Bathroom Occupancy Sensor with Voice & SMS Alerts via Twilio
- Connect Analog Sensors to Raspberry Pi with MCP3008 – Wiring, SPI Setup, and Sample Python Code
- Using an MCP3008 ADC to Read Analog Sensors with Raspberry Pi
- Integrating the Acconeer A111 Pulsed Radar with a Raspberry Pi: A Practical Guide