Simple Sample Code:
Demonstrates reading a single analog input (AIN) from a LabJack and logging it to a separate csv file.
from datetime import datetime
from time import sleep
from labjack import ljm
# Open first found LabJack
handle = ljm.open(ljm.constants.dtANY, ljm.constants.ctANY, "ANY")
#handle = ljm.openS("ANY", "ANY", "ANY")
info = ljm.getHandleInfo(handle)
print("Opened a LabJack with Device type: %i, Connection type: %i,\n" \
"Serial number: %i, IP address: %s, Port: %i,\nMax bytes per MB: %i" % \
(info, info, info, ljm.numberToIP(info), info, info))
# Setup and call eReadName to read from a AIN on the LabJack.
name = "AIN0"
result = ljm.eReadName(handle, name)
#Create file and location of file
#Make sure to specify the preferred name and path of this file
file = open('/home/data_log.csv', 'a')
if os.stat('/home/data_log.csv').st_size == 0:
file.write("Time, Sensor Name, Sensor Reading \n")
now = datetime.now()
file.write(str(now) + "," + str(name) + "," + str(result) + "\n")
#print("\n%s reading : %f V" % (name, result))
# Close handle
This is a simple application note explaining how to start logging data from a Labjack T4 onto a Raspberry Pi. We often have customers ask us how to perform simple standalone data logging and while you can do this using the Lua scripting features of the T-Series devices(T8, T7, T4); LabJack devices are the most powerful and deliver the best user experience when connected to a computer. Typically a customer interested in standalone data logging wants to reduce their cost, footprint but maintain reliability. This option does not add much to your costs but will greatly improve your reliability and simplify logging data without the risk of batteries going dead or a remote node losing its connection. To keep things as simple and straightforward as possible, this data logging system will be using Python.
- A Raspberry Pi 3 or newer that has Raspbian installed. The Pi Foundation has a guide titled Installing Raspbian with Noobs that is easy to follow.
- Basic command line/Linux terminal skills:
- Basic computer skills (connecting keyboard, mouse, turning on Raspberry Pi)
- LabJack T4 or LabJack T7
- Raspberry Pi 3 or newer
- Display for Raspberry Pi
- Keyboard and mouse to connect to Raspberry Pi
- An analog sensor of your choosing
- Measure and record data using a Raspberry Pi and a LabJack device
- Create a virtual environment
- Install LabJack LJM Driver
- Install and Run LJM Python Examples
- Read voltage from Analog input
- Log readings to separate file
Update Raspberry Pi (suggested but not required)
It is always good to keep your system up to date. Updating your system ensures that you are using the newest version of Raspberry Pi’s software and avoids any compatibility issues when running programs on the Raspberry Pi. Updating can sometimes break things, so only do this if you aren't on a mission critical system.
$ sudo apt-get update && sudo apt-get upgrade
Create Virtual Environment (optional)
It is often beneficial to use a Python virtual environment to keep Python packages you use for a particular application from affecting your system Python packages and vise-versa. A virtual environment can be created using the commands below. If you are unfamiliar with virtual environments, check out Python Virtual Environment for more information.
$ python3 -m venv myenv #make a venv
$ source myenv/bin/activate # activate the venv
Install the latest LJM Library
First, we recommend checking which architecture your device uses. Most older Raspberry Pis use the 32-bit ARM architecture (ARMv7) but newer Raspberry Pis also support 64-bit ARM (sometimes called ARMv8 or Aarch64). You can easily check your computer architecture with the following command:
$ uname -m
This should output something like ARMv7, ARMv8, or Aarch64. From there, navigate to the LJM Downloads page to download and install the appropriate ARM Linux driver. Expand "Details" toward the right of the installer download link for installation instructions.
Install labjack-ljm Python package
To run LJM using Python, both the LJM library and the labjack-ljm Python package must be installed. We recommend using pip to install labjack-ljm as shown in the following command:
$ pip install labjack-ljm
For more details and troubleshooting tips, check out Python for LJM.
With the LJM library installed and a virtual environment created, you should be ready to run some examples. Our newest Python examples can be downloaded from our labjack-ljm-python GitHub repository.
Standalone Data Logging
Your Raspberry Pi is now ready to begin logging data. There are many different ways you could record data however, one of the simplest ways is to write analog input data to a .csv file. This .csv file can then be accessed in the Raspberry Pi’s file directory and/or an external USB flash drive. An example python script for this is provided at the bottom of this app note.
Going Deeper: JEDI One by Machinechat
If you would like to go further than just logging data and want to monitor in real time, JEDI One is a perfect tool that is affordable and easy to run on a Raspberry Pi. JEDI One by Machinechat offers powerful yet lightweight software (less than 30MB) that can be used with LabJack UD and T-Series hardware. Pairing a Labjack with the JEDI One software from Machinechat, you can create rules, alerts and actions, email and text notifications, beautiful dashboards, and network monitoring, among other things. Best of all, this can all be set up in about 15 minutes.
If you are interested in using JEDI One with your Labjack device, check out Supercharging your LabJack for instructions and more.