File tree Expand file tree Collapse file tree 1 file changed +43
-0
lines changed Expand file tree Collapse file tree 1 file changed +43
-0
lines changed Original file line number Diff line number Diff line change
1
+ # SPDX-FileCopyrightText: Copyright (c) 2025 Liz Clark for Adafruit Industries
2
+ #
3
+ # SPDX-License-Identifier: MIT
4
+ """AS5600 Encoder"""
5
+ import usb_hid
6
+ import board
7
+ from adafruit_hid .consumer_control import ConsumerControl
8
+ from adafruit_hid .consumer_control_code import ConsumerControlCode
9
+ import adafruit_as5600
10
+
11
+ i2c = board .STEMMA_I2C ()
12
+ sensor = adafruit_as5600 .AS5600 (i2c )
13
+ enc_inc = ConsumerControlCode .VOLUME_INCREMENT
14
+ enc_dec = ConsumerControlCode .VOLUME_DECREMENT
15
+ cc = ConsumerControl (usb_hid .devices )
16
+
17
+ last_val = sensor .angle
18
+
19
+ THRESHOLD = sensor .max_angle // 2 # default max_angle is 4095
20
+ # you can change the max_angle. ex: sensor.max_angle = 1000
21
+
22
+ MIN_CHANGE = 25 # minimum change to register as movement
23
+ # increase to make less sensitive, decrease to make more sensitive
24
+
25
+ while True :
26
+ enc_val = sensor .angle
27
+ if abs (enc_val - last_val ) >= MIN_CHANGE or abs (enc_val - last_val ) > THRESHOLD :
28
+ # Calculate the difference
29
+ diff = enc_val - last_val
30
+ # Check for wraparound
31
+ if diff > THRESHOLD :
32
+ # Wrapped from ~4095 to ~0 (actually turning backwards)
33
+ cc .send (enc_dec )
34
+ elif diff < - THRESHOLD :
35
+ # Wrapped from ~0 to ~4095 (actually turning forwards)
36
+ cc .send (enc_inc )
37
+ elif diff > 0 :
38
+ # Normal forward rotation
39
+ cc .send (enc_inc )
40
+ else :
41
+ # Normal backward rotation (diff < 0)
42
+ cc .send (enc_dec )
43
+ last_val = enc_val
You can’t perform that action at this time.
0 commit comments