Sensing Analog Signal using Raspberry Pi – RadioStudio
Menu

Sensing Analog Signal using Raspberry Pi

By Surya Igor | Internet of Things

Nov 29
analog-input-banner

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.

Analog Signal

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

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

Potentiometer

potentiometer

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.

Project Setup

Thingspeak

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

  1. Sign up for a free account in Thingspeak
  2. Sign in to your Thingspeak account
  3. Create a new channel on My Channels 5
  4. Fill in the details about the project and save the channel6
  5. Click on API Keys and copy the Write API Key and save it in a text file.
    7

Hardware Setup

Now we are ready to setup the hardware. Follow the schematic and make the connection between the Raspberry Pi, MCP3008 and Potentiometer.

 

circuito-mcp3008-rpi2

3

Make sure that the connection are given as per the schematic.

2

Project Execution

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.

  1. 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”.
  2. Under “rpi-mcp3008-analogData-thingspeak” directory, navigate to the “adcRpi” subdirectory8
  3. 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).
  4. Run the script with following command

    python adcRpi.py

    9

  5. Open your browser and under your Thingspeak account, open the channel & click on private view
  6. 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.

analog_input_1
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.

 

Follow

About the Author

Surya is a hardware buff and tinkerer who loves all the stuff around IoT. He is a specialist in building proof-of-concepts around electronic hardware and conceptualizing use cases and solutions for Internet of Things. When not working with hardware, Surya can still be found fiddling with some crazy hardware design, trying to bring out an idea out of a raw circuit.

>