Example Code

Vibration Continuous Acquisition with Triggering in Python with NI-DAQmx

Products and Environment

This section reflects the products and operating system used to create the example.

To download NI software, including the products shown below, visit ni.com/downloads.

    Hardware

  • cDAQ-9174
  • NI-9234
  • NI-9344

    Driver

  • NI-DAQmx

    Programming Language

  • Python

Code and Documents

Attachment

Description

This Python script allows users to acquire and plot vibration data from a C Series Sound and Vibration Input Module, in addition to using digital edge triggering. The script utilizes NI-DAQmx, NumPy, and Matplotlib.

 

The code begins by instantiating constants such as the sample rate, the samples to acquire, the sampling mode, and the channel and trigger names. The DAQ task is created within a “with” block, as it provides a cleaner, more readable code, in addition to ensuring a proper use of memory resources. Within the task, a channel for the accelerometer is created, the timing of that task is configured, and the trigger is configured. Data is acquired through the “task.read” function in a for loop. Time data is generated using the NumPy linspace function, and both datasets are plotted using Pyplot functions. Overall, this program flow follows the same programming flow that you typically see in NI drivers: open, configure, read or write, and close.

 

If necessary, this script can be modified to accept different measurement and triggering types, longer or unlimited acquisition time, and more. For more information, read the API documentation in the link provided below.

 

 

 

How to Use

To utilize the script:

 

  1. Connect your C Series module and CompactDAQ chassis to your PC.
  2. Open the script in a Python 3 IDE.
  3. Modify the variable, “channel_name”, to the name of the module you are using. This name can be found in NI MAX.
  4. If you are using a trigger, modify the variable, “trig_name”, to the name of the trigger channel you are using.
  5. If you are not using a trigger, delete or comment the “trig_name” variable and the line that contains, “task.triggers.start_trigger.cfg_dig_edge_start_trig(trigger_source = trig_name).”
  6. Modify the variables, “sample_rate” and “samples_to_acquire”, to the sample rate and number of samples per channel necessary for your application, respectively.
  7. Run the script

 

 

Additional Information

This script was built in the Jupyter Notebook environment with a Python 3 kernel.

Related Links

NI-DAQmx Python Documentation

 

NI-DAQmx Python GitHub

 

NumPy Homepage

 

Pyplot Function Overview

 

 

Example code from the Example Code Exchange in the NI Community is licensed with the MIT license.

Contributors