ADCDAC Pi with Python

Created 02/10/2015 | ADCDAC Pi with Python on a Raspberry Pi

In this tutorial we will read from the input pins on the ADCDAC Pi and set a voltage on the output pins. 

The ADCDAC Pi uses the SPI port and we will be using the py-spidev python module, if you have not installed these already you can follow our SPI and Raspbian Linux on a Raspberry Pi tutorial to enable the SPI port on your Raspberry Pi.

We will use the AB Electronics python library to talk to the ADCDAC Pi, you can download the library from github using the following command in a terminal, and this will download the full python library into the currently selected folder.

git clone https://github.com/abelectronicsuk/ABElectronics_Python_Libraries.git

You will want to tell python where you download the AB Electronics python library by adding it into PYTHONPATH.

export PYTHONPATH=${PYTHONPATH}:~/Desktop/ABElectronics_Python_Libraries/ADCDACPi/

If you downloaded it to somewhere other than your desktop you will need to change the path to match your download destination.

Now all of the libraries are installed and the Raspberry Pi is configured to use SPI we can begin building our first project.

Reading from the ADC

The ADCDAC Pi contains two inputs connected to a 12 bit MCP3202 ADC chip and two outputs connected to a 12 bit MCP4822 DAC chip.

We will start by importing the ADCDACPi python library and the time library.  The ADCDACPi library is used for communicating with the ADCDAC Pi on your Rasbperry Pi.  Open a new text document in your favourite text editor and type in the following at the top of the page.

#!/usr/bin/python
from ABE_ADCDACPi import ADCDACPi
import time

Next we will create an instance of the ADCDACPi class and call it adcdac.  The ADCDAC class includes the ability to set the gain for the DAC during initialisation, a value of 1 or 2 is allowed.  In this instance as we are not using the DAC we will set the gain to be 1. 

adcdac = ADCDACPi(1)

The MCP3202 ADC uses the Vdd pin as a voltage reference and is connected to the 3.3V pin on the Raspberry Pi.  This means that the maximum voltage you can measure is the same as the voltage on the 3.3V pin.  In an ideal world the voltage on the 3.3V pin would be exactly 3.3V but due to slight variations in the tolerances of the components used on the Raspberry Pi and also depending on what else is connected to the GPIO port this voltage can vary by a small amount.  For this reason you can set the reference voltage using a function called set_adc_refvoltage().  In this tutorial we will assume that the voltage is exactly 3.3V but when you use the ADCDAC Pi in your own project we would recommend using a multimeter to measure the voltage between the 3.3V pin and ground and use that value to increase the accuracy of the ADCDAC Pi.

Add in the following code to set the reference voltage for the ADC inputs.

adcdac.set_adc_refvoltage(3.3)

Now we have the ADCDAC Pi set up for reading from the inputs we can create a while loop which reads a value from IN1 and prints it to the screen.  To do this we will use a function called read_adc_voltage() which reads the value of the specified input and returns it as a number.  To read from IN1 you would use read_adc_voltage(1,0), to read from IN2 use read_adc_voltage(2,0). 

We will add a 0.5 second delay between each read to make it easier to view the output.

while True:
   print adcdac.read_adc_voltage(1, 0)
   time.sleep(0.5)

Save the python script as demo-adcread.py and run it in a terminal using the following command.

python demo-adcread.py

If everything works correctly you should start seeing a list of numbers appear.  If you do not have anything connected to the inputs it should return 0.00 or something very close to 0.  Connect a voltage source such as a 1.5V battery between the IN1 and GND pin and see if the output changes on the screen.

 

Writing to the DAC

Now we know how to read a voltage from the inputs we will try setting a voltage on the output pins.  The 12 bit MCP4822 DAC chip contains an internal 2.048V voltage reference so by default we can set the output voltage to be between 0V and 2.048V. The DAC also includes a gain control which allows you to choose a gain of 1 or 2.  With a gain of 1 the output voltage will be 0V and 2.048V, with a gain of 2 the output will be between 0 and the supply voltage which in this case is 3.3V.

As with the read voltage tutorial above we will begin by importing the ADCDACPi python library as well as the time and RPi.GPIO libraries. 

#!/usr/bin/python
from ABE_ADCDACPi import ADCDACPi
import time
import RPi.GPIO as GPIO

The ADC DAC Pi uses GPIO pin 22 to control the LDAC pin on the DAC.  For normal operation this pin needs to be kept low.  To do this we will use the RPi.GPIO library to set pin 22 as an output and make it low.

GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(22, GPIO.OUT)
GPIO.output(22, False)

Next we will create an instance of the ADCDACPi class, call it adcdac and set a DAC gain of 1.

adcdac = ADCDACPi(1)

To set the voltage on an output pin we will use a function called set_dac_voltage(channel, value).  The set_dac_voltage takes two parameters, the output channel and the value to set.  We will create a while loop which sets the voltage to 1.5V, waits 0.5 seconds, sets it to 0V and waits another 0.5 seconds.  When run this will create a 1Hz square wave output on the O1 pin.

while True:
   adcdac.set_dac_voltage(1, 1.5)
   time.sleep(0.5)
   adcdac.set_dac_voltage(1, 0)
   time.sleep(0.5)

Save the file as demo-dacwrite.py and run it in a terminal using the following command.

python demo-dacwrite.py

If you connect a multimeter between the O1 pin and ground you should see it alternate between 0V and 1.5V, if you have an oscilloscope you can connect that to the O1 pin and see a square wave on the display.  Try changing the sleep values to adjust the speed of the square wave.

When you connect your multimeter or oscilloscope to the ADCDAC Pi ensure that you do not accidently short out the O1 or O2 pins to the ground pins.  At best this will cause the Raspberry Pi to reboot, at worst it could damage the ADCDAC Pi or the Raspberry Pi.

 

Shop

Support

Click to contact us.
X

Have any Questions?

Send us any questions about this page or product and we will reply within 24 hours.

Submit your Message

X

Have any Questions?

Thank you for contacting us.

We will respond to your enquiry in the next 24 hours.

You can find answers to many questions and additional product support in our Knowledge Base and the Community Forum.