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 a YouTube frame-by-frame advancer and YouTube 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.

YouTube Frame-by-Frame and Play/Pause

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_YouTube_Frame_Example/ and then click on the directory that matches the version of CircuitPython you're using and copy 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: 2021 Kattni Rembor for Adafruit Industries
# SPDX-License-Identifier: MIT

"""Rotary Trinkey YouTube Frame-by-Frame Example"""
import time
import rotaryio
import board
import digitalio
import usb_hid
from adafruit_hid.keyboard import Keyboard
from adafruit_hid.keyboard_layout_us import KeyboardLayoutUS

print("Rotary Trinkey YouTube Frame-by-Frame example")

encoder = rotaryio.IncrementalEncoder(board.ROTA, board.ROTB)
switch = digitalio.DigitalInOut(board.SWITCH)

time.sleep(1)  # Sleep for a bit to avoid a race condition on some systems
keyboard = Keyboard(usb_hid.devices)
keyboard_layout = KeyboardLayoutUS(keyboard)  # We're in the US :)

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):
    elif position_change < 0:
        for _ in range(-position_change):
    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.")
        keyboard_layout.write(' ')
        switch_state = None

With a YouTube video actively in focus on your computer, rotate the rotary encoder knob clockwise and counter-clockwise to advance forward and backward (respectively) frame-by-frame through the YouTube video. Press the rotary encoder button to play or pause the video.

This example only works when the YouTube video is currently in focus. It does not work with the video in the background.

First, you import the necessary modules and library. Then you setup the rotary encoder, the rotary encoder button switch, and the HID keyboard and keyboard layout.

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), advance the video forward one frame for each step in the rotation (by sending a "."), and print to the serial console. If the position changes negatively (counter-clockwise), advance the video backward one frame for each step in the rotation (by sending a ","), and print to the serial console. 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 (by sending a space), and reset switch_state.

That's all there is to advancing through YouTube videos frame-by-frame using CircuitPython and the Rotary Trinkey!

This guide was first published on Jun 02, 2021. It was last updated on Jun 23, 2024.

This page (YouTube Frame-by-Frame) was last updated on Jun 23, 2024.

Text editor powered by tinymce.