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.
The MCP3008 is a 10bit 8-channel Analogue-to-digital converter (ADC). It is cheap, easy to connect and doesn’t require any additional components. It uses the SPI bus protocol which is supported by the Pi’s GPIO header.
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.
Here are the bits I used :
- 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.
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 CLK GPIO11 (P1-23) DOUT GPIO9 (P1-21) DIN GPIO10 (P1-19 CS GPIO8 (P1-24) DGND GROUND
The CH0-CH7 pins are the 8 analogue inputs.
Here is my breadboard circuit :
It uses CH0 for the light sensor and CH1 for the TMP36 temperature sensor. The other 6 inputs are spare.
Light Dependent Resistor
I chose a nice chunky LDR (NORPS-12, datasheet). Under normal lighting its resistance is approximately 10Kohm while in the dark this increases to over 2Mohm.
When there is lots of light the LDR has a low resistance resulting in the output voltage dropping towards 0V.
When it is dark the LDR resistance increases resulting in the output voltage increasing towards 3.3V.
TMP36 Temperature Sensor
The TMP36 temperature sensor is a 3 pin device (datasheet). You can power it with 3.3V and the middle Vout pin will provide a voltage proportional to the temperature.
A temperature of 25 degrees C will result in an output of 0.750V. Each degree results in 10mV of output voltage.
So 0 degrees will give 0.5V and 100 degrees will give 1.5V.
Reading The Data Using a Python Script
The ADC is 10bit so it can report a range of numbers from 0 to 1023 (2 to the power of 10). A reading of 0 means the input is 0V and a reading of 1023 means the input is 3.3V. Our 0-3.3V range would equate to a temperature range of -50 to 280 degrees C using the TMP36.
For more detail: Analogue Sensors On The Raspberry Pi Using An MCP3008