The LabTrinket is based on the Trinket M0 microcontroller from Adafruit. It is designed to use the Tinket as a low cost component for take-home science labs allowing data gathering and some control of experiments. The Trinket is run connected to a host computer and responds to simple serial commands over USB. LabTrinket consists of two parts.
- A Circuit Python script to place on the Trinket microcontroller to interpret and execute serial commands from the host computer
- A python library for the host computer to allow it to communicate with the Trinket
For Windows you will need to download and install drivers from here: https://learn.adafruit.com/adafruit-trinket-m0-circuitpython-arduino/windows-driver-installation This step is not needed for Linux or MacOS. Follow the instructions at that link.
Connect the Board via the USB. You should see some LEDs flash on the board and you will notice the Trinket appears as an external drive on your computer, like a flash drive.
The Trinket M0 uses an onboard Python interpreter called CircuitPython. Before first use you will need to update to the latest version. Visit this section: https://learn.adafruit.com/adafruit-trinket-m0-circuitpython-arduino?view=all#what-is-circuitpython and follow the instructions very carefully to install the latest version on the Trinket.
The LabTrinket client code is called "main.py" can be found in the "CircuitPython" folder of this repo. https://github.com/jhoyland/LabTrinket After you have updated CircuitPython and restarted your Trinket (by plugging it out and in again) you can copy main.py onto it. It will overwrite the main.py which is already on your Trinket.
The Circuit Python on the Trinket needs a couple of libraries to operate the on board LED. You can get hold of the complete bundle of libraries from https://github.com/adafruit/Adafruit_CircuitPython_Bundle/releases/tag/20200925 You probably installed CircuitPython 5 in the earlier step so get version 5 of the bundle. Unzip this somewhere convenient. From the bundle you need two libraries:
adafruit_dotstar.mpy
adafruit_pypixelbuf.mpy
On your Trinket there should be a folder called \lib (you can create one if it's not there) Copy the two libraries into the lib folder on the Trinket. That's the whole install for the Trinket itself. You don't need the rest of the bundle you just unzipped and it can be deleted if you want to save space.
You need one last bit of install. The base Anaconda installation does not include the serial communications package. To install, you need to start up an "Ananconda Prompt" - on Windows this is on the Start Menu under "Anaconda". There you can type: conda install -c anaconda pyserial
Follow the on screen instructions. This should install the latest version. If not using Anaconda use pip or equivalent to install pyserial
Provided in this repo is a Jupyter Notebook to test the specific features of the LabTrinket. You can also run labtrinket.py directly for a demonstration
To use labtrinket from python
import labtrinket
import serial
You must first setup the serial connection e.eg
ser = serial.Serial()
ser.port = 'YOUR_SERIAL_PORT'
ser.baudrate = 9600
ser.open()
Here YOUR_SERIAL_PORT
is whatever port your Trinket is connected to.
With the serial port connected you can create a labtrinket object and associate it with the serial connection.
trinket = labtrinket.LabTrinket(ser)
Lab trinket commands:
ledSetColor(red=r,green=g,blue=b)
Sets LED color. Where r,g and b are integers from 0-255 for the LED red,greed and blue colors
ledSetBrightness(n)
Sets LED brightness to n. Where n is a percentage of maximum
ledOn()
Turns the LED on with the last RGB and brightness values
ledOff()
Turns the LED off
adcRequestValue()
Requests a single value from the Trinket. The function does not wait for the value. The Trinket reads the ADC value and returns it when ready. You must use adcGetValue to obtain the value from the serial buffer.
adcGetValue(tries=n)
Will try n times to get an ADC value from the serial buffer. Returns true if successful. The value itself is stored in the 'value' field of LabTrinket object. You can access this like:
x = trinket.value
adcRun()
Sets the Trinket ADC into "free-running" mode where it continually requests new values at a fixed rate. You receive these by continually calling adcGetValue
adcStop()
Ends free running mode. Values may remain in the serial buffer so you should either flush it or poll it with adcGetValue until empty
adcAveragedValue(n)
Takes n readings from the ADC and returns the mean value
adcDelay(d)
Sets the time in seconds of the delay between measurements in free running or averaged mode. Default value is 1 second.
dacOn()
Turns on the True DAC on pin 1 of the Trinket M0.
dacOff()
Turns off the DAC
dacVolts(v)
Sets the DAC voltage to v. Will not let you set a voltage higher than 3.3V