Analog Sensor Input Raspberry Pi Using a MCP3008: Wiring/installing/basic Program




About: 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 my findings in instructables. In the future i hope to make instructables A...

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!

Teacher Notes

Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.

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 -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://

When finished type in the command line:

cd py-spidev/

Now type in the command line:

sudo python install

When finished type in the command line:

cd ~

Now type in the command line:


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(),0)

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

If you want to stop the program hit Ctrl C

The picture shows what this program should output.


Datasheet MCP3008:

Video by Fraser May:


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

Be the First to Share


    • CNC Contest

      CNC Contest
    • Make it Move

      Make it Move
    • Teacher Contest

      Teacher Contest

    12 Discussions


    Reply 4 years ago on Introduction

    My pleasure I really enjoyed making the instructable page! I would like to pass on the credits to Fraser may.


    Question 1 year ago on Step 2

    I have a little issue running the program, i get this

    pi@raspberrypi:~ $ python
    File "", line 21
    if ((channel>7)or(channel
    IndentationError: expected an indented block

    where would i change the channel?

    1 answer

    Answer 1 year ago

    hi, so i think you are missing a indentation tab, i would first give that a go, the channel is predefined on the function that is defined earlier than the loop. if that does not work either you might also delete the complete if statement since its more of a check anyway. good luck!

    FabioD49hg. acevedo

    Reply 3 years ago

    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.


    3 years ago

    For me it worked only with "r = spi.xfer2(...)"


    3 years ago

    pi@WeatherPi ~/smokesens $ sudo python ./

    File "./", line 21

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


    IndentationError: expected an indented block

    Any ideaa what I have done wrong ?

    1 reply

    Reply 3 years ago

    make sure you don't just copy/paste the code into a file and save it.
    make it look exactly like the image of the code with all the jumping tabs


    4 years ago

    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ëren iets is fout gegaan


    4 years ago on Introduction

    Hallo Seempie

    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. ?

    Ik heb even het filmpje waar jij naar refereert bekeken, maar daar zie ik die 16 keer 0 waardes niet.

    Heb jij enig idee hoe dat komt?

    Alvast bedankt.