Official examples for Vernier Go Direct® Sensors in both Python and JavaScript
This project is maintained by VernierST
1 Go Direct spectrometers are not supported.
Here are some generally helpful links for getting started with JavaScript
If you have programmed with Python or Arduino, you are probably familiar with using an integrated development environment (IDE) that is customized to the language you are using. Since JavaScript is typically embedded in html code it does not have a dedicated IDE. This can make things a bit tricky when it comes to get started coding with this language.
We used, and can recommend using, Glitch.com or Visual Studio Code. Glitch is an online editor that makes getting started very easy – with no installation required. Glitch facilitates an online community which makes sharing scripts and searching others’ projects straight forward.
Visual Studio Code is more appropriate for coding more complex projects that may require version control or sophisticated debugging. VS Code provides syntax highlighting, bracket-matching, auto-indentation, and other tools to facilitate your coding experience.
Both Glitch and Visual Studio Code are free.
The easiest way to import a library is through a Content Delivery Network (CDN). This allows you to import libraries with a single line. To create programs with Go Direct compatibility put this in the header of your html file:
<script src="https://unpkg.com/@vernier/godirect/dist/godirect.min.umd.js"></script>
The device class creates an object to represent the sensor to be used in the code. This object has many properties that can be used to collect information about the sensor like the battery level, the name, the serial number, and the charging state. In addition to accessing these properties you can also modify the sensor settings through this class. Using the line of code below will assign the selected device to gdxDevice to be used later in the program.
const gdxDevice = await godirect.selectDevice()
To change the sample rate of the sensor, use:
gdxDevice.start(sampleRate);
Many Vernier sensors come with a variety of sensors all bundled into one. For example, the GDX-Force sensor can measure force, but it can also has a gyroscope and an accelerometer. The examples all use the default channel, but you can change the sensor channel to collect different types of data with the same sensor. This can be done with:
const sensor = gdxDevice.getSensor(channelNumber);
sensor.setEnabled(true);
The channel number is an integer corresponding to the available sensors on each device. Use the example program gdx_getting_started_1.html to see available sensor channels for your sensor.