ESP32 Motor with Encoder: Wiring, Code and Pitfalls

What does "ESP32 motor with encoder" actually mean?
When people search for an ESP32 motor with encoder, they usually want closed-loop motion: the ESP32 commands a motor (stepper or DC) and reads an encoder to know where the shaft really is. Without the encoder, a stepper can miss steps and a DC motor has no idea of its position; with it, the ESP32 can correct drift, report angle to a PC or a smart-home hub, and stop at limits.
The sources available here show two very different flavours of this:
- A robotics setup where an ESP32 drives TMC2209 stepper drivers, reads AS5600 magnetic encoders over I2C, and streams encoder angles to a Linux PC over micro-ROS (source 2).
- A smart-home setup where an ESP32 running the Arduino Zigbee library presents a motorized blind as a
ZigbeeWindowCoveringendpoint, with explicit flags for whether lift and tilt are encoder-controlled and closed-loop (source 1).
In between sits the hardware question that trips up almost everyone: how to feed an encoder signal that lives at 5 V or higher into an ESP32 pin that is only rated for 3.3 V (source 3).
\2Bi-directional level shifter test with 5 V applied to HV1: only the HV1 and LV1 indicator LEDs should light, showing the shifter passing a signal rather than driving a load.
Which encoder types show up in real ESP32 builds?
The sources describe two kinds of encoder interface, and they behave very differently on the ESP32 side.
| Encoder style | Example in sources | How the ESP32 talks to it | Notes |
|---|---|---|---|
| Magnetic absolute encoder | AS5600 (source 2) | I2C via Wire |
Gives an absolute angle; several on one bus need an I2C multiplexer (PCA9548A at 0x70) |
| Pulse-output motor encoder | Unnamed encoder, 3.5–20 V supply (source 3) | Digital GPIO inputs | Needs level shifting into a 3.3 V-tolerant pin |
The AS5600 route is attractive because it is a bus device: one Wire instance, a multiplexer channel per motor, and you get an angle without counting pulses. The pulse-output route is what most gearmotors with built-in encoders use, and that is where the voltage question in the next section matters.
If you want to time pulses in hardware, the ESP-IDF RMT peripheral is worth knowing about: its receiver samples incoming signals into symbols made of a 15-bit duration and a 1-bit level, and it can be told the basic characteristics of the signal so glitches and noise are filtered out (source 0). The RMT documentation is written around infrared and general-purpose sequences rather than encoders specifically, so treat this as a capability to explore, not a documented encoder recipe.
\2Level shifter idle state with 3.3 V on the LV side and 5 V on the HV side; with no logic signal applied all LEDs should be off, which is the baseline before connecting an encoder.
How do I connect an encoder that runs above 3.3 V to an ESP32?
Source 3 is a cautionary tale worth reading in full. The builder wanted to connect a motor encoder (3.5–20 V supply) to an ESP32-S3 DevKitC, whose IO pins tolerate 3.3 V, through a SparkFun bi-directional logic level shifter (product 12009). The outputs looked noisy and "floating", and neither INPUT_PULLUP nor INPUT_PULLDOWN on the ESP32 fixed it. Adding 4.7 kΩ + LED pull-downs made it worse.
The accepted answer explains why:
- That style of bi-directional shifter is not meant to drive loads. It has internal 10 kΩ pull-ups on each side and is designed for signalling between devices with open-drain outputs that only sink current.
- Adding 4.7 kΩ pull-downs created voltage dividers with the internal 10 kΩ pull-ups; the measured 2 V on the LV side and 4.2 V on the HV side were not floating at all, just the divider output.
- If the encoder has a push-pull output, the shifter can still work, but only as a unidirectional path from HV to LV.
- For that HV-to-LV case, the answer adds a 100 Ω series resistor on the LV side to protect the MCU pin from spikes. Fast edges on the HV side couple through the MOSFET's parasitic capacitance and would otherwise hit the ESP32 pin as transients.
- The HV-side pull-up can be removed when the transmitter is push-pull, though leaving it does no harm.
Practical takeaway: before you wire an encoder to an ESP32, find out whether its outputs are open-drain or push-pull, confirm its supply voltage, and pick a level-shifting approach that matches. Do not try to "fix" a bi-directional MOSFET shifter with extra pull-downs.
\2An ESP32 development board, the kind used as the controller for motor and encoder projects. Image: Edwiyanto, CC BY-SA 4.0, via Wikimedia Commons
What does an ESP32 motor encoder example look like?
Source 2 contains a complete (if partly working) ESP32 motor encoder example built for micro-ROS. The hardware list is instructive:
- ESP32 connected to an Ubuntu 22.04 PC over USB serial (UART0) at 921600 baud
arduino-esp32core downgraded to 2.0.2 for micro-ROS compatibility- TMC2209 stepper drivers configured over UART (
Serial2, RX on GPIO 16, TX on GPIO 17,R_SENSE 0.11, 1000 mA driver current) - AccelStepper for motion, 64 microsteps, 200 steps per revolution
- AS5600 magnetic encoders behind a PCA9548A I2C multiplexer at address
0x70 - Encoder data published every 20 ms
The pin map from that code:
| Signal | GPIO |
|---|---|
| STEP1 / DIR1 | 13 / 4 |
| STEP2 / DIR2 | 25 / 32 |
| TMC2209 UART RX (PDN_UART) | 16 |
| TMC2209 UART TX (SPREAD) | 17 |
| CLK | 18 |
| Status LED | 14 |
The multiplexer wrapper is small enough to reuse directly:
class PCA9548A {
public:
PCA9548A(uint8_t address, TwoWire &wire = Wire)
: _addr(address), _wire(wire) {}
void begin() { _wire.begin(); }
void selectChannel(uint8_t channel) {
if (channel > 7) return;
_wire.beginTransmission(_addr);
_wire.write(1 << channel);
_wire.endTransmission();
}
private:
uint8_t _addr;
TwoWire &_wire;
};
PCA9548A mux(0x70, Wire);
AS5600 encoder(&Wire);
The design uses arrays sized by N_MOTOR (set to 1 for testing) so it can scale to six motors later: one AccelStepper, one TMC2209Stepper and one encoder channel per joint.
On the PC side the micro-ROS agent is launched like this:
ros2 launch robot_launcher robot_launcher.launch.py
with the agent arguments serial --dev /dev/ttyACM0 --baudrate 921600. Note that the author's publisher worked but adding a subscriber for velocity commands sent the ESP32 into an error loop as soon as the agent started; the sources do not include a fix, so treat this example as a solid hardware reference rather than finished firmware.
Can I expose an encoder-controlled motor to a smart-home hub?
Yes, if your motor is driving a blind, shade or similar, the Arduino Zigbee library on ESP32 has an endpoint built for exactly that (source 1). ZigbeeWindowCovering implements the Zigbee Home Automation standard for motorized window coverings and supports lift and tilt position control, several covering types (roller shade, drapery, awning, shutter, blind, projector screen and more), limits, and callbacks.
Two configuration calls matter for encoder builds:
setConfigStatus(...)takes flags forlift_closed_loop,tilt_closed_loop,lift_encoder_controlledandtilt_encoder_controlled, which is how you declare to the network that position feedback comes from an encoder.setMode(...)includesmotor_reversedandcalibration_mode, useful when you first map encoder counts to open/closed positions.
setLimits() then stores installed open and closed limits for lift and tilt, and setLiftPercentage() / setTiltPercentage() report 0–100 % position back to the hub. Callbacks like onGoToLiftPercentage and onStop give you the hooks to drive the motor until the encoder says the target is reached. The library ships a Zigbee_Window_Covering example sketch.
Common pitfalls with ESP32 motor encoders
- Feeding 5 V+ encoder signals straight into the ESP32. ESP32-S3 pins tolerate 3.3 V; many encoders run at 3.5–20 V (source 3). Level shift, and match the shifter to the encoder's output type.
- Misusing a bi-directional MOSFET level shifter. It is for open-drain signalling, not for driving loads. Pull-downs turn it into a voltage divider, and push-pull sources only work one way with a 100 Ω series resistor on the ESP32 side (source 3).
- Expecting
INPUT_PULLUPto cure a wiring problem. In source 3 it did nothing, because the issue was the external shifter, not the ESP32 pin mode. - Losing your only debug port. In source 2, UART0 was both the micro-ROS link and the only serial port, so
Serial.printdebugging was impossible. Plan a second UART or another debug path before you commit pins. - Library version mismatches. The micro-ROS build in source 2 needed the arduino-esp32 core pinned to 2.0.2. Check compatibility before upgrading.
- Running several I2C encoders without a multiplexer. The AS5600 example uses a PCA9548A so each motor's encoder gets its own channel (source 2).
- Treating a partly working example as finished. The micro-ROS code published angles fine but faulted when a subscriber was added, and the sources offer no resolution.
What the sources do not cover
To stay honest: none of these sources documents the ESP32's pulse-counter hardware or gives a quadrature decoding example, nor do they specify DC motor H-bridge wiring, current limits for specific motors, or PID tuning numbers. The RMT documentation (source 0) describes a flexible receiver that can filter glitches and capture signal timing, but does not present it as an encoder solution. If you need those details, verify them against Espressif's documentation for your exact chip rather than guessing from this article.
Frequently asked questions
Can I connect a 5 V motor encoder directly to an ESP32?
Not safely. Source 3 states the ESP32-S3 DevKitC IO pins tolerate 3.3 V while the encoder in question runs on 3.5–20 V, so a level-shifting stage is required.
Why does my bi-directional level shifter output look floating with an encoder?
Because that shifter type is designed for open-drain signalling, not driving loads. Its internal 10 kΩ pull-ups form a divider with any pull-downs you add; the 2 V and 4.2 V readings in source 3 were divider outputs, not floating pins.
Is there an ESP32 motor encoder example with multiple encoders?
Source 2 shows AS5600 magnetic encoders read through a PCA9548A I2C multiplexer at 0x70, with arrays sized by N_MOTOR so the design can grow to six motors.
How do I tell a Zigbee hub that my motor is encoder-controlled?
Use ZigbeeWindowCovering::setConfigStatus(), which has lift_encoder_controlled and tilt_encoder_controlled flags alongside closed-loop flags (source 1).
Which arduino-esp32 version did the micro-ROS encoder example use?
The author downgraded the esp32 core to 2.0.2 to resolve micro-ROS compatibility problems (source 2).
An ESP32 motor with encoder is very achievable, and the AS5600 + TMC2209 micro-ROS build shows a clean, scalable pattern for robotics, while the Zigbee window covering endpoint shows how to surface the same feedback to a smart home. Get the voltage interface right first: match your level shifter to the encoder's output type and add series protection on the ESP32 side. Treat the micro-ROS firmware as a reference, not a drop-in, since its subscriber issue is unresolved in the sources.
Sources
github.comRemote Control Transceiver (RMT) (esp-idf docs)github.comZigbeeWindowCovering (arduino-esp32 docs)stackoverflow.comSubscriber issues on ESP32 running Micro-roselectronics.stackexchange.comBi-directional Logic Level Shifter has floating outputsThis guide summarises the public sources listed above in our own words. Check the original documentation before relying on exact figures.


