or

# DAC Dynamics Write

100 Views - Created 27 days ago

27 days ago

Posted by:
Zsombor98

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?

Zsombor

27 days ago

Posted by:
andrew

Location:
United Kingdom

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
'''
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

while True:

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

AB Electronics UK Tech Support