Raspberry Pi and MCP3008 - Caronte Consulting
How To Raspberry Pi MCP3008

Raspberry Pi and MCP3008

Read analog signals on Raspberry Pi  with MCP3008

Short story: on the Raspberry Pi you can’t analyze analog signals. If you have this need you have to get an external help. The most common configuration involved use of an analog/digital converter (aka ADC, Analog to Digital Converter).

MCP3008

Hard stuff? No, if you use the MCP3008, an analog to digital converter produced by Microchip (here the datasheet) which allows you to read up to 8 analog signals. Each read channel is encoded with 10 bits (so the maximum readable value will be 210-1 = 1023, while the minimum value will be 0). The less demanding people can use the MCP3004, that only has 4 channels. To communicate with the world, the MCP3008 uses a classical communication system between microcontroller/microprocessor and integrated circuits: Serial Peripheral Interface (SPI), an easy thing to configure on Raspberry Pi.

MCP3008 precision is quite similar to Arduino Uno, than this chip is a good option to start with ADC circuits and in the same time have a good precision.

Installing the Development Environment

As a development environment we prefer Python, so let’s open the terminal and install it:

sudo apt-get update
sudo apt-get install build-essential python-dev python-smbus python-pip
sudo pip3 install spidev

Wiring

This way to connect MCP3008 to the Raspberry Pi is called Software SPI.

After the wiring, run your Raspberry Pi than execute the follow:

sudo raspi-config

then enable the SPI.

ARRRR! DISCOVER THE ADVENTURES OF CARONTE CONSULTING’s PIRATES

FOLLOW US ON FACEBOOK

Checking the operation

To check the correct reading from the MCP3008 you can use a simplification of the code suggested by raspibo.org:

import spidev
import time
import math

mcp3008 = spidev.SpiDev(0,0)
adcnum=0 #this means what channel you want to read
xferarg=[1,(1+adcnum)<<4,0]
vref = 3.3
ret=mcp3008.xfer2([1,(8+adcnum)<<4,0])
adcout = (((ret[1]&3) << 8) + ret[2]) * 3.3 / 1024
print ((ret[1]&3) << 8) + ret[2], adcout

mcp3008.close()

Adafruit’s library

You can take advantage of the Adafruit library to query the MCP3008 in a humanly understandable way. You can install the library with the following command:

sudo pip install adafruit-mcp3008

After that you can try this code (it’s a simplification of Tony DiCola’s simpletest.py) to check the ADC:

import time
import Adafruit_GPIO.SPI as SPI
import Adafruit_MCP3008

CLK = 18
MISO = 23
MOSI = 24
CS = 25

mcp = Adafruit_MCP3008.MCP3008(clk=CLK, cs=CS, miso=MISO, mosi=MOSI)
print('Reading MCP3008 values, press Ctrl-C to quit...')
print('| {0:>4} | {1:>4} | {2:>4} | {3:>4} | {4:>4} | {5:>4} | {6:>4} | {7:>4} |'.format(*range(8)))
print('-' * 57)
while True:

values = [0]*8
for i in range(8):

values[i] = mcp.read_adc(i)
print('| {0:>4} | {1:>4} | {2:>4} | {3:>4} | {4:>4} | {5:>4} | {6:>4} | {7:>4} |'.format(*values))

time.sleep(0.5)

WE ARE MONITORING SPECIALISTS

DON’T WASTE YOUR TIME AND CALL US!

Share your thoughts