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

Step 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.

Step 2: Follow the instructions on how to install and configure I2C on Raspbian Linux.

Step 3: Follow the instructions on how to install and configure SPI on Raspbian Linux.

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/ folder.

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

from ABE_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/python

from ABE_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.