Reading analog values with the mcp3008 and the raspberry pi

Reading analog values with the mcp3008 and the raspberry pi Titelbild

Folgende Teile braucht ihr für die Anleitung

Smart Plant kit for the Raspberry Produktbild

Smart Plant kit for the Raspberry | 19.95€

MCP3008 - Measuring analog values with the Raspberry Pi xx

The Raspberry Pi is nearly an almighty computer and by utilizing the gpio pins we are easily able to access our environment to complete tasks such as measuring data.
With the gpio pins we are easily able to read digital data consisting of 0 or 1. For example, with a push button we have only two states. Either there is a current present in the circuit in which we would read a 1 (when the button is pushed) or there is no connected circuit (when the button is not pushed) and we get a 0. But there are other sensors which communicate their values with a value of 0 or 1. For this you need an ADC (Analog to Digital Converter). The MCP3008 is an example of such an ADC. To connect the MCP3008 to the Raspberry Pi we simply use the SPI gpio pins and the spio communication protocol to gain the ability to read 8 analog inputs at a time.

MCP3008 ADC Circuit

The end with the half circle should be on the Downside. CH0-CH7 are the analog inputs we will be utilizing.

  • VDD: Supply voltage for the mcp3008
  • V_REF: Analog input reference voltage
  • AGND: Ground for the analog input
  • CLK: Short for Clock, needed for the SPI Communication
  • D_OUT: Output for SPI
  • D_IN: Input for SPI
  • CS/SHDN: Short for cable select, because we can use multiple devices for spi.
  • DGND: Ground for the MCP3008

As we stated previously, you will need to use SPI for the communication. If you are not sure how to enable SPI, look here: After settling any software issues or configuration changes, we need to connect the MCP3008 to the Pi as shown below. To ensure connections are made to the correct pins, remember that the half circle marks the downside of the ADC. Connecting the MCP3008 over the separator in the middle of the breadboard between e and f usually makes for the cleanest and easiest setup.

MCP3008 connected to the raspberry pi
Connections raspberry Pi Connections MCP3008
V_REF 3,3V
VDD 3,3V

Now we will write a program to receive and read the information delivered by the MCP3008 to our Raspberry Pi. Create a new file with the command:


Fill the file with the following code:

import spidev
import time
import os
import RPi.GPIO as gpio

# Start SPI connection
spi = spidev.SpiDev(),0)

# Read MCP3008 data
def analogInput(channel):
  adc = spi.xfer2([1,(8+channel)<<4,0])
  data = ((adc[1]&3) << 8) + adc[2]
  return data

while True:
  print("0: "+str(analogInput(0)))
  print("1: "+str(analogInput(1)))
  print("2: "+str(analogInput(2)))
  print("3: "+str(analogInput(3)))
  print("4: "+str(analogInput(4)))
  print("5: "+str(analogInput(5)))
  print("6: "+str(analogInput(6)))

Press Ctrl + X to exit the file and enter Y to save it when prompted. Next you need to start your program using the following command:

sudo python

It is important that you have superuser/root rights when executing this program. This requires using sudo before the terminal command. Also keep in mind that this a program for the python2 environment. If you haven’t connected any of the components yet there may be some random output. This however will change as soon as you have some useful electrical components connected.

Noch Fragen oder Feedback?

Bevor du eine Frage stellen kannst musst du dich zuerst Anmelden oder Regestrieren!