CircuitPython's rotaryio module makes it easy to read the rotary encoder rotation. Along with the Adafruit CircuitPython HID library, it's easy to turn the Rotary Trinkey into a volume knob and play/pause button using the rotary encoder and the button switch.

All the necessary modules and libraries for this example are included with CircuitPython for the Rotary Trinkey, so you do not need to load any separate files onto your board.

Rotary Encoder Location

The rotary encoder (highlighted in red) is on the top of the board. The Rotary Trinkey does not come with an encoder! You must provide and solder on your own.

Volume Knob and Play/Pause Button

In the example below, click the Download Project Bundle button below to download the necessary files in a zip file. Extract the contents of the zip file, open the directory Rotary_Trinkey/CircuitPython_Volume_Knob_Example/ and then click on the directory that matches the version of CircuitPython you're using and copy code.py to your CIRCUITPY drive.

You do not need to copy the entire Project Bundle to your board - all the necessary libraries are included in CircuitPython for Rotary Trinkey!
# SPDX-FileCopyrightText: 2018 Kattni Rembor for Adafruit Industries
#
# SPDX-License-Identifier: MIT

"""Rotary Trinkey Volume and Mute HID example"""
import rotaryio
import board
import usb_hid
import digitalio
from adafruit_hid.consumer_control import ConsumerControl
from adafruit_hid.consumer_control_code import ConsumerControlCode

print("Rotary Trinkey volume and mute example")

encoder = rotaryio.IncrementalEncoder(board.ROTA, board.ROTB)
switch = digitalio.DigitalInOut(board.SWITCH)
switch.switch_to_input(pull=digitalio.Pull.DOWN)

cc = ConsumerControl(usb_hid.devices)

switch_state = None
last_position = encoder.position

while True:
    current_position = encoder.position
    position_change = current_position - last_position
    if position_change > 0:
        for _ in range(position_change):
            cc.send(ConsumerControlCode.VOLUME_INCREMENT)
        print(current_position)
    elif position_change < 0:
        for _ in range(-position_change):
            cc.send(ConsumerControlCode.VOLUME_DECREMENT)
        print(current_position)
    last_position = current_position
    if not switch.value and switch_state is None:
        switch_state = "pressed"
    if switch.value and switch_state == "pressed":
        print("switch pressed.")
        cc.send(ConsumerControlCode.PLAY_PAUSE)
        switch_state = None

Turn the rotary encoder to increase or decrease the volume. Press the button to play or pause your media (depending on the current state).

First, you import the necessary modules and library. Then you setup the rotary encoder, the rotary encoder button switch, and the HID ConsumerControl (which handles the volume and play/pause).

Before the loop, you set the switch_state to None, and the last_position to the rotary encoder position.

Inside the loop, you get the current rotary encoder position. Then using the last_position, you calculate the change in position.

If the position has changes positively (clockwise), increase the volume one step for each step in the rotation. If the position changes negatively (counter-clockwise), decrease the volume one step for each step in the rotation. Then, reset last_position to be able to update the calculation on the next trip through the loop.

If the button is pressed, set switch_state = "pressed". On release, print to the serial console, send the play/pause command, and reset switch_state.

That's all there is to making a volume knob and play/pause button using CircuitPython and the Rotary Trinkey!

This guide was first published on Jun 02, 2021. It was last updated on May 26, 2021.

This page (Volume and Play/Pause) was last updated on Jun 07, 2023.

Text editor powered by tinymce.