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.
import u12 # this is the labjack device library
import time
import datetime
d = u12.U12() # initializes device
# 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.
# 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.
# 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)
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.
!tail labjack.txt
This work is licensed under a Creative Commons Attribution 3.0 Unported License.