Skip to content

code for AS5600 encoder project #3093

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 1, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions AS5600_Magnetic_Encoder/code.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# 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