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.
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. 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.
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!
Micro-Cap Simulation Code
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. 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.
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. 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.