12th Anniversary Sale 12% Off in July & August with the voucher code "SUMMER12" *excludes trade customers
Support Forum

Share your projects and post your questions

Register   or   Sign In
The Forum

DAC Dynamics Write

The Expander Pi is an Analogue, Digital and RTC Expansion Interface for the Raspberry Pi

02/11/2021

Posted by:
Zsombor98

Zsombor98 Avatar

Hi!


Can you help me? I've looked the whole internet for my question but i cant find the solution. So i bought the Expander pi and your example codes are vry useful to me because im new to raspberry and python. For my application first of all i would like to write the value of the voltage on one of my ADC channel to the DAC. With the help of your codes i can easily read the ADC channels, but i cant deal with the DAC... Only i found to set the DAC channel to a value but how can i dinamically write these channels?

Many thanks to your answer!



Zsombor

02/11/2021

Posted by:
andrew

andrew Avatar

Location:
United Kingdom

andrew Twitter  andrew Website  

Hi Zsombor

The easiest way to do what you want would be to read the ADC value into a variable using the read_adc_voltage() function and write the variable to the DAC using the set_dac_voltage() function.

The code below reads a value from the ADC channel 1 and writes it to the DAC channel 1.


#!/usr/bin/env python

import time
import ExpanderPi

def main():
'''
Main program function
'''
adc = ExpanderPi.ADC() # create an instance of the ADC
dac = ExpanderPi.DAC(2) # create an instance of the DAC with the gain set to 2 so the maximum voltage is 4.095V

# set the ADC reference voltage. this should be set to the exact voltage
adc.set_adc_refvoltage(4.096)

print("Updating DAC from ADC")

while True:

# read the adc voltage on channel 1 to a variable
involt = adc.read_adc_voltage(1, 0)

# write the variable to the dac on channel 1
dac.set_dac_voltage(1, involt)

time.sleep(0.1)

if __name__ == "__main__":
main()


The ADC voltage reference is set to a value of 4.096 which should match the voltage of the reference on the Expander Pi. The DAC is set to use a gain of 2 which gives it a voltage range of 0 to 4.095V. This allows the DAC to have the same voltage range as the ADC so any voltage between 0 and 4.095V on the ADC input will be sent to the DAC output.

A variable called involt is used to store the value from the ADC before writing it to the DAC.

I hope this helps you with your problem.

Sign in to post your reply


Note: documents in Portable Document Format (PDF) require Adobe Acrobat Reader 5.0 or higher to view.
Download Adobe Acrobat Reader or other PDF reading software for your computer or mobile device.