I am having trouble using Python and the nidaqmx library to properly trigger an analog input channel to read N values. I have two analog input channels, ai0 and ai1. Channel ai0 is the trigger channel that reads 9V until the circuit is connected and the voltage goes to 3V. At that moment, I want to read N samples from channel ai1. I want to repeat this process for 1 minute. I am using an NI USB-6361 daq.
My code is below and the problem is my code does not wait to execute the read task until the trigger is satisfied. The trigger is configured correctly because if I remove ai1 task, the program will read N samples from channel ai0 once the voltage drops. However, I want to read the signal from ai1 but use ai0 as the trigger. Hope this makes sense and thanks for your help.
import nidaqmx
num_samples = 1000;
s_freq = 1e3;
tend = num_samples/s_freq;
#read from DAQ
def readdaq():
task = nidaqmx.Task()
task.ai_channels.add_ai_voltage_chan("Dev1/ai0",max_val=10, min_val=0)
task.triggers.reference_trigger.cfg_anlg_edge_ref_trig("Dev1/ai0", pretrigger_samples = 10, trigger_slope=nidaqmx.constants.Slope.FALLING, trigger_level = 5)
task.stop()
task.close()
task = nidaqmx.Task()
task.ai_channels.add_ai_voltage_chan("Dev1/ai1",max_val=10, min_val=0)
task.timing.cfg_samp_clk_timing(s_freq, sample_mode=nidaqmx.constants.AcquisitionType.FINITE, samps_per_chan=num_samples)
task.start()
value = task.read(number_of_samples_per_channel=num_samples)
task.stop()
task.close()
return value