How to Control NI DAQ Device with Python and NI DAQmx ?

Updated Aug 8, 2018

Reported In

Hardware

  • USB-6008
  • USB-6353
  • USB-6210

Operating System

  • Windows

Programming Language

  • Python

Other

Python
NI-DAQmx python API

Issue Details

I want to read the inputs and write to outputs of NI-DAQ device, using python. How to do it?

Solution

For reading and writing procedure with NI-DAQ device, using Python, you'll need Python and nidaqmx python API.
If you have not installed Python and related Python API then you can directly go to step 5.
In case you have already installed Python and DAQmx python API, then to make sure the installation procedure was correct you should proceed with re-installation. Re-installation procedure is mentioned below.

1) Open Python Interpreter or Python IDLE
  1. Type import os press enter
  2. Type os.getcwd() and press enter
  3. It gives you the directory where python was installed. Take a note of this path.
2) Go to uninstall program and uninstall Python and Python Launcher
 

Figure 1. Uninstall existing Python installation

3) Once Uninstallation is finished then delete the folder which you have noted in step 1.3
4) Now we can proceed for fresh installation
5) Download Python
6) Install python. Custom installation option will let you choose path for installation directory. Choose a easy to access location in C drive, like C:\Program Files (x86)
7) Let installation finish
8) Go to ez_setup.py source code page here
9) Copy everything by CTRL+A, paste it in a new notepad file
10) Save notepad file in easy to access location, like Desktop with name ez_setup.py. Close file
11) Open windows command prompt as Administrator
12) Go to Desktop directory using command cd
13) On command prompt window type ez_install.py and press enter
14) Go to script folder of python installation folder. Path could be like C:\Program Files (x86)\Python36-32\Scripts
15) Go to computer properties, then select Advanced system settings. In Advanced tab click on Environment Variables..
Under system variable, look for PATH. Select this and click on Edit. In Variable value, at the end, add a semicolon and path of script folder as above. For example, add ;C:\Program Files (x86)\Python36-32\Scripts
Also add address of python folder to PATH of environment variable. For example, add at the end ;C:\Program Files (x86)\Python36-32
 

Figure 2. Editing Environment Variables
 
16) Open windows command prompt as Administrator
17) Type easy_install nidaqmx. It will download and install nidaqmx python API and other dependencies also.
18) If you see an error message, asking you to install Visual C++ Build Tools, then please download it from here, and install it. Repeat step 17 again.
19) Now you are ready with the setup.

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

Figure 3. Testing nidaqmx Python API

Additional Information

To understand about other functions and APIs in DAQmx for python, you can go through the complete documentation found here.

WAS THIS ARTICLE HELPFUL?

Not Helpful