Knowledge Base

The AB Electronics UK Knowledge Base provides support solutions, tutorials and troubleshooting guides.

Knowledge Base

ADC DAC Pi Python Library

ADCDACPi Python Library Installation and Usage

Created 29/07/2024 | Last Updated: 30/07/2024

Python Library to use with ADC DAC Pi Zero Raspberry Pi development board.

The example python files can be found in /ABElectronics_Python_Libraries/ADCDACPi/demos.

Downloading and Installing the library

To download to your Raspberry Pi type in the terminal:

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

To install the python library navigate into the ABElectronics_Python_Libraries folder and run:

For Python 2.7:

sudo python setup.py install

For Python 3:

sudo python3 setup.py install

If you have PIP installed you can install the library directly from GitHub with the following command:

For Python 2.7:

sudo python2.7 -m pip install git+https://github.com/abelectronicsuk/ABElectronics_Python_Libraries.git

For Python 3:

sudo python3 -m pip install git+https://github.com/abelectronicsuk/ABElectronics_Python_Libraries.git

Functions:

Initialising the ADCDACPi object.

adcdac = ADCDACPi(gain_factor)

Parameter: gain_factor - 1 or 2 When the gain is set to 1 the voltage range of the DAC will be 0 to 2.048V. When the gain is set to 2 the voltage will be 0 to 3.3V

read_adc_voltage(channel, mode) 

Read the voltage from the selected channel on the ADC
Parameter: channel - 1 or 2
Parameter: mode - 0 = single-ended, 1 = differential
Returns: number as a float between 0 and 2.048

read_adc_raw(channel, mode) 

Read the raw value from the selected channel on the ADC
Parameter: channel - 1 or 2
Parameter: mode - 0 = single-ended, 1 = differential
Returns: int

set_adc_refvoltage(voltage)

Set the reference voltage for the analogue to digital converter.
The ADC uses the raspberry pi 3.3V power as a voltage reference so using this method to set the reference to match the exact output voltage from the 3.3V regulator will increase the accuracy of the ADC readings.
Parameters: voltage
Returns: null

set_dac_voltage(channel, voltage)

Set the voltage for the selected channel on the DAC. The DAC has two gain values, 1 or 2, which can be set when the ADCDACPi object is created. A gain of 1 will give a voltage between 0 and 2.047 volts. A gain of 2 will give a voltage between 0 and 3.3 volts.
Parameters: channel - 1 or 2, voltage - target DAC voltage
Returns: null

set_dac_raw(channel, value)

Set the raw value for the selected channel on the DAC
Parameters: channel - 1 or 2, value int between 0 and 4095
Returns: null

Usage

To use the ADC DAC Pi library in your code you must first import the library:

from ADCDACPi import ADCDACPi

Next you must initialise the ADCDACPi object and set a gain of 1 or 2 for the DAC:

adcdac = ADCDACPi(1)

Set the reference voltage.

adcdac.set_adc_refvoltage(3.3)

Read the voltage from channel 2 and display it on the screen

print adcdac.read_adc_voltage(2)