Analog Sensors Reading with Raspberry Pi as Interface
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. We need a A/D interface easy to configure in the RPi and the MCP3008 is 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 a MCP3008 to read a light sensor and control/supervise the light inside Zabbix.
- Raspberry Pi
- MCP3008 8 channel ADC
- Light dependent resistor (LDR)
- 10 Kohm resistor
- Some wiring
The MCP3008 read the analog value and give a 10 bits number that is transmitted by the SPI Bus.
The Serial Peripheral Interface bus or SPI bus is a synchronous serial data link standard, that operates in full duplex mode.
Devices communicate in master/slave mode where the master device initiates the data frame. Multiple slave devices are allowed with individual slave select lines.
Sometimes SPI is called a four-wire serial bus, contrasting with three-, two-, and one-wire serial buses. SPI is often referred to as SSI (Synchronous Serial Interface).
To enable hardware SPI on the RPi we need to make a modification to one of the system files:
sudo nano /etc/modprobe.d/raspi-blacklist.conf
Add a ‘#’ character in front of the line spi-bcm2708. Use CTRL-X, then Y, then Return to save the file and exit. Reboot using the following :
To check the change has worked run the following command :
You should see “spi_bcm2708″ listed in the output.
Install Python SPI Wrapper
In this project we are going to use Python and In order to read data from the SPI bus in Python we can install a library called ‘py-spidev’. To install it we first need to install ‘python-dev’ :
sudo apt-get install python-dev
Then to finish we can download ‘py-spidev’ and compile it ready for use :
mkdir py-spidev cd py-spidev wget https://raw.github.com/doceme/py-spidev/master/setup.py wget https://raw.github.com/doceme/py-spidev/master/spidev_module.c sudo python setup.py install
The CH0-CH7 pins are the 8 analogue inputs.
Light Dependent Resistor
I chose a LDR EG & G Vactec, VT43, CdS, 8 Ω to 300 KΩ. 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. In this project has been used a LDR but whatever device changing his resistance depending of some phenomenon can be used and wired in the IC to be controlled.
Reading The Data
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 0-10000 Lux range.
For more detail: Analog Sensors Reading with Raspberry Pi and Zabbix Supervisor
Current Project / Post can also be found using:
- схема подключения MCP3008 к Raspberry PI3