DAC Outputs Tutorial on the Expander Pi

Created 17/06/2017 | A tutorial showing how to use the DAC channels on the Expander Pi

This demo shows how to write to the DAC output channels on the Expander Pi to set the output voltage levels. This demo will generate a 1.5V p-p square wave at 1Hz on channel 1

With your Raspberry Pi switched off install the Expander Pi on the Raspberry Pi GPIO port and insert a CR2032 coin battery into the battery holder.  Using the Expander Pi without a battery installed may damage the Expander Pi and will stop it from appearing on the I2C bus.

We will use the AB Electronics python library to talk to the Expander Pi, to download the library visit our Python Library and Demos knowlege base article.

You will need to enable i2c on your Raspberry Pi, see our other tutorial on i2c: I2C, SMBus and Raspbian Linux

The AB Electronics python library uses another library called python-smbus, you can install it using apt-get with the following commands.

sudo apt-get update

sudo apt-get install python-smbus

With the libraries installed and the Raspberry Pi configured to use i2c we can begin building our project.

 

For this tutorial we will start by creating a new python program file called demo-dacwrite.py. You can use your favourite text editor for writing the program. You can find a complete example of demo_dacwrite.py in the ABElectronics_Python_Libraries/ExpanderPi/demos folder.

At the top of your program you will need to import the DAC class from the ExpanderPi library and time library.

from ExpanderPi import DAC
import time

Now we create an instance of the DAC class with the gain set to 1

dac = DAC(1)

Now we create a loop toggle the output voltage with a 1 second delay between switching from 1.5V to 0V

while True:
    dac.set_dac_voltage(1, 1.5)  # set the voltage on channel 1 to 1.5V
    time.sleep(1)  # wait 1 seconds
    dac.set_dac_voltage(1, 0)  # set the voltage on channel 1 to 0V
    time.sleep(1)  # wait 1 seconds

To run the demo in a console window enter:

python demo_dacwrite.py

The full code for this demo is:

#!/usr/bin/env python

from ExpanderPi import DAC
import time

"""
================================================
ABElectronics Expander Pi | DAC Write Demo
run with: python demo_dacwrite.py
================================================
this demo will generate a 1.5V p-p square wave at 1Hz on channel 1
"""

dac = DAC(1) # create a dac instance with  the gain set to 1

while True:
    dac.set_dac_voltage(1, 1.5)  # set the voltage on channel 1 to 1.5V
    time.sleep(1)  # wait 1 seconds
    dac.set_dac_voltage(1, 0)  # set the voltage on channel 1 to 0V
    time.sleep(1)  # wait 1 seconds

 

Shop

Support

Products added to basket

Items

Total (inc shipping)

CheckoutView Basket

Free Shipping on all orders over £30

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.