This content is not available in your preferred language.

The content is shown in another available language. Your browser may include features that can help translate the text.

Control NI DAQ Device with Python and NI DAQmx

Updated May 14, 2021

Environment

Driver

  • NI-DAQmx

Programming Language

  • Python

I have a USB DAQ system from NI and I want to:
  • Acquire data from my hardware using Python code to do data processing.
  • Use the NI-DAQmx C functions in my Python application.

The NI-DAQmx driver can be downloaded from the NI website, and then the NI-DAQmx Python Documentation explains how to install the Python support. The documentation for the nidaqmx Python package is hosted on the NI Github Sit. It includes all of the nidaqmx Python example programs and the function definitions.

Install Python and the NI-DAQmx Python API:
  1. Download Python.
  2. Install Python.
  3. Running the NI-DAQmx Python API requires NI-DAQmx or NI-DAQmx Runtime. Visit the ni.com/downloads to download the latest version of NI-DAQmx.
  4. Install the NI-DAQmx Python API:
    • It can be installed with pip:
      • $ python -m pip install nidaqmx
    • Or easy_install from setuptools:
      • $ python -m easy_install nidaqmx
    • You also can download the project source and run:
      • $ python setup.py install

Test Python environment with nidaqmx:
  1. Open IDLE(Python)
  2. Type import nidaqmx and press enter
  3. Type with nidaqmx.Task() as task: and press enter
  4. Type task.ai_channels.add_ai_voltage_chan("Dev1/ai0") press enter. Note that Dev1 is the name of the device connected to the computer. You can find this name in NI-MAX. You'll get an error if this name doesn't match. ai0 is the channel number that you want to use to acquire data. Make sure that this is the correct channel name.
  5. Type task.read()and press enter
  6. You should be able to see the value read from the AI0 input of the Dev1 device.

The image above illustrates the output of the steps mentioned above.

 

If you believe your current installation procedure is incorrect, you can do a re-installation.

Re-installation procedure:
Please select the corresponding uninstallation method according to your previous installation approach, and follow by reinstalling it:
  • Uninstall using pip:
    • python -m pip uninstall nidaqmx
  • Uninstall using setuptools:
    1. python easy_install -m nidaqmx
    2. A file path will be provided after perform Step 1. Remove the .egg file in the file path.
  • Remove project source
    • Remove nidaqmx folder within Python's site-packages folder.