The main use case for this Trinkey is to use it for making AT devices. CircuitPython has excellent HID device support that lets you write Python code to build HID devices. In the example below, you'll attach two switches to the TRRS jack to send HID commands. The example is sending keyboard presses, but you can also send mouse operations, or do both.
TRRS Standards
If you are using a TRRS splitter instead of an audio plug terminal block, it's important to keep in mind that there are two main standards for TRRS: CTIA and OMTP. If you're familiar with TRS-A and TRS-B MIDI standards, then you'll know how 3.5 mm jacks can be tricky. The pin definitions for your TRRS Trinkey may vary depending on the type of adapter or cable you are using.
- Trinkey TIP (left audio) to button 1 output (blue wire)
- Trinkey RING_1 (right audio) to button 2 output (green wire)
- Trinkey SLEEVE to button 1 GND (black wire)
- Trinkey SLEEVE to button 2 GND (black wire)
CircuitPython Usage
To use with CircuitPython, you need to first install the HID library into the lib folder onto your CIRCUITPY drive. Then you need to update code.py with the example script.
Thankfully, we can do this in one go. In the example below, click the Download Project Bundle button below to download the necessary libraries and the code.py file in a zip file. Extract the contents of the zip file, and copy the entire lib folder and the code.py file to your CIRCUITPY drive.
Your CIRCUITPY/lib folder should contain the following folder:
- adafruit_hid/
Example Code
Once everything is saved to the CIRCUITPY drive, connect to the serial console to see the data printed out!
# SPDX-FileCopyrightText: 2024 ladyada for Adafruit Industries
#
# SPDX-License-Identifier: MIT
import time
import board
import digitalio
import usb_hid
from adafruit_hid.keyboard import Keyboard
from adafruit_hid.keycode import Keycode
# HID Keyboard setup
keyboard = Keyboard(usb_hid.devices)
# Define pins for switches and grounds
tip_switch = digitalio.DigitalInOut(board.TIP_SWITCH)
tip_switch.direction = digitalio.Direction.INPUT
tip_switch.pull = digitalio.Pull.UP
sleeve = digitalio.DigitalInOut(board.SLEEVE)
sleeve.direction = digitalio.Direction.OUTPUT
sleeve.value = False
ring_2 = digitalio.DigitalInOut(board.RING_2)
# Set TIP and RING_1 initially as outputs to low for jack detection
tip = digitalio.DigitalInOut(board.TIP)
tip.direction = digitalio.Direction.OUTPUT
tip.value = False
ring_1 = digitalio.DigitalInOut(board.RING_1)
ring_1.direction = digitalio.Direction.OUTPUT
ring_1.value = False
# Track the state of cable insertion
last_cable_state = False
while True:
# Drive TIP low and check TIP_SWITCH to detect if cable is inserted
tip.direction = digitalio.Direction.OUTPUT
tip.value = False
time.sleep(0.001) # Wait a moment for the state to stabilize
cable_inserted = tip_switch.value # Active low when cable is inserted
# Handle the detected state change for cable insertion
if cable_inserted and not last_cable_state:
print("inserted!")
time.sleep(0.25) # Debounce and allow time for complete insertion
last_cable_state = cable_inserted
if cable_inserted:
# Now configure TIP and RING_1 as inputs with pull-ups
ring_2.direction = digitalio.Direction.OUTPUT
ring_2.value = False
tip.direction = digitalio.Direction.INPUT
tip.pull = digitalio.Pull.UP
sleeve.direction = digitalio.Direction.INPUT
sleeve.pull = digitalio.Pull.UP
# Check the switches and send keycodes
keycode = []
if not tip.value:
print("A")
keycode.append(Keycode.A)
if not sleeve.value:
print("B")
keycode.append(Keycode.B)
if keycode:
keyboard.send(*keycode)
else:
keyboard.release_all()
else:
keyboard.release_all()
time.sleep(0.01) # Sample at 100 Hz
In the code, the direction of the TRRS pins are changed to switch between plug detection and inputs. When you insert a cable, you'll see "inserted!" print to the serial console. When TIP is tied to ground (SLEEVE), you'll see "A" print to the serial console. You'll also send an A keycode over HID. When RING_1 is tied to ground (SLEEVE), you'll see "B" print to the serial console. You'll also send a B keycode over HID.
Page last edited January 22, 2025
Text editor powered by tinymce.