DAQmx AI Trigger in Python

Updated Feb 16, 2024

Reported In

Driver

  • NI-DAQmx

Programming Language

  • Python

Issue Details

I want to use DAQmx Python AI trigger to trigger some actions upon receiving an AI start trigger. How could I achieve this via Python DAQmx APIs?

Solution

We could always try to refer to the DAQmx Python documentation for APIs usages details.

For the trigger signal source, I think you could follow the steps mentioned in KB: Use SMB Connector on PXI(e) Controller to Route Trigger Signals , to invoke the "cfg_dig_edge_start_trig", and set the source as [nameofthe controller/TRIG_SMB] like what the LV example did in the KB.

Please refer to a simple Python example code below to achieve this:

import pprint
import nidaqmx

pp = pprint.PrettyPrinter(indent=4)

with nidaqmx.Task() as task:
   clk_rate = 2.0e6 # Sampling freq. It is determined by the ext. clk rate. Unit: [Hz] Please note that the maximum sampling freq. of DAQ board(PXIe-6378) is 3.5 MSa/s/ch.
   tr = [-0.1,1.0] # Timerange to plot.
   num_pts = int(clk_rate * (tr[1]-tr[0])) # Total data acquisition time.

   task.ai_channels.add_ai_voltage_chan("PXIe6378/ai0")
   task.timing.cfg_samp_clk_timing(rate=float(clk_rate), samps_per_chan=num_pts) # Configure the clock configuration.
   task.triggers.start_trigger.cfg_anlg_edge_start_trig(trigger_source = 'APFI1', trigger_level=1.5) # Setup the trigger conditions
   print("1 Channel N Sample Read: ")
   data = task.read(number_of_samples_per_channel=num_pts, timeout=100)
   pp.pprint(data)
Note: we're simulating a PXIe-6378 device in NI MAX to run the Python script, please change the arguments of add_ai_voltage_chan for your case.