Introduction: How to Take Analog Readings on the Raspberry Pi

About: Electronic hobbyist : Love to explore the field of applied electronics and embedded systems with a motive to contribute and share creative ideas. Green Energy enthusiast

Hello everyone! In this tutorial I am going to show you how can we directly capture analog values using the Raspberry Pi. As we all know that the Raspberry Pi is quite a powerful mini computer module which is popular among hobbyists and professionals and has nearly all the features that any electronic enthusiast wants. However the only drawback of the pi is the lack of a dedicated analog to digital converter hardware, which makes the Pi unsuitable for directly recording the analog values from any sensor. The solution to this is to either use an Arduino in connection with the Pi or to use a dedicated ADC. For this project I will be using the MCP3204-12 bit ADC.

Supplies

  • Raspberry Pi ( you can use any model that you have available)
  • MCP3204 ADC or MCP3008 ADC
  • Analog Sensor (I am using a 10K potentiometer instead)
  • Breadboard
  • Jumper Wires

Step 1: Taking Values From the Arduino Instead....

One alternative to get the analog values to the raspberry pi is to use the arduino which has a dedicated 10 bit ADC. The arduino and the Raspberry Pi can communicate over the serial port to transmit the information. This method can be used when you are experimenting with some sensor data and at the same time you want to utilize the processing power of the Pi. The drawback of this configuration is that you would be utilizing more hardware resources and would also have to write separate codes for arduino and the Pi.

Step 2: Using an ADC..

The alternative to using the Arduino as the ADC is to use a dedicated ADC IC that serves the same purpose. For this project I will be using the MCP3204 IC which is a 4 channel 12 bit ADC which can communicate with the Raspberry Pi using the SPI Protocol. Foe demonstration purposes I will be using the IC in the 10 bit mode.

I have attached the pinout of this IC showing the pin description.

Step 3: Connecting Up the Raspberry Pi and the ADC

Now that we have our hardware sorted, let's get into the connection scheme of the ADC and the Pi.

The Raspberry Pi had 2 SPI interfaces: SPI0 and SPI1. For our application we would be using the SPI0 and we will be using the physical(or hardware) SPI where we connect the ADC to the specific hardware SPI pins of the Pi

I have attached the Pinout of the Pi and the circuit diagram that I have used in the project

The connection scheme is as follows:

  • VDD(Pin14) and Vref(Pin13) of the ADC to the 5V supply of the Pi
  • DGND(Pin7) and AGND(Pin12) of the ADC to the ground of the Pi
  • The CLK(Pin11) of the ADC to GPIO 11 (Physical pin 23) of the Pi
  • The Dout(Pin10) of the ADC to GPIO 9 (Physical pin 21) of the Pi
  • The Din(Pin 9) of the ADC to GPIO 10 (Physical pin 19) of the Pi
  • The Chip Select (Pin 8) of the ADC to GPIO 8 (Physical pin 24) of the Pi

Step 4: Final Setup and the Code..

Now that all the power and communication connections have been made,it's time to attach any sensor whose value we wish to see. I an using a 10K potentiometer as a sensor.

The codes have been written in two parts, the first code is pretty much about setting up the libraries, enabling the SPI communication and then acquiring the ADC value from the MCP3204 , then printing it out on the python terminal.

The second code is more interactive and creates a graph of the real time data that is coming from the sensor.

You can play around with the code and make it suitable for your needs.

Step 5: Instruction Video

This is the video describing in detail all the necessary steps to implement this project. I hope this was helpful !

Robotics Contest

Participated in the
Robotics Contest