Official examples for Vernier Go Direct® Sensors in both Python and JavaScript
This project is maintained by VernierST
This guide shows you how to get started writing Python and VPython programs to communicate with the sensors on-board most Vernier Go Direct1 devices.
There is a separate guide for Getting Started with Vernier Go Direct Sensors and Web VPython. This is an exciting option for the following reasons:
Note that we also have a Getting Started with Vernier LabQuest® and Python guide for using Python with Vernier LabQuest connecting our wired, BTA and BTD LabQuest sensors, with our Digital Control Unit (DCU-BTD) for controlling motors and other output devices, and with off-the-shelf or custom-built sensors.
This 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, focusing on installing the godirect module and downloading the examples may be sufficient.
1 Go Direct spectrometers, Mini GC, Polarimeter, Go Wireless Heart Rate, and Cyclic Voltammetry System do not work with the godirect library. Go Direct sensors that are not supported, or that may require advanced programming, calibration, or analysis, include Blood Pressure, Sound, Ion-Selective Electrode, Optical Dissolved Oxygen, Conductivity, and timing/event devices like Photogates, Drop Counters, Projectile Launcher, Radiation, and Rotary Motion.
We have developed a Python module called godirect
that allows you to communicate with the sensors of a Vernier Go Direct device via USB or Bluetooth Low Energy (BLE). This requires the following:
The godirect module 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, Linux, 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 godirect module and other Python modules that you may need. 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, for example). There are slight differences in the required steps to install the godirect module for Windows, macOs, and Linux (including Linux running on Raspberry Pi). Follow the steps outlined below for your platform.
Run the following command in Powershell or Command Prompt to install the godirect module for USB and BLE:
pip3 install godirect
If you received an error during the installation of godirect, part of which looks like the message below, it is probably because your Windows system does not have the appropriate compiler. The godirect module includes a package that requires a C++ compiler.
In order to install without this failure, you will need to follow the instructions to download and install the command line Build Tools for Visual Studio. The latest version of the tools download can be found by scrolling down to the All Downloads section and expanding Tools for Visual Studio (as shown in the figure below). After completing the installation of this tool repeat the pip3
installation steps for godirect.
Run the following command in Terminal to install the godirect module for USB and BLE:
pip3 install godirect
Run the following command in your terminal to install the godirect module for USB and BLE:
pip3 install godirect
Some Linux distributions will not include all of the tools required by the godirect module and its dependencies. If you see errors when you run the pip3
install, you might need to first install the two packages as shown below, before trying again:
sudo apt install libusb1.0.0
sudo apt install libudev-dev
In order to communicate with Go Direct devices over USB on Linux systems, you will need to provide a special udev rule that allows the device to be identified when it is plugged in. The rule file is located on our github directory. Use the following steps to download the rule file and then move it to the proper location:
cd ~/Desktop
pwd
ls
sudo cp vstlibusb.rules /etc/udev/rules.d/.
At this point, you should have Python 3 installed and have the godirect module installed. Before moving to examples, confirm the installation of the godirect module by showing the version information. Run the following command in the terminal:
pip3 show godirect
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 godirect --upgrade
With the godirect module installed, it is time to run an example.
Before diving in to the “getting_started..” examples, go back to the downloaded files and note that there are folders with other examples. Specifically, we have created support for combining Go Direct sensors with VPython. The VPython examples, as well as the Getting Started with Vernier Go Direct® Sensors and VPython manual are located in the /vpython_examples/ folder.
The getting started examples demonstrate how to collect data from the sensors of a Go Direct device. Notice at the very top of the getting started examples the import is for a local module named gdx
that can be found in the ./gdx/ folder.
from gdx import gdx
gdx = gdx.gdx()
The gdx module was created for a cleaner, simpler entry point into coding with Go Direct devices. If you go to the /gdx/ folder and open gdx.py, you will see that it imports godirect and provides simplified functionality of the godirect methods.
Here are some key points to keep in mind:
pip install godirect
. Instead, it is part of the download of the godirect-examples repository. Look for it in the download, in the same directory as the getting started examples.import gdx
in your program, Python must be able to find the /gdx/ folder. Here are two ways that make it easy for Python to find the /gdx/ folder:
import os
import sys
file_path = os.path.abspath(os.path.dirname(sys.argv[0]))
os.chdir(file_path)
os.chdir("..")
gdx_module_path = os.getcwd()
if gdx_module_path not in sys.path:
sys.path.append(gdx_module_path)
The gdx functions used in a typical program to collect data include:
gdx.open()
gdx.select_sensors()
gdx.start()
gdx.read()
gdx.stop()
gdx.close()
The gdx functions used in a typical VPython program are described in the Getting Started with Vernier Go Direct® Sensors and VPython manual.
A simple program using the gdx module looks like this (note that in this example the gdx folder is assumed to be in the same directory as the example, or in site-packages where Python can find it):
from gdx import gdx
gdx = gdx.gdx()
gdx.open(connection='usb')
gdx.select_sensors()
gdx.start()
for i in range(0,5):
measurements = gdx.read()
if measurements == None:
break
print(measurements)
gdx.stop()
gdx.close()
Here is some more information about the gdx functions, including how you might add arguments to a few of the functions:
gdx.open()
connection=
and device_to_open=
connection=
parameter can be set to connect Go Direct devices to either USB 'usb'
or Bluetooth 'ble'
gdx.open(connection='usb')
gdx.open(connection='ble')
device_to_open=
argument is left blank the function finds all available Go Direct devices, prints the list to the terminal, and prompts the user to select the devices to connect.device_to_open=
with your device name or names.
gdx.open(connection='usb', device_to_open=“GDX-FOR 071000U9, GDX-HD 151000C1”)
gdx.open(connection='ble', device_to_open=“GDX-FOR 071000U9, GDX-HD 151000C1”)
gdx.open()
function it will search and connect to those specific Go Direct devices. In the above code snippet a Go Direct Force and Acceleration device with a serial number of 071000U9 and a Go Direct Hand Dynamometer with serial number 151000C1 will be opened.gdx.open(connection='ble', device_to_open=“proximity_pairing”)
gdx.select_sensors()
gdx.select_sensors([1])
gdx.select_sensors([[1,2,3],[1]])
gdx.select_sensors()
argument blank and you will see the list in the terminal.gdx.start()
select_sensors()
function.gdx.start(1000)
to sample every 1000 milliseconds, or gdx.start(100)
to sample every 100 milliseconds.measurements = gdx.read()
gdx.read()
function will take single point readings from the selected sensors at the desired period and return the readings as a 1D list.gdx.stop()
gdx.stop()
function stops data collection but does not disconnect the device, so it is possible to start a new round of data collection.gdx.close()
gdx.close()
you can not call any other gdx functions.As stated earlier, these gdx functions are available in the gdx.py file, in the gdx folder. The gdx.py file is easy to locate and available for you to modify. Consider adding your own functions, or modifying the functions to meet your needs.
ModuleNotFoundError: No module named 'gdx'
, remember to place a copy of the /gdx/ folder in the same directory as your file, or in the site-packages directory, or create code to add a system path to the location of the /gdx/ folder.pip3 install bleak
error: option --single-version-externally-managed not recognized
This is a known incompatibility and the options to work around it are described here: https://github.com/hbldh/bleak/issues/147
If you wish to connect to Go Direct device using a Bluegiga BLED112 Bluetooth® Low Energy Dongle, rather than the native BLuetooth radio, you will need to install the vernierpygatt module with this command:
pip3 install vernierpygatt
You will also need to pass use_ble_bg=True
into the godirect module. There is a comment regarding this in the gdx.ble_open
function in the gdx.py file.
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.