Daniel Soto

Energy Access

LabJack U12 Data Acquisition Device and IPython Notebook


This notebook has a simple implementation of a current and voltage meter using a LabJack U12 data acquisition device being run within an ipython notebook. This device has multiplatform drivers and was unreasonably easy to set up on my OS X laptop. I used a single click installer from the LabJack website and then downloaded and installed the python library from github.

I have some new Lithium Iron Phosphate batteries that I wanted to test so I placed a shunt between my battery charger and the battery pack to simultaneously measure the voltage and current delivered to the battery.

The LabJack platform is appealing since I can create very simple and accessible data acquisition on any platform for the classes I teach. Being able to do a simple acquisition in ~20 lines of code with a ~100 USD device is great.

In [1]:
import u12          # this is the labjack device library
import time
import datetime
In [2]:
d = u12.U12()       # initializes device
In [3]:
# sets up an output data file
fout = open('labjack.txt', 'wa')

This defines two convenience functions to encapsulate the details of reading the shunt resistor and battery voltage.

In [4]:
# channel = 0 sets to single-ended input for AI0
def readVoltage():
    return d.eAnalogIn(channel=0)['voltage']

# setting channel to 9 takes the differential between AI2 and AI3
# gain setting of 7 is for a +/- 1 volt range
def readCurrent():
    return d.eAnalogIn(channel=9, gain=7)['voltage']

Executes a loop to read current and voltage, take timestamp and write to file.

In [5]:
# get initial timestamp
start = datetime.datetime.now()
while 1:
    # find time interval between start of test and now
    now = datetime.datetime.now()
    ts = (now - start).total_seconds()
    c = readCurrent()
    v = readVoltage()
    # write csv to file
    fout.write(str(ts)+','+str(v)+','+str(c)+'\n')
    fout.flush()
    time.sleep(1.0)
---------------------------------------------------------------------------
KeyboardInterrupt                         Traceback (most recent call last)
<ipython-input-5-b1e7aeb9c6a1> in <module>()
     10     fout.write(str(ts)+','+str(v)+','+str(c)+'\n')
     11     fout.flush()
---> 12     time.sleep(1.0)

KeyboardInterrupt: 

This is the output in the file. Note that there is about 30 msec of overhead in the loop. There are ways to do synchronous capture, but most of my energy applications don't need that level of timebase precision.

In [6]:
!tail labjack.txt
18.564275,4.5947265625,-0.044921875
19.595143,4.5947265625,-0.04443359375
20.627395,4.5947265625,-0.04541015625
21.659249,4.5947265625,-0.044921875
22.683053,4.5947265625,-0.044921875
23.715622,4.599609375,-0.044921875
24.747434,4.6044921875,-0.044921875
25.779089,4.5947265625,-0.04443359375
26.80343,4.5947265625,-0.044921875
27.835673,4.599609375,-0.044921875

Creative Commons License
This work is licensed under a Creative Commons Attribution 3.0 Unported License.