code for AS5600 encoder project
CircuitPython code to control CC volume with a magnet and AS5600 sensor
This commit is contained in:
parent
1930a2ff6e
commit
78373685c1
1 changed files with 43 additions and 0 deletions
43
AS5600_Magnetic_Encoder/code.py
Normal file
43
AS5600_Magnetic_Encoder/code.py
Normal file
|
|
@ -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
|
||||||
Loading…
Reference in a new issue