Once you've finished setting up your QT Py RP2040 with CircuitPython, you can access the code and necessary libraries by downloading the Project Bundle.
To do this, click on the Download Project Bundle button in the window below. It will download to your computer as a zipped folder.
# SPDX-FileCopyrightText: Copyright (c) 2025 Liz Clark for Adafruit Industries
#
# SPDX-License-Identifier: MIT
"""AS5600 Encoder"""
import usb_hid
import board
from adafruit_hid.consumer_control import ConsumerControl
from adafruit_hid.consumer_control_code import ConsumerControlCode
import adafruit_as5600
i2c = board.STEMMA_I2C()
sensor = adafruit_as5600.AS5600(i2c)
enc_inc = ConsumerControlCode.VOLUME_INCREMENT
enc_dec = ConsumerControlCode.VOLUME_DECREMENT
cc = ConsumerControl(usb_hid.devices)
last_val = sensor.angle
THRESHOLD = sensor.max_angle // 2 # default max_angle is 4095
# you can change the max_angle. ex: sensor.max_angle = 1000
MIN_CHANGE = 25 # minimum change to register as movement
# increase to make less sensitive, decrease to make more sensitive
while True:
enc_val = sensor.angle
if abs(enc_val - last_val) >= MIN_CHANGE or abs(enc_val - last_val) > THRESHOLD:
# Calculate the difference
diff = enc_val - last_val
# Check for wraparound
if diff > THRESHOLD:
# Wrapped from ~4095 to ~0 (actually turning backwards)
cc.send(enc_dec)
elif diff < -THRESHOLD:
# Wrapped from ~0 to ~4095 (actually turning forwards)
cc.send(enc_inc)
elif diff > 0:
# Normal forward rotation
cc.send(enc_inc)
else:
# Normal backward rotation (diff < 0)
cc.send(enc_dec)
last_val = enc_val
Upload the Code and Libraries to the QT Py RP2040
After downloading the Project Bundle, plug your QT Py RP2040 into the computer's USB port with a known good USB data+power cable. You should see a new flash drive appear in the computer's File Explorer or Finder (depending on your operating system) called CIRCUITPY. Unzip the folder and copy the following items to the QT Py RP2040's CIRCUITPY drive.
- lib folder
- code.py
Your QT Py RP2040 CIRCUITPY drive should look like this after copying the lib folder and code.py file:
How the Code Works
At the top of the code, the AS5600 is instantiated over I2C. enc_inc and enc_dec are setup to send the Consumer Control message for volume increase and volume decrease. cc is setup as a Consumer Control USB HID device.
i2c = board.STEMMA_I2C() sensor = adafruit_as5600.AS5600(i2c) enc_inc = ConsumerControlCode.VOLUME_INCREMENT enc_dec = ConsumerControlCode.VOLUME_DECREMENT cc = ConsumerControl(usb_hid.devices)
Variables
A few variables are created before the loop. last_val will hold the last sensor angle reading. THRESHOLD takes the maximum angle of the AS5600 (4095) and divides it by 2 with //. MIN_CHANGE is a user defined value that affects the sensitivity of the encoder. You can increase the value to make the encoder less sensitive or decrease the value to make it more sensitive.
last_val = sensor.angle THRESHOLD = sensor.max_angle // 2 # default max_angle is 4095 # you can change the max_angle. ex: sensor.max_angle = 1000 MIN_CHANGE = 25 # minimum change to register as movement # increase to make less sensitive, decrease to make more sensitive
The Loop
In the loop, the angle is measured with the sensor. If the angle compared to the last measurement is greater than the value of MIN_CHANGE or THRESHOLD, then a volume increase or volume decrease Consumer Control message is sent depending on the direction of the rotation.
The logic used takes wraparound into account, since the measured angle could, for example, jump from 0 to 359 degrees or vice versa when being rotated. The code is setup to pay attention to the direction of the rotation and base the sent Consumer Control message on that value.
while True:
enc_val = sensor.angle
if abs(enc_val - last_val) >= MIN_CHANGE or abs(enc_val - last_val) > THRESHOLD:
# Calculate the difference
diff = enc_val - last_val
# Check for wraparound
if diff > THRESHOLD:
# Wrapped from ~4095 to ~0 (actually turning backwards)
cc.send(enc_dec)
elif diff < -THRESHOLD:
# Wrapped from ~0 to ~4095 (actually turning forwards)
cc.send(enc_inc)
elif diff > 0:
# Normal forward rotation
cc.send(enc_inc)
else:
# Normal backward rotation (diff < 0)
cc.send(enc_dec)
last_val = enc_val
Page last edited October 14, 2025
Text editor powered by tinymce.