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.
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.
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.
Do the following wiring to connect the MCP3008 chip and Soil Moisture sensor to the Raspberry Pi with a hardware SPI.
Software SPI
Install Required Libraries
sudo apt-get update
sudo apt-get install build-essential python-dev python-smbus python-pip
sudo pip install adafruit-mcp3008
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 ConfigurationCLK = 23MISO = 21MOSI = 19CS = 24MCP = 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.
0 Comments