Using a NI DAQ Device with Python and NI DAQmx in Windows

Updated Aug 29, 2023

Environment

Driver

  • NI-DAQmx

Programming Language

  • Python

This article outlines the steps to configure your Windows PC to use the NI-DAQmx Python API.

Install Python and the NI-DAQmx Python API:
  1. Download and install Python
  2. Open a new command prompt or terminal.
  3. 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
The NI-DAQmx Python Documentation explains how to install the Python support. The documentation for the NI-DAQmx Python package is hosted on the NI GitHub repository. It includes all of the NI-DAQmx Python example programs. Running the NI-DAQmx Python API requires NI-DAQmx or NI-DAQmx Runtime.

Test Python environment with NI-DAQmx:
  1. Open IDLE (Python).
  2. Type import nidaqmx and press enter.
  3. Type with nidaqmx.Task() as task: and press enter.
  4. Make sure to keep the Python indentation (four spaces) and 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 screenshot below illustrates the output of the steps mentioned in the Steps section.

The script below is the test script used in the Steps section of this article.

import nidaqmx
with nidaqmx.Task() as task:
    task.ai_channels.add_ai_voltage_chan("Dev1/ai0")
    task.read()

Next Steps