Official examples for Vernier LabQuest® interfaces in Python
This project is maintained by VernierST
This guide shows you how to get started writing Python programs for your Vernier LabQuest®. The guide contains the following topics:
If you are new to Python you should look over all of the steps. If you are familiar with Python you might just focus on installing the labquest package and downloading the examples.
We have developed a Python package called labquest
that allows you to communicate with Vernier LabQuest devices via USB. This requires the following:
To communicate with LabQuest devices on Windows computers a driver must be installed. If you already have Vernier Graphical Analysis or Logger Pro software installed, you have the driver. If you do not have this Vernier software, the driver can be installed with either the free trial of Graphical Analysis Pro or the free demo version of Logger Pro software.
The labquest package is designed to work with Python 3, it is not compatible with Python 2. In some cases, Python is pre-installed and shipped with your operating system. Use the following steps to check for Python 3 on your machine, and to install Python 3 if needed:
python --version
python3 --version
With Python installed, you need to choose an application that you will use to write and run your Python programs. You can write your programs in a simple text or code editor, or you can use an Integrated Development Environment (IDE). An IDE is a software application that provides all of the tools to write, comment, edit, debug, develop and run software programs. Python has an IDE bundled with it called IDLE that is cross-platform, and suitable for beginners. If you are new to Python and programming, we recommend that you start with this tool.
Later, you may want to research the various IDEs, learn about the different features, and give one a try. For example, Visual Studio Code is a free IDE available for Windows and Mac. There are many other choices, and you can find more information at python.org
Once you have Python 3 installed, you will use a tool called pip
(a Python 3 package manager) to install the labquest package. Python automatically includes pip
, so this tool is ready to use. Note that we will be using the pip3
command, rather than just pip
, to ensure that the Vernier files will be associated with Python 3, and not Python 2.
The pip3
commands are executed by running them in your operating systems’ tool for executing commands (Powershell, Command Prompt, or Terminal window). There are slight differences in the required steps to install the labquest module for Windows, and macOs. Follow the steps outlined below for your platform.
Run the following command:
pip3 install labquest
At this point, you should have Python 3 installed and have the labquest package installed. Before moving to examples, confirm the installation of the labquest package by showing the version information. Run the following command in the terminal:
pip3 show labquest
Running the pip3 show
command will provide information about the installed package, including the version number.
Should you need to update to a newer version in the future, run the following command in the terminal:
pip3 install labquest --upgrade
With the labquest package installed, it is time to run an example.
The examples demonstrate how to collect data from LabQuest analog and digital sensors, as well as how to control the output lines of a Vernier Digital Control Unit (DCU). These examples all use the labquest package to communicate with the LabQuest device.
A typical example program to collect periodic, single point data from analog sensors, motion detector, rotary motion, and photogate counting would include the following functions:
lq.open()
lq.select_sensors()
lq.start()
lq.read()
lq.stop()
lq.close()
A simple program using these functions looks like this:
from labquest import LabQuest
lq = LabQuest()
lq.open()
lq.select_sensors(ch1='lq_sensor')
lq.start(100)
for x in range(10):
ch1_measurement = lq.read('ch1')
if ch1_measurement == None:
break
print(ch1_measurement)
lq.stop()
lq.close()
LabQuest analog sensors, motion detector, rotary motion, and photogate counting will all follow this format (with slight changes to configure data collection from the correct channel and the correct sensor).
Performing fast data collection (a packet of samples in a short time frame), and performing photogate timing will each use a different read function (this is described below).
In addition, there are examples to show how to control the output from a Digital Control Unit (DCU) connected to LabQuest’s digital channels. The DCU allows you to control small electrical devices such as servo motors, dc motors, fans, buzzers, and such.
Here is some more information about the functions:
from labquest import LabQuest
lq = LabQuest()
lq
(you can give this a different name if you would like) to access the LabQuest()
class. The functions are located in the LabQuest()
class and can now be accessed using ‘lq’ and the function’s name, such as lq.open()
, lq.start()
, or lq.read()
.lq.open()
lq.open()
will open communication to your LabQuest device from your computer via USB.
There are no arguments for this function.
Multiple LabQuest devices can be connected as long as they are the same type (for example, you can connect two LabQuest Minis, or two LabQuest 3’s, but not a Mini and a LabQuest 3).
lq.select_sensors()
Use this function to configure all of the sensors used in your program.
If this function’s argument is left blank, a prompt in the terminal allows the user to configure each LabQuest channel.
This function has parameters for all of the channels (ch1, ch2, ch3, dig1, dig2). Set each channel with a string value corresponding to what sensor is connected (e.g. ch1=’lq_sensor’)
'lq_sensor', 'lq_sensor_cal0', 'lq_sensor_cal1', 'lq_sensor_cal2', 'raw_voltage'
‘motion’, ‘rotary_motion’, ‘rotary_motion_high_res’, ‘photogate_count’, ‘photogate_timing’, ‘dcu’, ‘dcu_pwm’
Here are some examples of how you would set the parameters to match your sensor configuration.
lq.select_sensors(ch1='lq_sensor')
lq.select_sensors(ch1='lq_sensor', ch2='lq_sensor_cal1')
lq.select_sensors(ch1='lq_sensor', dig1='motion'})
lq.select_sensors(ch1='raw_voltage')
lq.start()
Start collecting data at a specified period (time between samples).
This function takes an argument to set the period in milliseconds. For example, lq.start(1000)
to sample every 1000 milliseconds, or lq.start(100)
to sample every 100 milliseconds.
If this function’s argument is left blank, a prompt in the terminal will appear for the user to enter the specified period.
measurement = lq.read()
The lq.read()
function will return single point readings from the selected channel at the desired period.
Use a separate lq.read()
for each configured channel.
ch1_measurement = lq.read('ch1')
dig1_measurement = lq.read('dig1')
Place the lq.read()
function in a loop and make sure the loop can iterate fast enough to keep up with the sampling period (that is, do not have other code in the data collection loop that might slow the loop).
lq.read_multi_pt()
measurements = lq.read_multi_pt()
The lq.read_multi_pt()
function returns multi-point readings from the selected channel at the desired period.
ch1_measurements = lq.read_multi_pt('ch1', 100)
lq.read()
measurements = lq.photogate_timing()
When using a photogate to collect timing data, measurements are not returned with lq.read()
, instead they are returned with the lq.photogate_timing()
function.
This function returns a list of measurements of the photogate’s blocked time, unblocked time, blocked time, unblocked time, etc..
lq.start()
measurements = lq.photogate_timing('dig1', 15, 10)
lq.stop()
Stop data collection on the selected sensors.
Calling the lq.stop()
function stops data collection but does not disconnect the sensor, so it is possible to start a new round of data collection using start(), read(), and stop().
If the program is using the Digital Control Unit (DCU), calling lq.stop()
turns off all DCU output lines and stops any pulse width modulation (pwm) output.
This function does take arguments that allow independent control of stopping data collection and stopping DCU output. For example, if you wanted to stop pwm output, but continue data collection, set the arguments appropriately.
stop(stop_measurements=False, stop_dcu=False, stop_pwm=True)
lq.close()
lq.close()
you can not call any other lq functions.All of the content in this repository is available under the terms of the BSD 3-Clause License.
Vernier products are designed for educational use. Our products are not designed nor are they recommended for any industrial, medical, or commercial process such as life support, patient diagnosis, control of a manufacturing process, or industrial testing of any kind.