You can use your Trinkey QT2040 to connect various I2C sensors and breakouts to your PC running Windows, Mac OSX, or Linux. The U2IF firmware gets loaded onto the Trinkey, and enables you to do the following:

  • Use the Trinkey as an I2C USB adapter for connecting any I2C sensor or display, etc.
  • Control the built-in NeoPixel RGB LED.
  • Read the built-in BOOT button.

This approach is useful if you're looking to run Python code on your computer and have it communicate with the devices connected through the QT2040 Trinkey.

Computer Setup

Follow the setup instructions for your operating system: Windows, Mac OSX or Linux. Then complete the post setup checks to make sure the U2IF setup was successful. Then head back here, and continue setting up your Trinkey!

Trinkey Setup

Setup for the Trinkey is simple. First, download the U2IF Firmware for QT2040 Trinkey file linked below.

Next, put the Trinkey into bootloader mode using the following steps:

  • Press and hold the BOOT button.
  • While holding it (do not let go of the BOOT button!), press and release the RESET button.
  • Do not let go of BOOT until the RPI-RP2 drive appears.

Finally, drag the firmware file to the RPI-RP2 drive. The board will reset after the copy is complete. Note that no folders will show up. So it may seem like nothing happened.

No folders will show up after reset - this is normal.

Trinkey Example Code

This example requires you to install the NeoPixel library. Run the following command in your terminal:

pip install adafruit-circuitpython-neopixel

Depending on your setup, you may need to use pip3!

Then, save the following file to your computer, wherever is convenient for you.

"""CircuitPython U2IF Example for QT2040 Trinkey"""
import board
import digitalio
import neopixel

pixel = neopixel.NeoPixel(board.NEOPIXEL, 1)

button = digitalio.DigitalInOut(board.BUTTON)
button.switch_to_input(pull=digitalio.Pull.UP)

while True:
    if not button.value:
        pixel.fill((255, 0, 0))
    else:
        pixel.fill((0, 0, 0))

With the Trinkey plugged into your computer, run the following command from the directory in which you saved the above file:

python u2if_qt2040_example.py

Now, press the BOOT button on the Trinkey. The NeoPixel lights up!

This is a simple example, but you can so all sorts of things with the BOOT button as an input, or controlling the NeoPixel. Give it a try!

Trinkey I2C Example Code

This example reads the temperature from an MCP9808 I2C temperature sensor.

First, wire up the sensor as follows.

Simply connect the STEMMA QT cable from the STEMMA QT port on your board to the STEMMA QT port on the MCP9808.

The example requires you to install the MCP9808 library. Run the following command in your terminal:

pip install adafruit-circuitpython-mcp9808

Depending on your setup, you may need to use pip3!

Then, save the following file to your computer, wherever is convenient for you.

"""
CircuitPython U2IF I2C QT2040 Trinkey Example

Reads the temperature every two seconds from an MCP9808 I2C temperature sensor
connected via STEMMA QT.
"""
import time
import board
import adafruit_mcp9808

i2c = board.I2C()  # uses board.SCL and board.SDA
mcp9808 = adafruit_mcp9808.MCP9808(i2c)

while True:
    temperature_celsius = mcp9808.temperature
    temperature_fahrenheit = temperature_celsius * 9 / 5 + 32
    print("Temperature: {:.2f} C {:.2f} F ".format(temperature_celsius, temperature_fahrenheit))
    time.sleep(2)

With the Trinkey plugged into your computer, run the following command from the directory in which you saved the above file:

python u2if_qt2040_i2c_example.py

The temperature will display in both C and F, every two seconds.

You can use any CircuitPython I2C sensor library in a similar way. That's all there is to reading an I2C sensor using Python on your computer with U2IF!

This guide was first published on Jun 23, 2021. It was last updated on 2022-01-27 13:41:15 -0500.

This page (U2IF Python Example) was last updated on May 20, 2022.

Text editor powered by tinymce.