Analogue Sensors On The Raspberry Pi Using An MCP3008
Analogue Sensors On The Raspberry Pi Using An MCP3008
Analogue Sensors On The Raspberry Pi Using An MCP3008
89
BY MATT ON OCTOBER 20, 2013PYTHON, SENSORS, TUTORIALS & HELP
The Raspberry Pi has no built in analogue inputs which means it is a bit of a pain
to use many of the available sensors. I wanted to update my garage security
system with the ability to use more sensors so I decided to investigate an easy
and cheap way to do it. The MCP3008 was the answer.
This article explains how to use an MCP3008 device to provide 8 analogue inputs which
you can use with a range of sensors. In the example circuit below I use my MCP3008 to
read a temperature and light sensor.
Raspberry Pi
MCP3008 8 channel ADC
Light dependent resistor (LDR)
TMP36 temperature sensor
10 Kohm resistor
The first step is enabling the SPI interface on the Pi which is usually disabled by default.
Please follow my Enabling The SPI Interface On The Raspberry Pi article to setup SPI and
install the SPI Python wrapper.
Circuit
The following list shows how the MCP3008 can be connected. It requires 4 GPIO pins on
the Pi P1 Header.
VDD 3.3V
VREF 3.3V
AGND GROUND
CS GPIO8 (P1-24)
DGND GROUND
When it is dark the LDR resistance increases resulting in the output voltage increasing
towards 3.3V.
So 0 degrees will give 0.5V and 100 degrees will give 1.5V.
1 #!/usr/bin/python
2
import spidev
3 import time
4 import os
5
6 # Open SPI bus
7 spi = spidev.SpiDev()
8 spi.open(0,0)
9
# Function to read SPI data from MCP3008 chip
10 # Channel must be an integer 0-7
11 def ReadChannel(channel):
12 adc = spi.xfer2([1,(8+channel)<<4,0])
13 data = ((adc[1]&3) << 8) + adc[2]
14 return data
15
# Function to convert data to voltage level,
16 # rounded to specified number of decimal places.
17 def ConvertVolts(data,places):
18 volts = (data * 3.3) / float(1023)
19 volts = round(volts,places)
return volts
20
21 # Function to calculate temperature from
22 # TMP36 data, rounded to specified
23 # number of decimal places.
24 def ConvertTemp(data,places):
25
26 # ADC Value
# (approx) Temp Volts
27 # 0 -50 0.00
28 # 78 -25 0.25
29 # 155 0 0.50
30 # 233 25 0.75
31 # 310 50 1.00
# 465 100 1.50
32 # 775 200 2.50
33 # 1023 280 3.30
34
35 temp = ((data * 330)/float(1023))-50
36 temp = round(temp,places)
return temp
37
38 # Define sensor channels
39 light_channel = 0
40 temp_channel = 1
41
42 # Define delay between readings
43 delay = 5
44
45
46
47
48
49
while True:
50
51 # Read the light sensor data
52 light_level = ReadChannel(light_channel)
53 light_volts = ConvertVolts(light_level,2)
54
55 # Read the temperature sensor data
56 temp_level = ReadChannel(temp_channel)
temp_volts = ConvertVolts(temp_level,2)
57 temp = ConvertTemp(temp_level,2)
58
59 # Print out results
60 print "--------------------------------------------"
61 print("Light: {} ({}V)".format(light_level,light_volts))
print("Temp : {} ({}V) {} deg C".format(temp_level,temp_volts,temp))
62
63 # Wait before repeating loop
64 time.sleep(delay)
65
66
67
68
69
Here is a screen-shot of the output :
You can download this script directly to your Pi using :
wget https://bitbucket.org/MattHawkinsUK/rpispy-
misc/raw/master/mcp3008/mcp3008_tmp36.py
Alternatively if you are a Git fan you can clone my Raspberry Pi misc scripts repo using :
The exact reason why you do the above is explained in the datasheet but that is outside
the scope of this article.