Almost all the data captured from real world is in analog form. Analog sensors play an important role in sensing such parameters, be it the temperature of the room or vibrations produced by machines. In this post, we are going to create a small project to show you how to capture analog signal from Raspberry Pi 3 and visualize it for further analysis.
An analog signal is any continuous signal for which the time varying feature (variable) of the signal is a representation of some other time varying quantity, i.e., analogous to another time varying signal (Source Wikipedia).
In most cases, an analog signal is represented by a continuous signal plotted on an x-y graph. The x-axis represents time and y-axis represents the dimension of a physical parameter.
Within the analog world, we use different sensors to measure various physical parameters such as color, chemical reaction, pressure, temperature, position, force, proximity, presence, flow, light, sound, speed, magnetic & electric field, etc. These parameters play a significant role in a large industrial setup where we need to track a multitude of equipment to make the system work efficiently as a whole.
Let’s have a look at the hardware components that we are going to use for capturing analog signal.
Raspberry Pi is the credit card sized computer which has rich peripherals to interface various sensors. The Raspberry Pi does not have inbuilt ADC module but has rich features of serial communication SPI, UART, I2S, I2C, etc. So for applications where analog signal is input, we need an ADC(Analog to Digital Converter) Chip to read analog values from the sensors.
MCP3008 ADC Chip
MCP3008 is a low cost SPI-based 8-channel 10-bit analog to digital Converter. This module is appropriate for the single ended and differential input analog sensors such as LDR, Temperature, 3 Axis accelerometer etc.,
The precision of this ADC module is similar to any of 8 bit micro-controllers and the module provides 8 channels with SPI Interface.
MCP3008 Pin Out
A potentiometer is a simple knob that provides a variable resistance, which we can read as an analog voltage value. It has three pins to which we connect three wires as follows,
- The first goes to ground from one of the outer pins of the potentiometer.
- The second goes from 3.3 volts to the other outer pin of the potentiometer.
- The third goes from MCP3008 analog input 0 to the middle pin of the potentiometer.
By turning the shaft of the potentiometer, we change the amount of resistance on either side of the wiper which connects to the center pin of the potentiometer. This changes the relative “closeness” of that pin to 3.3 volts (Vcc) and ground, giving us a different analog voltage input. When the shaft is turned all the way in one direction, there is 0 volt going to the pin. Turning it all the way in other direction results in 3.3 volts of input going to the pin. When connected to MCP3008, this analog voltage in the range of 0 to 3.3 volts is proportionately sensed as a number between 0 and 1023, therefore giving us a digital output.
For visualizing the data captured through analog signals, we will use Thingspeak. We need a registered account with Thingspeak and set it up for our project.
Setting Up the Thingspeak
- Sign up for a free account in Thingspeak
- Sign in to your Thingspeak account
- Create a new channel on My Channels
- Fill in the details about the project and save the channel
- Click on API Keys and copy the Write API Key and save it in a text file.
Now we are ready to setup the hardware. Follow the schematic and make the connection between the Raspberry Pi, MCP3008 and Potentiometer.
Make sure that the connection are given as per the schematic.
We are now ready to execute the project.
The application software for this program is written in python and is available under GitHub.
Before executing the program, make sure to follow the steps provided in the repository README file to setup Raspberry Pi for SPi communication.
Follow the steps below to execute the project.
- Log into the raspberry pi console and clone the GitHub respository under your home directory. Once cloned you will see a sub directory named “rpi-mcp3008-analogData-thingspeak”.
- Under “rpi-mcp3008-analogData-thingspeak” directory, navigate to the “adcRpi” subdirectory
- Open the file adcRpi.py and modify the Thingspeak API key on Line Number 11. Replace the key string with the one that you generated through your Thingspeak account and saved in text file (Step 5 under “Setting up Thingspeak).
- Run the script with following command
- Open your browser and under your Thingspeak account, open the channel & click on private view
- You will see the graph that captures analog voltage values from the potentiometer. As you move the knob , the graph will start getting updated with new digital values corresponding to the analog voltage.
I encourage you to try your hands on this setup. Also please share your experiences or any particular challenges that you might have faced in executing this setup. If u have any technical doubts then you can post your comment below and I will get back to you.