05-27-2022 03:58 PM
Hello,
I'd like to generate waveforms using my myDAQ using the Python ni-daqmx api. In general I am trying to learn how to control DAQ's via the python API in general. I found a really good AI example here that is good for code structure and gave me a basic idea for Analog Inputs-> https://forums.ni.com/t5/Example-Code/Python-Voltage-Continuous-Input-py/ta-p/3938650 and was hoping to find a good demonstration for AO.
The best examples I have been able to find for Analog Output is something like this-> https://forums.ni.com/t5/Multifunction-DAQ/Make-a-40kHz-square-wave-on-analog-output-with-Python/td-... He commits a Python sin and calls a list, a list. After fixing that however I am still having issues with setting my sample rate. This forum post is also doing something similar to what I want but his solution isn't completely clear and hasn't helped my case unfortunatly. https://forums.ni.com/t5/Multifunction-DAQ/Python-NIDAQmx-Analog-output-with-USB-6001-cannot-se/td-p...
I can verify that the clock rate is being changed with "print( task.timing.samp_clk_rate)". However, when trying manually set the sample rate no output is detected on my volt meter and the script finishes almost immediately. When switched into auto the script runs nearly as expected and I can verify the output on my volt meter. (Nearly because it seems to run longer than expected, I don't know why but I'll worry about that later)
Below is my version of the code, yes the for loops are an eye sore, but I was really thinking I would have this worked out quickly and then move on with my life!
Thanks for any help.
import nidaqmx
rate = 1000 # Sample rate in Hz
duration = 5 # Duration in seconds
samples = int(rate * duration)
physical_channel = "myDAQ1/ao1"
a = []
for x in range(samples//5):
a.append(5)
for x in range(samples//5):
a.append(2)
for x in range(samples//5):
a.append(3)
for x in range(samples//5):
a.append(4)
for x in range(samples//5):
a.append(1)
#print(a)
with nidaqmx.Task() as task:
task.ao_channels.add_ao_voltage_chan(physical_channel)
task.timing.cfg_samp_clk_timing(rate)# sample_mode=nidaqmx.constants.AcquisitionType.CONTINUOUS)#,samps_per_chan = samples)
task.write(a, auto_start=False)
task.start()