Industrial manufacturing
Industrial Internet of Things | Industrial materials | Equipment Maintenance and Repair | Industrial programming |
home  MfgRobots >> Industrial manufacturing >  >> Manufacturing Technology >> Manufacturing process

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

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

  1. Open a terminal and run:
    sudo raspi-config
  2. Navigate to Advanced Options → SPI and enable it.
  3. Reboot the Pi:
    sudo reboot
  4. After reboot, verify SPI devices exist:
    ls /dev/
    You should see spidev0.0 and spidev0.1.
  5. 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

  1. Install prerequisites:
    sudo apt-get install python-dev git
  2. Clone the py-spidev repository:
    git clone https://github.com/doceme/py-spidev
  3. 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

  1. C# Fundamentals: Input and Output Essentials
  2. TMP006 Infrared Temperature Sensor with Raspberry Pi: Python Library & Setup Guide
  3. Build a Remote Temperature Sensor with Raspberry Pi and Python – Step‑by‑Step Guide
  4. Control an LED with a PIR Motion Sensor on Raspberry Pi
  5. Reading a Linear Hall Sensor with Raspberry Pi: Java Guide & ADC0832 Insights
  6. Reading Analog Sensors with Raspberry Pi and Zabbix: A Step‑by‑Step Guide
  7. Raspberry Pi–Based Bathroom Occupancy Sensor with Voice & SMS Alerts via Twilio
  8. Connect Analog Sensors to Raspberry Pi with MCP3008 – Wiring, SPI Setup, and Sample Python Code
  9. Using an MCP3008 ADC to Read Analog Sensors with Raspberry Pi
  10. Integrating the Acconeer A111 Pulsed Radar with a Raspberry Pi: A Practical Guide