Tuesday, 22 October 2013

Measuring DC voltages on the Raspberry Pi using the ADC Pi module

The Raspberry Pi does not have a built in analogue to digital converter (ADC) so a suitable “external” ADC module had to be found. I chose the ADC Pi module from Abelectronics because of its multiple 18 bit input channels and its i2c interface. It also had the advantage of having a “stackable” design so that once connected to the Raspberry Pi’s GPIO header, it allowed other modules to be stacked on top.

Although this post deals with how to use ADC Pi module in the context of the Raspberry Pi Wobbulator, the information contained here is applicable to ANY situation where you want to use the Raspberry Pi to measure a DC voltage (e.g. analogue sensors like LDR's, thermistors etc). The ADC Pi module is extremely versatile.

All 8 input channels on the ADC Pi  module have a built-in potential divider giving an effective input voltage range of approximately 0 to 5V DC. However considering the low level output of the DDS module, I decided to remove the potential divider on Channel 1 and connect the input directly to the ADC chip, thus reducing the input voltage range to approximately 0 to 2V DC. Some right angled PCB pins were also soldered to the input terminals on the ADC Pi module to provide easy connection of “flying leads”.

In the photo above the input pins that I added are on the left and the pins on the right are the “address select” pins for the onboard ADC chip (but you really don't need to worry these - just leave the jumpers where they are). The modified input to Channel 1 can clearly be seen in the above photo - apologies for the crude soldering - I have since learned how to solder surface mount components, and bought a fine tipped iron...

The ADC Pi module has an onboard programmable gain amplifier (PGA) for each input channel with a user selectable gain of 1x, 2x, 4x and 8x, effectively increasing the sensitivity of the input channels by a factor of up to 8. Thus the input voltage range of Channel 1 will be approximately 0 to 250mV DC if the maximum gain is selected, which is of the same order of magnitude as the peak output from the DDS module. Abelectronics provide some sample Python software for communicating with and controlling the ADC Pi module. This software is available on GitHub and gives examples of how to take readings from the ADC Pi module and how to specify the input channel and select the gain of the PGA. The ADC Pi module uses I2C protocol to communicate with the Raspberry Pi, and this uses GPIO pins 3 and 5 (GPIO2 and GPIO3 on the Raspberry Pi Rev 2 board). However, I2C is not enabled by default on the Raspberry Pi, so there are a few preparatory tasks you need to perform before you can connect and use the ADC Pi module:

Open the file “/etc/modprobe.d/raspi-blacklist.conf” in Leafpad, go to the end of the file and "comment out" the last line by inserting a ‘#’ at the start of the line so that it looks like as shown below. Then save and close the file but please note that you will need root privileges to do so.

Open the file “/etc/modules” in Leafpad and add the line "i2c-dev" to the end of the file so that it looks like as shown below. Then save and close the file.

Now you need to install the “i2c-tools” package. Entering the following at the command prompt and press Enter:

pi@raspberrypi ~ $ sudo apt-get install i2c-tools_

Add your user name to the i2c group by entering the following at the command prompt (assuming your user name is “pi”) and pressing  Enter:

pi@raspberrypi ~ $ sudo adduser pi i2c_

Now it's time to power down your Raspberry Pi and plug the ADC Pi module into the GPIO header.

After you reboot your Raspberry Pi, enter the following at the command line prompt and press  Enter:

pi@raspberrypi ~ $ i2cdetect -y 1_

This should detect the presence of the ADC Pi module and display the address of the i2c bus as shown below.

The ADC Pi module uses the Quick2Wire library to allow easy access to the I2C port from Python 3. You need to download and install the Quick2Wire library, but first you need to download and install Setuptools.

Download the most up-to-date version of Setuptools (currently v1.1.6) from the Python website. Extract the “.tar.gz” package, go to the folder called “/setuptools-1.1.6” and enter the following at the command line prompt and press Enter:

.../setuptools-1.1.6 $ sudo python3 ez_setup.py_

Now download the Quick2Wire Python API package from GitHub. Extract the ".zip" file, go to the folder called “/quick2wire-python-api-master” and enter the following at the command line prompt and press Enter:

.../quick2wire-python-api-master $ sudo python3 setup.py install_

Download the "adcpiv2" sample code from GitHub. Launch IDLE3 and open the file "adcpiv2.py" and run it by selecting "Run->Run Module (or by pressing "F5"). The program continually takes readings from channel 1 on the ADC Pi module and displays the results on the Python Shell window. The results shown in the screenshot below were obtained by connecting a “AA” battery to the input of channel 1 on the ADC Pi module while the program was running.

 Congratulations! You've just made the world's most complex battery tester! You can stop the program running by pressing "Ctrl + C" when the Python Shell window is active.

So now we have a Raspberry Pi capable of generating an RF signal (see my previous post) and measuring an analogue DC signal. The final part of hardware required for the Raspberry Pi Wobbulator is some sort of detector to convert the RF coming out of the circuit under test into an analogue DC signal. That will be the subject of my next post.


  1. Just found your pages via the Pi forum.
    Interesting stuff. I'm not into radio stuff, but as a physicist was interested in a lot of what you are doing.
    Gave me lots of ( further) ideas of things to play with on my Pis!
    JohnF ( tenochtitlanuk on the forums)

    1. Thanks John - glad you found my blog post of interest!