So now that we bought the MCP3008 ADC lets get it up and running!

Note: This is my first instructables submision so if something is wrong just tell me so and if you like my style leave a comment, also I'm from the Netherlands so my English might not be perfect.

In this project we will be using a Fundruino moisture sensor, don't mind if you don't have the same sensor this process will work for any analog sensor. Also you might have the MCP3004 this project will work aswell only the wiring is slightly different so I suggest that you check the datasheet first, the datasheet is stated at the end of step 3.

So lets get started!

Step 1: ​Step 1: Wiring

It is important to know the orientation of your chip. It is marked by a small semi-circular indentation on top of the chip.

Wire up as shown in the wire scheme (Note: You should use 3.3V only!)

MCP3008 VDD -> 3.3V (red)

MCP3008 VREF -> 3.3V (red)

MCP3008 AGND -> GND (black)

MCP3008 CLK -> pin 23 (orange)

MCP3008 DOUT -> pin 21(yellow)

MCP3008 DIN -> pin 19 (blue)

MCP3008 CS -> pin 24 (violet)

MCP3008 DGND -> GND (black)

These instructions are in the same order as the right side of the MCP3008 with the semi-circular indentation pointed upwards.

The left side is the analog input were the right side is the digital output.

In this project a Fundruino moisture sensor is used to give the analog input, this sensor is the same as the Ardruino moisture sensor only cheaper.

To wire up the sensor:

S pin -> one of the channels on the left side of the MCP3008 (green)

+ pin -> 3.3V (red)

- pin -> GND (black)

Now that we wired up lets convert analog inputs to digital outputs!

Step 2: Step 2: Update Your Raspberry Pi

For the MCP3008 chip we need the following software on our Pi,

Type in the command line:

ls /dev/

Now you should be presented with al lot of yellow text, check if spidev0.0 and spidev0.1 is in there. The position is shown by the red circle in the picture with the yellow text. if not no worries the following instructions will tell you how to get it there. If you do have the spidev0.0 and spidev0.1 you can skip to Unblacklisting.

First we want to enable the SPI functions on the Raspberry Pi:

Type in the command line:

sudo raspi-config

Now you will see the config window that you saw when your first booted your Rasbian.

scroll down to Advanced Options and enable SPI

Now reboot

Now type in the command line :

ls /dev/

If spidev0.0 and spidev0.1 is in there skip to Unblacklisting.

If not we need to update your Rasbian

Type in the command line:

sudo apt-get -y update && sudo apt-get -y upgrade

This will take a while.

When finished type in the command line:

sudo wget http://goo.gl/1BOfJ -O /usr/bin/rpi-update

When finished type in the command line:

sudo chmod +x /usr/bin/rpi-update

When finished type in the command line:

sudo reboot

Now you need to reboot to activate the update.

So type in the command line:

sudo reboot

When finished type in the command line:

ls /dev/

Now the spidev0.0 and spidev0.1 should be there, if not you might have made a mistake so retry updating your pi. If it's there we need to unblacklist it.


Type in your command line:

sudo nano /etc/modprobe.d/raspi-blacklist.conf

Now there is two options, or you will see nothing in the blacklist file or you will see three lines of text.

If you see nothing just type:

blacklist i2c-bcm2708

If you do see three lines of text delete the hash before the blacklist i2c-bcm2708 sentence.

After your finished Unblacklisting reboot now.

Type in the command line:

sudo reboot

When finished type in the command line:

sudo apt-get install python-dev git

When finished type in the command line:

git clone git://github.com/doceme/py-spidev

When finished type in the command line:

cd py-spidev/

Now type in the command line:

sudo python setup.py install

When finished type in the command line:

cd ~

Now type in the command line:

nano mcp3008.py

we are now ready to write our software

Step 3: Step 3: Pasting the Code

Now you can copy my code into your file or you can write it yourself.

My code:

For some reasons the indentation left behind, in the photo you will find the propper structure so you just need to indent correctly and you are good to go.

#! /usr/bin/env python

# python programa to comunicate with an MCP3008

# Import our SpiDe wrapper and our sleep function

import spidev

from time import sleep

# Establish SPI device on Bus 0,Device 0

spi = spidev.SpiDev()


def getAdc (channel):

#check valid channel

if ((channel>7)or(channel<0)):

return -1

# Preform SPI transaction and store returned bits in 'r'

r = spi.xfer([1, (8+channel) << 4, 0])

#Filter data bits from retruned bits

adcOut = ((r[1]&3) << 8) + r[2]

percent = int(round(adcOut/10.24))

#print out 0-1023 value and percentage

print("ADC Output: {0:4d} Percentage: {1:3}%".format (adcOut,percent))


while True:


Note: If you are using another channel you should write getAdc(your channel) because that is the condition used by the function to determine which channel you are using.

Now you can run this program by typing in the command line:

sudo python mcp3008.py

If you want to stop the program hit Ctrl C

The picture shows what this program should output.


Datasheet MCP3008: http://www.adafruit.com/datasheets/MCP3008.pdf

Video by Fraser May: https://www.youtube.com/watch?v=oW0mTwFck_c

Adafruit: https://learn.adafruit.com/reading-a-analog-in-an...

Hardware: Raspberry Pi model B, Fundruino moisture sensor, MCP3008

<p>Nicely done. Thanks for sharing this!</p>
<p>My pleasure I really enjoyed making the instructable page! I would like to pass on the credits to Fraser may.</p>
<p>This work with raspi 3?<br>the spi pin configuration is not same.</p>
<p>I did it with a rasp 3. It works the same: The SPI pins are the same. Just get GPIO header overview for the rasp 3.</p>
<p>Thank you&iexcl;</p>
<p>For me it worked only with &quot;<em>r = spi.xfer2(...)&quot;</em></p>
<p>pi@WeatherPi ~/smokesens $ sudo python ./<em>mcp3008.py</em></p><p> File &quot;./<em>mcp3008.py</em>&quot;, line 21</p><p> if ((channel&gt;7)or(channel&lt;0)):</p><p> ^</p><p>IndentationError: expected an indented block<br><br>Any ideaa what I have done wrong ? </p>
<p>make sure you don't just copy/paste the code into a file and save it.<br>make it look exactly like the image of the code with all the jumping tabs</p>
Het kan zijn dat de code niet goed geindent is, ik heb die fout nog niet meegemaakt dus ik weet niet precies hoe je het moet oplossen, maar het zou kunnen dat er bij het kopi&euml;ren iets is fout gegaan <br><br>
<p>Hallo Seempie</p><p>Bedankt voor het artikel. Het werkt wel, maar waarom zie ik tussen de metingen 16 keer de waarde 0 en daarna weer de gemeten waarde en vervolgens weer 16 keer 0 enz. ?</p><p>Ik heb even het filmpje waar jij naar refereert bekeken, maar daar zie ik die 16 keer 0 waardes niet.</p><p>Heb jij enig idee hoe dat komt?</p><p>Alvast bedankt.</p><p>Grtz,</p><p>Stormlord</p>

About This Instructable




Bio: Hi there ! I'm currently doing my final thesis, it is About pattern reconition and sensors. I'm programming in python and i Will put ... More »
More by seempie:The "Real" Persian Tea Mango lassi Analog sensor input raspberry pi using a MCP3008: wiring/installing/basic program 
Add instructable to: