A continuation of the helpful Building Raspberry Pi Controllers series. Here you'll learn how to read analog data with an Raspberry Pi.

### Previous Articles:

Build a Raspberry Pi Pushbutton Switch

Build Raspberry Pi Controllers: LED Flasher

Build an Object Detection DC Motor Controller

Building Raspberry Pi Controllers: IR Remote Event Counter

### Intro

The RPi (Raspberry Pi) has the ability to perform a variety of electrical-electronic functions such as reading digital data from an electric pushbutton switch, controlling a DC motor, and flashing LEDs. The only electronic function it's not capable of doing is reading electrical signals and data from an analog device. Although the RPi has a multitude of GPIO pins, HDMI, an audio jack, LCD, and camera connectors, it's not capable of reading analog signals because it lacks an ADC (Analog to Digital Converter) circuit. Wiring an ADC to the RPi is quite easy to do, but there is another technique that can be used to read analog electrical signals or data: here, you'll learn how to read electrical signals from an analog device with an RPi using the Step Response technique. The electronic components and circuit required to read analog electrical signals is shown in the block diagram of Figure 1. In addition, a Parts List is provided showing all of the electronic components required to build the RPi project.

### Step Response Technique: Reading Analog Data

As briefly mentioned in the introduction, the RPi isn't able to read electrical analog signals or data. The reason it's not able to read such signals or data is because the RPi's BCM (Broadcom) microcontroller doesn't have an ADC circuit embedded within its silicon substrate. Two alternative methods that can allow the RPi to read electrical analog signals are to wire an external ADC IC (the MCP3008) or use a simple charging-discharging circuit. In this project, you will be using a charging-discharging circuit to read electrical analog signals. The charging-discharging circuit will allow continuous data to be read by the RPi using a step response technique. The step response technique works by seeing how a RC (resistor-capacitor) circuit responds to an electrical pulse switching from a low to high signal transition within a specified time frame. The electrical response behavior of the RC circuit is based on the charging and discharging of the capacitor receiving an electrical pulse or step signal. Figure 2 shows an electrical circuit model of a basic RC circuit.  The applied step signal and the circuit's charging-discharging response is shown in the waveform plots of Figure 3. To create the RC circuit and its step response waveforms, I used a free circuit simulation software package called Micro-Cap.

##### Figure 3.  The step input pulse signal [blue:V(1)(V)] and the RC circuit's charging-discharging response [V(3)(V)]

In addition, as shown in Figure 3, the step input pulse signal is applied to the RC circuit for 5ms (millisecond). The capacitor (C1) is charged during that time through resistors R1 and R2. Once the capacitor is fully charged, at the falling edge of the step pulse signal the capacitor discharges through resistor R3. The discharge time is set for 1ms. This charging and discharging of the capacitor can be accomplished using Python code implemented on an RPI. As this continuous cycle of charging and discharging occurs, a Python code software counter is used to provide an equivalent continuous value of the analog device's data being read by the RPi. The software counter's value is constantly being updated and displayed based on the main application code being cycled continuously. If you're interested in exploring the electrical behavior of this RC circuit, the Micro-Cap simulation code file can be obtained below. I've included two circuit schematic diagrams showing the charging and discharging circuits wired to the RPi. Figures 4 and 5 show the charging and discharging circuits wired to the RPi's GPIO pins 18 and 23. For additional information on RC circuits and how electrical transient responses work, check out Vol. I - Direct Current (DC), Chapter 16: RC and L/R Time Constants.

##### Figure 5. The RPi C1 capacitor discharging circuit interface.

With a basic knowledge of how to read electrical analog signals or data using the step response technique under our belts, let's build a RPi analog device interface circuit!

### Building an RPi Analog Device Interface Circuit

With the electronic components obtained from the Project Parts List, you are now able to wire the RPi analog device interface circuit. Following the parts placement shown in Figure 6, wire the RPi analog device interface circuit on a solderless breadboard. As an additional wiring reference, I included a circuit schematic diagram of the interface circuit for the intermediate to advanced electronics maker.

##### Figure 7. The analog device circuit schematic diagram.

Also, here's my assembled analog device interface circuit you can use as a reference model:

##### Figure 8. The assembled analog device interface circuit built on a solderless breadboard.

Before moving on to the testing stage of the analog device interface circuit, check for wiring errors. If you have no wiring errors, let's load a Python code for checking the analog device interface circuit to the RPi.

### Testing the Analog Read Function and Interface Circuit

You have built your analog device interface circuit and are now ready to test it! A small Python application code is required to ensure the circuit is working correctly. The application code is shown in Listing 1. The Python application code can be typed onto the LXTerminal by opening the nano editor with the linux command ~sudo nano pot_step.py. Also, the Python code can be saved on your RPi's SD card by clicking the code button below.

With the pot_step Python code entered into the LX Terminal, type the Linux command ~sudo python pot_step.py after the prompt onto the screen. A stream of scrolling analog data should be visible on the screen. By rotating the potentiometer, the analog values will change as shown in Figure 9. Congratulations on building your analog device interface circuit! I've included a small video clip showing the operation of this awesome analog device interface circuit for your reference. You can explore this simple unique interface circuit further by replacing the 10Kilo-ohm potentiometer with a photocell. Waving your hand over the photocell will change the detected light displaying its value on the screen.  As always, record your circuit modifications in a lab notebook. Next time, you learn how to wire an LCD (liquid crystal display) to the RPi and display messages using the Python programming language.

##### Figure 9. The potentiometer analog values being displayed on a video monitor screen

                    Listing 1.

####################################
#         Analog Read Code         #
#         by Simon Monk            #
#         modified by Don Wilcher  #
#         Jan 1/31/15              #
####################################

# include RPi libraries in to Python code
import RPi.GPIO as GPIO
import time

# instantiate GPIO as an object
GPIO.setmode(GPIO.BCM)

# define GPIO pins with variables a_pin and b_pin
a_pin = 18
b_pin = 23

# create discharge function for reading capacitor data
def discharge():
GPIO.setup(a_pin, GPIO.IN)
GPIO.setup(b_pin, GPIO.OUT)
GPIO.output(b_pin, False)
time.sleep(0.005)

# create time function for capturing analog count value
def charge_time():
GPIO.setup(b_pin, GPIO.IN)
GPIO.setup(a_pin, GPIO.OUT)
count = 0
GPIO.output(a_pin, True)
while not GPIO.input(b_pin):
count = count +1
return count

# create analog read function for reading charging and discharging data
discharge()
return charge_time()

# provide a loop to display analog data count value on the screen
while True:
time.sleep(1)




Next Article in Series: Building Raspberry Pi Controllers Part 6: Displaying Messages on a LCD with a RPi

Give this project a try for yourself! Get the BOM.

• R2new2 2016-02-12

Hi Don Wilcher, im new to circuits, python and pi, trying to test out this project for a project i have planed.
will this work if i am using a polyester capacitor 220nanoFarad, 63volts with ±10% tolerance? to read any type of potentiometer value with my pi. and is it safe?
thanx

• R2new2 2016-02-12

and using 1 kilo-ohm (Brown, Black, Red, Gold) ±5% resistor is that similar as
1Kilo-ohm resistor (brown, black, brown, gold), 1/4W, 5%

• Don Wilcher 2016-02-13

Hi R2new2
Both values are the same. The capitals vs lower case letters for the color code will not affect the true 1Kilo-ohm value,

• R2new2 2016-02-13

sorry typo
and using 1 kilo-ohm (Brown, Black, Red, Gold) 5% tolerance, resistor is that similar as
1Kilo-ohm resistor (brown, black, brown, gold), 1/4W, 5%

• Don Wilcher 2016-02-13

Hi R2new2
Polyester caps will work along with any potentiometer value.

• Don Wilcher 2016-02-13

Hi Don Wilcher, im new to circuits, python and pi, trying to test out this project for a project i have planed.
will this work if i am using a polyester capacitor 220nanoFarad, 63volts with ±10% tolerance? to read any type of potentiometer value with my pi. and is it safe?
thanx.

Hi R2new2

Hi R2new2
Polyester caps will work along with any potentiometer value.

sorry typo
and using 1 kilo-ohm (Brown, Black, Red, Gold) 5% tolerance, resistor is that similar as
1Kilo-ohm resistor (brown, black, brown, gold), 1/4W, 5%

Hi R2new2
Both values are the same. The capitals vs lower case letters for the color code will not affect the true 1Kilo-ohm value,

• R2new2 2016-02-14

Thank you sir, im testing it after i’ve done following your breadboard arragement. Pi is very very hard to come by, for me and caps resistors and etc. i managed to scavenge from my junk. Hope it works, this is my second attempt. Another thing, is my cap specs safe and working for this project?afraid of damaging broadcom chip, the pi and stuff. my pi is pi 2 model B. I will run it after i get a confirmation, thank you sir

• gargoyle 2016-02-17

Hi
I found the article a little hard to follow. If I may summarize just to make sure I under stand the basics.
- Raspberry Pi does not contain an Analog to Digital converter (A-D).
-  to simulate the functionality of an A-D an RC circuit is used.
- The Raspberry Pi times how long it takes to charge the Capacitor thru a Variable resistor or potentiometer.
- The charge voltage voltage is supplied by the Raspberry Pi GPIO-18
- This is limited to reading variable resistance values from a pot, photo-resistor, or thermister etc.
- This won’t read an externally varying voltage for example from a shunt resistor or external battery, GPIO pins are limited to 3.3V max anyway.
Is this more or less the jist of it?

• pcarew 2016-02-23

Correct, this will simply count the time it takes (which is a function of current flow into the capacitor) for the capacitor to reach a value that is read as a logic high.

It so happens that on 3.3v logic systems, a logic ‘true’ can happen anywhere from 0.8v to 2v. If you tried driving this with an external voltage that is not 3.3v you would have very indeterminate results. If you used a voltage larger than 3.3v, you run the risk of damaging the PI. If you use a voltage that is below 0.8v, you would never read a logic true.

So to cut a long story short, you can’t determine the actual analog voltage, only a relative value for the potentiometer setting.

Still a very useful technique where appropriate.

• Don Wilcher 2016-02-24

You are correct. The intent of this circuit is to read resistive devices that assist in the charging of the external capacitor. As mentioned in the article, to read varying voltage levels requires an external ADC IC like MCP3008 component.

• Don Wilcher 2016-02-24

You are correct. The intent of this circuit is to read resistive devices that assist in the charging of the external capacitor. As mentioned in the article, to read varying voltage levels requires an external ADC IC like MCP3008 component.

• KKW 2016-09-12

Don,
(R1) 1Kilo-ohm resistor (brown, black, brown, gold), 1/4W, 5%
(R2) 1Kilo-ohm resistor (brown, black, brown, gold), 1/4W, 5%
These are 100 ohm resistors.

Brown, Black, Red is a 1K resistor.

• David Jarrett 2017-03-07

This is a very interesting an informative article. Newbie question, if you don’t mind…
How many different inputs can the RPi handle using this type of setup?
Would it be possible to monitor 4 - 6 different input signals simultaneously, or would that type of application need an actual ADC interface?

• Bill Harvey 2017-11-10

Don;
An old post but here goes. Could this circuit be used to monitor voltage levels being output from a toy RC car Rx board. I have tested the board and when connected up to 2 x AA batteries when operating the joystick X and Y axis + and - 2.6v register on a multimeter.

• Jimray2300 2018-03-23

Can one use this converter to take readings from a stepper motor?

• RK37 2018-03-26

What kind of readings are you referring to? Stepper motors are typically driven in an “open-loop” fashion; in other words, there is usually no need to analyze feedback signals from the motor.

• Hecklemore 2019-02-12

Hello Don WIlcher,
Is there a way to tweak this, to read out the signals of a 1.3 Mohm potentiometer from an old radio?
Thanks