Simple test
Ensure your device works with this simple test.
examples/as5600_simpletest.py
1# SPDX-FileCopyrightText: Copyright (c) 2025 Liz Clark for Adafruit Industries
2#
3# SPDX-License-Identifier: MIT
4"""AS5600 Simple Test"""
5
6import time
7
8import board
9
10import adafruit_as5600
11
12i2c = board.I2C()
13sensor = adafruit_as5600.AS5600(i2c)
14
15while True:
16 # Read angle values
17 if sensor.magnet_detected:
18 if sensor.max_gain_overflow is True:
19 print("Magnet is too weak")
20 if sensor.min_gain_overflow is True:
21 print("Magnet is too strong")
22 print(f"Raw angle: {sensor.raw_angle}")
23 print(f"Scaled angle: {sensor.angle}")
24 print(f"Magnitude: {sensor.magnitude}")
25 else:
26 print("Waiting for magnet..")
27 print()
28 time.sleep(2)
Full test
Full test of the library
examples/as5600_fulltest.py
1# SPDX-FileCopyrightText: Copyright (c) 2025 Liz Clark for Adafruit Industries
2#
3# SPDX-License-Identifier: MIT
4
5"""
6Full library testing example for the Adafruit AS5600 CircuitPython library
7
8This example tests all functionality of the AS5600 magnetic angle sensor
9"""
10
11import time
12
13import board
14
15import adafruit_as5600
16
17# Initialize I2C and AS5600
18i2c = board.I2C() # uses board.SCL and board.SDA
19as5600 = adafruit_as5600.AS5600(i2c)
20
21print("Adafruit AS5600 Full Test")
22print("AS5600 found!")
23print()
24
25# Test zm_count property
26zm_count = as5600.zm_count
27print(f"ZM Count (burn count): {zm_count}")
28
29# Test z_position property
30z_pos = as5600.z_position
31print(f"Z Position: {z_pos}")
32
33# Test setting z_position (XOR current value with 0xADA to change it)
34test_pos = (z_pos ^ 0xADA) & 0x0FFF # XOR with 0xADA and keep within 12-bit range
35print(f"Setting Z Position to {test_pos} (0x{test_pos:03X})... ")
36try:
37 as5600.z_position = test_pos
38 print("Success")
39 new_z_pos = as5600.z_position
40 print(f"New Z Position: {new_z_pos} (0x{new_z_pos:03X})")
41except Exception as e:
42 print(f"Failed: {e}")
43
44# Test m_position property
45m_pos = as5600.m_position
46print(f"M Position: {m_pos}")
47
48# Test setting m_position (XOR current value with 0xBEE)
49test_m_pos = (m_pos ^ 0xBEE) & 0x0FFF
50print(f"Setting M Position to {test_m_pos} (0x{test_m_pos:03X})... ")
51try:
52 as5600.m_position = test_m_pos
53 print("Success")
54 new_m_pos = as5600.m_position
55 print(f"New M Position: {new_m_pos} (0x{new_m_pos:03X})")
56except Exception as e:
57 print(f"Failed: {e}")
58
59# Test max_angle property
60max_angle = as5600.max_angle
61print(f"Max Angle: {max_angle}")
62
63# Test setting max_angle (XOR current value with 0xCAB)
64test_max_angle = (max_angle ^ 0xCAB) & 0x0FFF
65print(f"Setting Max Angle to {test_max_angle} (0x{test_max_angle:03X})... ")
66try:
67 as5600.max_angle = test_max_angle
68 print("Success")
69 new_max_angle = as5600.max_angle
70 print(f"New Max Angle: {new_max_angle} (0x{new_max_angle:03X})")
71except Exception as e:
72 print(f"Failed: {e}")
73
74# Test watchdog property
75print("Turning on watchdog... ")
76try:
77 as5600.watchdog = True
78 print("Success")
79 print(f"Watchdog status: {'ENABLED' if as5600.watchdog else 'DISABLED'}")
80except Exception as e:
81 print(f"Failed: {e}")
82
83print("Turning off watchdog...")
84try:
85 as5600.watchdog = False
86 print("Success")
87 print(f"Watchdog status: {'ENABLED' if as5600.watchdog else 'DISABLED'}")
88except Exception as e:
89 print(f"Failed: {e}")
90
91# Test power_mode property
92print("Setting power mode...")
93try:
94 as5600.power_mode = adafruit_as5600.POWER_MODE_NOM
95 print("Success")
96 mode = as5600.power_mode
97 print("Power mode: ")
98 if mode == adafruit_as5600.POWER_MODE_NOM:
99 print("Normal")
100 elif mode == adafruit_as5600.POWER_MODE_LPM1:
101 print("Low Power Mode 1")
102 elif mode == adafruit_as5600.POWER_MODE_LPM2:
103 print("Low Power Mode 2")
104 elif mode == adafruit_as5600.POWER_MODE_LPM3:
105 print("Low Power Mode 3")
106except Exception as e:
107 print(f"Failed: {e}")
108
109# Test hysteresis property
110print("Setting hysteresis...")
111try:
112 as5600.hysteresis = adafruit_as5600.HYSTERESIS_OFF
113 print("Success")
114 hysteresis = as5600.hysteresis
115 print("Hysteresis: ")
116 if hysteresis == adafruit_as5600.HYSTERESIS_OFF:
117 print("OFF")
118 elif hysteresis == adafruit_as5600.HYSTERESIS_1LSB:
119 print("1 LSB")
120 elif hysteresis == adafruit_as5600.HYSTERESIS_2LSB:
121 print("2 LSB")
122 elif hysteresis == adafruit_as5600.HYSTERESIS_3LSB:
123 print("3 LSB")
124except Exception as e:
125 print(f"Failed: {e}")
126
127# Test output_stage property
128print("Setting output stage...")
129try:
130 as5600.output_stage = adafruit_as5600.OUTPUT_STAGE_ANALOG_FULL
131 print("Success")
132 output_stage = as5600.output_stage
133 print("Output stage: ")
134 if output_stage == adafruit_as5600.OUTPUT_STAGE_ANALOG_FULL:
135 print("Analog Full (0% to 100%)")
136 elif output_stage == adafruit_as5600.OUTPUT_STAGE_ANALOG_REDUCED:
137 print("Analog Reduced (10% to 90%)")
138 elif output_stage == adafruit_as5600.OUTPUT_STAGE_DIGITAL_PWM:
139 print("Digital PWM")
140 elif output_stage == adafruit_as5600.OUTPUT_STAGE_RESERVED:
141 print("Reserved")
142except Exception as e:
143 print(f"Failed: {e}")
144
145# Test pwm_frequency property
146print("Setting PWM frequency...")
147try:
148 as5600.pwm_frequency = adafruit_as5600.PWM_FREQ_115HZ
149 print("Success")
150 pwm_freq = as5600.pwm_frequency
151 print("PWM frequency: ")
152 if pwm_freq == adafruit_as5600.PWM_FREQ_115HZ:
153 print("115 Hz")
154 elif pwm_freq == adafruit_as5600.PWM_FREQ_230HZ:
155 print("230 Hz")
156 elif pwm_freq == adafruit_as5600.PWM_FREQ_460HZ:
157 print("460 Hz")
158 elif pwm_freq == adafruit_as5600.PWM_FREQ_920HZ:
159 print("920 Hz")
160except Exception as e:
161 print(f"Failed: {e}")
162
163# Test slow_filter property
164print("Setting slow filter to 16x (options: 16X=0, 8X=1, 4X=2, 2X=3)... ")
165try:
166 as5600.slow_filter = adafruit_as5600.SLOW_FILTER_16X
167 print("Success")
168 slow_filter = as5600.slow_filter
169 print("Slow filter: ")
170 if slow_filter == adafruit_as5600.SLOW_FILTER_16X:
171 print("16x")
172 elif slow_filter == adafruit_as5600.SLOW_FILTER_8X:
173 print("8x")
174 elif slow_filter == adafruit_as5600.SLOW_FILTER_4X:
175 print("4x")
176 elif slow_filter == adafruit_as5600.SLOW_FILTER_2X:
177 print("2x")
178except Exception as e:
179 print(f"Failed: {e}")
180
181# Test fast_filter_threshold property
182print("Setting fast filter threshold... ")
183try:
184 as5600.fast_filter_threshold = adafruit_as5600.FAST_FILTER_SLOW_ONLY
185 print("Success")
186 fast_thresh = as5600.fast_filter_threshold
187 print("Fast filter threshold: ", end="")
188 if fast_thresh == adafruit_as5600.FAST_FILTER_SLOW_ONLY:
189 print("Slow filter only")
190 elif fast_thresh == adafruit_as5600.FAST_FILTER_6LSB:
191 print("6 LSB")
192 elif fast_thresh == adafruit_as5600.FAST_FILTER_7LSB:
193 print("7 LSB")
194 elif fast_thresh == adafruit_as5600.FAST_FILTER_9LSB:
195 print("9 LSB")
196 elif fast_thresh == adafruit_as5600.FAST_FILTER_18LSB:
197 print("18 LSB")
198 elif fast_thresh == adafruit_as5600.FAST_FILTER_21LSB:
199 print("21 LSB")
200 elif fast_thresh == adafruit_as5600.FAST_FILTER_24LSB:
201 print("24 LSB")
202 elif fast_thresh == adafruit_as5600.FAST_FILTER_10LSB:
203 print("10 LSB")
204except Exception as e:
205 print(f"Failed: {e}")
206
207# Reset position settings to defaults
208print("\nResetting position settings to defaults...")
209as5600.z_position = 0
210as5600.m_position = 4095
211as5600.max_angle = 4095
212
213print("\nStarting continuous angle reading...")
214print("=" * 80)
215
216# Continuously read and display angle values
217while True:
218 # Get angle readings
219 raw_angle = as5600.raw_angle
220 angle = as5600.angle
221
222 # Build output string
223 output = f"Raw: {raw_angle:4d} (0x{raw_angle:03X}) | Scaled: {angle:4d} (0x{angle:03X})"
224
225 # Check status conditions
226 if as5600.magnet_detected:
227 output += " | Magnet: YES"
228 else:
229 output += " | Magnet: NO "
230
231 if as5600.min_gain_overflow:
232 output += " | MH: magnet too strong"
233
234 if as5600.max_gain_overflow:
235 output += " | ML: magnet too weak"
236
237 # Get AGC and Magnitude values
238 agc = as5600.agc
239 magnitude = as5600.magnitude
240 output += f" | AGC: {agc:3d} | Mag: {magnitude:4d}"
241
242 print(output)
243 time.sleep(2)