Interface MCP3008 to the Raspberry Pi to Read Analog Signal

Analog to digital


This tutorial explains how to use the MCP3008 ADC IC to convert analog signal to digital signal and send it to Raspberry Pi via SPI bus.

Overview  

The Raspberry Pi has a set of GPIO (General Purpose Input Output) digital pins that can be used for reading digital logic signals or for outputting digital logic states. However, the Raspberry Pi with its GPIO pins, cannot read analog signals. The absence of analog inputs on the Raspberry Pi can be solved using an analog-digital converter (ADC) like the MCP3008 IC.

In this article, we will go over how to interface the MCP3008 ADC chip with the Raspberry Pi to use it with a range of analog sensors like photo sensors, MQ gas sensors, pulse heart rate sensors, sound sensors, and many more. 

MCP3008

The MCP3008 is a programmable 8-channel 10-bit analog to digital converter with on-chip sample and hold circuitry and SPI serial interface. The function of a sample and hold circuit is to take a sample of the given input signal and hold the sampled value for a short period of time. It is mainly used in analog-to-digital converters like the MCP3008 to eliminate variations in input analog signals that can corrupt the conversion process. MCP3008 uses the SPI (Serial Peripheral Interface) protocol for communication with the devices. The analog input channels of MCP3008 can be configured either as single-ended inputs or as pseudo-differential pairs as per our choice. 


MCP3008 diagram


Interface MCP3008 and Soil Moisture Sensor With Raspberry Pi

The Raspberry Pi is a digital-only computer and doesn't have an integrated analog-digital converter like the Arduino to read analog inputs. Thus, to use analog sensors with the Raspberry Pi, an analog-digital converter like the MCP3008 IC would be required to interface with the Pi to convert any type of analog signal to a digital signal that your Raspberry Pi can process. I will demonstrate this process by interfacing a Soil Moisture sensor and MCP3008 with the Raspberry Pi.

Interface MCP3008 to the Raspberry Pi to read Analog Signal

In this tutorial, the SPI (Serial Peripheral Interface) protocol is used to provide communication between the Raspberry Pi and the MCP3008 IC chip. The SPI serial connection can be made either through a hardware SPI or software SPI.

Hardware SPI 

The first thing to do while using hardware SPI is to enable the SPI device and load the SPI kernel module. Finally reboot the device to ensure that the changes take effect. Run the following command from the terminal to enable the SPI device in your Raspberry Pi.

pi@raspberrypi:~ $ raspi-config

The Raspberry Pi Software Configuration Tool (raspi-config) window will show up. Go to "Interfacing Options" and then select the option to enable the SPI interface. Select "Yes" to prompts asking for "Wheather you want to load theSPI module at boot time as well". Reboot the Pi to ensure that the changes take effect. After rebooting, run the ls -l /dev/spi* command from the terminal to check if you can see "/dev/spidev0.0 and /dev/spidev0.1" as result.

SPI interface

Do the following wiring to connect the MCP3008 chip and Soil Moisture sensor to the Raspberry Pi with a hardware SPI.

Hardware SPI


Software SPI

Do the following wiring to connect the MCP3008 chip and Soil Moisture sensor to the Raspberry Pi with a software SPI. In software SPI, the MCP3008 pin 10 (CS/SHDN), pin 11 (DIN), pin 12 (DOUT), pin13 (CLK) can be connected to any free digital GPIO pins on the Raspberry Pi.

software SPI

Install Required Libraries

To program the MCP3008 ADC and Soil Moisture sensor to the Raspberry PI GPIO, we will need to install the following packages on the Raspberry Pi.

sudo apt-get update
sudo apt-get install build-essential python-dev python-smbus python-pip
sudo pip install adafruit-mcp3008


Python Script

The following code snippet needs to be added to our python code which configures the MCP3008 chip to use the software SPI


# Software SPI Configuration
CLK   = 23
MISO  = 21
MOSI  = 19
CS    = 24
MCP = Adafruit_MCP3008.MCP3008(clk=CLK, miso=MISO, mosi=MOSI, cs=CS)


Similarly, for hardware SPI, use the following code segment for configuration. While using hardware SPI, make sure to enable the SPI interface on the Raspberry Pi.


# Hardware SPI Configuration
SPI_PORT    = 0
SPI_DEVICE = 0
MCP = Adafruit_MCP3008.MCP3008(spi=SPI.SpiDev(SPI_PORT, SPI_DEVICE))


Below is the complete Python code for reading analog value from soil moisture sensor using MCP3008.

Soil Moisture sensor+MCP2008+Raspberry Pi- software spi


Click to download

Soil Moisture sensor+MCP2008+Raspberry Pi- Hardware spi
Click to download

Output Screenshot

Interface MCP3008 to the Raspberry Pi to read Analog Signal


Post a Comment

0 Comments