Closed-Loop Stepper Motor Position Feedback with the AS5600 Magnetic Encoder and ESP32
An open-loop stepper driver has no idea whether the motor shaft actually moved. It sends a pulse, assumes the rotor advanced one step, and moves on — which is fine until the motor stalls against a jammed axis, skips a step under a sudden load spike, or the belt slips on a pulley, at which point your controller's internal position tracking quietly diverges from reality and nothing tells it that happened. GRBL and Klipper both offer sensorless stall detection on drivers like the TMC2209 (covered elsewhere on this site), but sensorless detection infers a stall from back-EMF and motor current, which gets unreliable at low speed and needs per-motor tuning. A magnetic rotary encoder like the AS5600 takes a more direct approach: it measures the actual shaft angle, continuously, with no mechanical contact.
How the AS5600 Works
The AS5600 is a contactless 12-bit Hall-effect angle sensor. You mount it on a small PCB a couple of millimeters from the end of the motor shaft, and glue or press a small diametrically-magnetized magnet (not a standard flat disc magnet — it must be magnetized through its diameter, not its face) onto the shaft end facing the sensor. As the shaft rotates, the sensor's Hall array reads the magnetic field's rotation and outputs an absolute angle across the full 360 degrees as a 12-bit value (4096 counts per revolution) over I2C, or as an analog voltage/PWM duty cycle if you don't want to deal with I2C timing.
Because the magnet and sensor never touch, there's no mechanical wear, no brushes, and no slip — the angle reading is accurate at any shaft speed the sensor's internal sampling can keep up with, which comfortably covers typical NEMA 17 stepper speeds on router, laser, and printer gantries.
Wiring to an ESP32
AS5600 PinESP32 ConnectionNotes VCC3.3VThe AS5600 is 3.3V-5V tolerant on most breakout boards; check your specific module GNDGNDCommon ground with the stepper driver's logic side SDAAny GPIO (commonly 21)Add 4.7k pull-up if your breakout doesn't already include one SCLAny GPIO (commonly 22)Same pull-up note as SDA DIR (optional)Unused for basic readingSets direction of angle increase; leave at default unless you need it reversedMounting tolerance matters more than wiring: the magnet should sit centered and parallel to the sensor die, with the air gap the datasheet recommends (typically around 0.5-3mm depending on magnet strength). Off-axis mounting introduces measurable angle error, which defeats the purpose of adding a precision sensor in the first place.
Building a Practical Stall / Missed-Step Detector
This is not full closed-loop torque control in the ODrive or servo-motor sense — doing that properly means running field-oriented control at a high loop rate, which is a much bigger project than most maker builds need. What's genuinely useful and achievable with an ESP32 and one AS5600 per axis is missed-step detection: the firmware tracks the position it commanded (from step pulses sent) and periodically compares it against the AS5600's actual measured angle, converted to the same units. If the two diverge by more than an expected tolerance — accounting for normal belt/lead-screw compliance — the firmware flags a fault instead of continuing to execute a toolpath against a position that no longer matches reality.
- Track commanded position in firmware as you already do for step generation (a running count of steps issued, converted to expected shaft angle using your microstepping setting).
- Poll the AS5600 over I2C at a rate fast enough to catch slip before it compounds — polling every 10-50ms per axis is usually sufficient for router/laser gantry speeds; faster if you're protecting a high-speed pick-and-place type motion.
- Compare expected vs measured angle, accounting for gear or belt reduction ratio between the motor shaft (where the sensor lives) and the actual axis.
- On divergence past your tolerance, halt motion and raise an alarm rather than silently continuing — this is the entire value of the sensor. Catching the fault immediately after a stall, instead of discovering a ruined part at the end of a two-hour job, is the whole point.
Where This Is Worth the Extra Wiring
Put an AS5600 on axes where a missed step is expensive to discover late: the Z-axis of a CNC router (a stalled Z can plunge wrong and break a bit or gouge a part), a 3D printer extruder's drive gear shaft (catching a clog or skipped extruder step before an entire print is wasted plastic), or a robotics joint where position accuracy actually matters to the mechanism. It's generally overkill to add one to every axis of a hobby 3D printer's X/Y gantry, where Klipper's existing tuning and lower stakes per failure don't justify the wiring and mounting effort.
Safety
The diametric magnets used with the AS5600 are small but strong enough to pinch skin if two are handled carelessly together, and strong enough to corrupt data on magnetic storage media or interfere with pacemakers at close range — treat them with the same care as any small rare-earth magnet. Keep them away from other Hall-effect sensors and stepper motor bodies during assembly (many stepper housings are lightly magnetic and can pull a loose magnet out of your fingers unexpectedly), and never mount the magnet directly to a rotating shaft without a secure adhesive or mechanical retention — a magnet that detaches at speed becomes a small, fast-moving projectile inside an enclosure full of electronics.
A single AS5600 and a few lines of comparison logic turn "the motor stopped moving and nobody noticed" into an alarm you can act on immediately, for a sensor that costs a few dollars and takes one I2C bus to read — one of the better bang-for-the-wire upgrades available for any ESP32-driven motion project.