Writing a simple script in Python to get a chart

27 enero 2013

In last article we talked about how to modify one of the examples for Python available on this website to create a customized application, to use in your own experiments.

To complement that article, and also thinking of those of you who have little experience with Python, now we will show you step by step how to do in Python to configure a simple data acquisition experiment for openDAQ from scratch, creating a graph with the data returned.

The objective is to demonstrate how easy it is to create a script in Python to collect data from openDAQ and to represent it.

First, of course, you must have downloaded the Python IDE. All the necessary information about how to install it can be found on the website of the foundation.

Apart Python IDE, you will need some modules. The ones used in this example are PySerial, Matplotlib, and Numpy. You can find the links on Support page.

Most important, you must have downloaded daq.py, the main module to control openDAQ in Python.In Support page there is a link to the repository, where you can find the file.

Now let's see step by step how this example could be programmed. You can download here the complete code for the example.

Import modules. You should've downloaded them previously through the Support page:

import serial

import matplotlib

import matplotlib.pyplot as plt

from daq import *

import numpy as np


Connect to the device and set up the acquisition. It is the most important step and where you access to the device configuration at low level:

 dq = DAQ("COM10")

#(Change to the serial port indentificator in your PC)

period=200 #period = 200ms

numberPoints=20 #we are going to get 20 points, 4sec.


ninput=0 #Using A8 as SE input (negative-input=GND)

nSamples=20 #Mean of 20 samples per point

gain= GAINx1 #+-4V range, x1 amplification

dq.create_stream(1,period) #Create the Stream

#Set Datachannel 1 number of points and run once:



#Configure number of samples, + input, - input

#gain and number of channel


Get the points. Then, we are using an infinite loop to collect the points (get_stream). The instrument will send a STOP code when it has collected the number of points configured to.

 while True:

result = dq.get_stream(data,channel)

if result == 1:

#data available

print "New data received -> n Points = ",len(data)

elif result == 3:


print "Stop received"



After collecting the raw data, some operations must be made to display them in mV.

 for i in range(len(data)):

dataTemp =float(data[i])

dataTemp *= -gains[gain]

dataTemp += offset[gain]

dataTemp /= 100000



Draw and show the graph. Finally, we define a Matplotlib graph, the collected points are passed through it, and we define legends and plot title to be displayed.

 #Define plot, figure and chart

fig = plt.figure()

plt.xlabel("Time (ms)")

plt.ylabel("Voltage (mV)")

plt.title("My chart")

fig.canvas.set_window_title("Example 1")



#Lastly show our chart



The final graph depends on what you have connected to openDAQ. In our case, we have a signal connected to the A8 input of about 900mV.