23 Mar

Data Acquisition (DAQ) Using Python

It is VERY easy to interface with hardware using Python. While I am definitely not a Python expert, I thought it would still be worthwhile to write this quick post. How many times have you wanted to connect a microcontroller based project up to the computer to either save data or control something via a simple program?

In my previous post I discussed using a thermistor for our toaster oven based reflow oven. The next step for our toaster oven is to see how well it can follow a temperature curve. The Python code below will allow us to log/acquire the temperature of our oven over a set duration (a few minutes). While the example presented here is specifically for saving raw data to a file (to be read in MATLAB), it can be used in any application. For example, I am currently using Python to interface with a Bluetooth Low Energy breakout board (buy one now at our store!).

This simple code will receive a single byte of data over a virtual serial port and save it to a file. The code actually does not need to be modified to receive 16-bit values. While this example will acquire data for a specific duration of time, Python can be used to interface directly with a microcontroller for any number of applications. Be sure to install pySerial, otherwise this will not work.

import serial

# Duration in seconds, sample rate (fs) in Hz
duration = 420;
fs = 2.0;

# Calculate sample time and number of loops needed
T = 1/fs;
loops = int(duration/T)

# Setup serial port and open file to write
serial = serial.Serial("COM3", 9600, timeout=5)
f = open("Data1.raw", "w")

# Loop as needed, writing datato file
for i in range(0,loops):
data = serial.read()
f.write(data)
print data),
# Be sure to close the file afterwards
f.close()

Change the sample rate (fs) and the duration to match your application. Set the COM port and baud rate. Specify the file name where you want to save the data, and then run the script. The code should be fairly self explanatory.

Now that we have a file which contains our data, time to do something with it! The MATLAB (Octave) code below will read in a file which contains 16-bit values and plot it.

clear all;
close all;
clc

fid = fopen('Data1.raw');
raw_data = fread(fid, 'uint16');
fclose(fid);

x = double(raw_data)

plot(x);

This is all you need to interface your next project with your computer or to test out your toaster oven reflow oven. The example below shows the entire temperature curve from a test run of the toaster oven. Stay tuned for more cool projects!

Python Acquired Temperature

Python Acquired Temperature

4 thoughts on “Data Acquisition (DAQ) Using Python

  1. This is very helpful to me. In other sources there is a wealth of information about interfacing to an NI library in Python, but I have several instruments with on-board processing which I would like to log and display using Python. No need for NI hardware on this particular project I’m embarking upon.

    Question though… Can this be used with TCP connection? I know the package is called “serial” but I didn’t think Bluetooth falls under Serial communication protocols either. I’m quite a newbie to this. So, I hope my question at least makes sense. Thanks again!

    • Glad the post was helpful. TCP is a networking protocol and is different than what I am referring to when I say “serial port”. In this case, the UART peripheral on an MCU can send data through a UART to USB device (such as the FTDI chips) to transmit data to a PC. The USB device shows up as a virtual com port on the PC side, which is why we can use pyserial. If you are looking to use TCP and do not have much experience, I would recommend using an Arduino with an ethernet shield. A quick Google search should provide you with a plethora of references. Keep in mind that “serial” is a very broad term. SPI, I2C, UART, and many other protocols are considered “serial” communications since bits are not sent in parallel.

  2. I have a question regarding your code, im working with a dac controller(usb-2408 series) and im trying to write some python code to acquire the data collected from my sensores that i have connected into the different channels.

    So my question is, can this module serial be used to run such an application, and if so, would a simple modification to the postet code work to collect data from multiple sensors ?

    • Simon, it all depends on the communication protocol of the device and how the device enumerates on the PC. If it shows up as a virtual COM port, you may be able to communicate with it, if the protocol is available. Doing this would not be simple if you do not have experience working with custom communication protocols. Your best bet would be to find an existing library for communicating with the device. Good luck!

Leave a Reply

Your email address will not be published. Required fields are marked *