DAQ With Python
DAQ With Python
blog
Hans-Petter Halvorsen
Free Textbook with lots of Practical Examples
https://www.halvorsen.blog/documents/programming/python/
Additional Python Resources
https://www.halvorsen.blog/documents/programming/python/
Contents
• How can we use NI Hardware with Python?
• What is DAQ?
• TC-01 Thermocouple Device
• DAQmx Note! The Python Examples provided will work for all
NI-DAQ Devices using the NI-DAQmx Driver, which
nidaqmx Python API Python Library/API for Communication with NI DAQmx Driver
Free
NI DAQ
In this Tutorial we will use NI TC-01 Thermocouple
Hardware
LabVIEW
• In this Tutorial we will use Python and
not LabVIEW
• But if you want to learn more about
LabVIEW, you may take a look at my
LabVIEW resources:
• https://halvorsen.blog/documents/prog
ramming/labview/labview.php
NI DAQ Hardware Some Examples
TC-01 Thermocouple
myDAQ
NI-DAQmx
USB-6001 Hardware Driver
cDAQ
USB-6008
Note! The Python Examples provided will work for all NI-DAQ Devices using the NI-DAQmx Driver, which is several hundreds different types
NI USB TC-01 Thermocouple
Connect
to PC
Connect
Thermocouple
Sensor https://www.ni.com/en-no/support/model.usb-tc01.html
NI USB TC-01 Thermocouple
Sensors Digital IO
(Analog/Digital
Interface) Data Acquisition PC
Hardware
Digital Signals
A computer can only deal with discrete signals
You typically log data at specific intervals
𝑡 = Continuous Time
𝑘 = Discrete Time
𝑇! = Sampling Time
𝑇!
With MAX you can make sure your DAQ device works as expected before you start using it in your Python program.
You can use the Test Panels to test your analog and digital inputs and outputs channels.
nidaqmx Python API
• Python Library/API for Communication with NI
DAQmx Driver
• Running nidaqmx requires NI-DAQmx or NI-
DAQmx Runtime
• Visit the ni.com/downloads to download the
latest version of NI-DAQmx
• nidaqmx can be installed with pip:
pip install nidaqmx
• https://github.com/ni/nidaqmx-python
nidaqmx Python Package
Installation using PIP
nidaqmx Python Package
MAX
task = nidaqmx.Task()
task.ai_channels.add_ai_thrmcpl_chan("TC01/ai0")
task.start()
value = task.read()
print(round(value,1))
task.stop()
task.close()
For Loop import nidaqmx
import time
Ts = 5 # Sampling Time
N = 60
for k in range(N):
value = task.read()
print("T =", round(value,1), "[degC]")
time.sleep(Ts)
# Initialize Logging
Tstop = 60 # Logging Time [seconds]
Ts = 2 # Sampling Time [seconds]
N = int(Tstop/Ts)
data = []
# Plotting
t = np.arange(0,Tstop,Ts)
plt.plot(t,data, "-o")
plt.title('Temperature')
plt.xlabel('t [s]')
plt.ylabel('Temp [degC]')
plt.grid()
Tmin = 18; Tmax = 28
plt.axis([0, Tstop, Tmin, Tmax])
plt.show()
Python Code:
import nidaqmx
import
import
time
numpy as np
Cont.
import matplotlib.pyplot as plt # Logging Temperature Data from DAQ Device
for k in range(N):
Log to File
# Initialize Logging value = task.read()
Tstop = 10 # Logging Time [seconds] print("T =", round(value,1), "[degC]")
Ts = 2 # Sampling Time [seconds] data.append(value)
N = int(Tstop/Ts) time.sleep(Ts)
data = [] writefiledata(k*Ts, value)
https://www.halvorsen.blog/documents/programming/python/
Hans-Petter Halvorsen
University of South-Eastern Norway
www.usn.no
E-mail: hans.p.halvorsen@usn.no
Web: https://www.halvorsen.blog