[KongchangAI]
· 2 min read· 1,253 words

ESP32 + NEMA 17 Stepper Motor Self-Balancing Robot Open-Source Project

ESP32 + NEMA 17 Stepper Motor Self-Balancing Robot Open-Source Project

Developer builds a self-balancing robot with ESP32 and stepper motors, open-sourcing all code and a web tuning tool.

This article introduces an atypical self-balancing robot project based on ESP32 and two NEMA 17 stepper motors. Instead of the common DC motor plus encoder approach, the author leverages the stepper motors' high low-speed torque and open-loop positioning accuracy, using hardware timer interrupts to generate step pulses and directly manipulating GPIO registers for real-time performance. The system uses MPU6050 for attitude data, PID for control decisions, and DRV8825 to drive the stepper motors in 16× microstep mode. The project also includes a web interface and wireless control for real-time adjustment of PID parameters and angle offset, significantly improving the tuning experience. The control logic draws on two open-source projects, YARB and B-ROBOT EVO2, and has been verified on two body sizes; all source code is open-sourced on GitHub.

An Atypical Self-Balancing Approach: Why Choose Stepper Motors?

In the field of self-balancing robots, DC gear motors with encoders are almost the default choice—cheap, fast response, and simple control. But a developer on Reddit went the opposite way, building a self-balancing robot with ESP32 and two NEMA 17 stepper motors, and has open-sourced all the source code.

reddit source: I built ESP32 self-balancing robot using NEMA 17 stepper motors

This choice itself is worth pondering. Stepper motors offer high torque at low speeds and high positioning accuracy, and can achieve precise step control without additional encoders—under 16× microstepping, the rotation angle per pulse is very small. The cost is that response speed and high-speed performance are inferior to DC motors, and the real-time requirements for pulse generation are higher. The author uses hardware timer interrupts to generate step pulses and directly manipulates GPIO registers to ensure efficiency—this is almost the approach of using a microcontroller as a software motion controller.

Microstepping is a control technique for stepper motor drivers. By making the current in the motor coils vary sinusoidally, it subdivides a full step (e.g., the common 1.8° for NEMA 17) into multiple smaller step angles. With 16× microstepping, for example, each pulse corresponds to only 0.1125°, theoretically significantly improving motion smoothness and positioning resolution. However, microstepping does not increase the absolute accuracy of the motor; its actual effect is influenced by load, drive voltage, and current loop performance. At high microstep settings, the torque per step also decreases, so a trade-off between smoothness and dynamic response is needed.

The open-loop characteristics of stepper motors are also key to understanding this approach. Stepper motors rotate according to the number of pulses, enabling position control without encoders, but this also means that once steps are lost (e.g., due to sudden load changes or excessive acceleration), the controller cannot detect it. In a self-balancing robot, this means the system must have sufficient torque margin and rely on feedback from the attitude sensor (MPU6050) for indirect correction, rather than directly relying on motor position feedback.

Hardware Composition and Core Control Chain

The project's hardware list is very restrained: ESP32 main controller, two NEMA 17 stepper motors, two DRV8825 driver boards, and one MPU6050 six-axis sensor. The entire system runs in 16× microstep mode.

The control chain can be divided into three layers:

The attitude sensing layer is handled by the MPU6050, providing raw data for tilt angle estimation. The author did not elaborate on filtering details, but mentioned using the MPU6050 for angle estimation, which usually means dealing with the fusion of accelerometer and gyroscope data.

The control decision layer uses a PID algorithm. Traditional self-balancing vehicles mostly use cascaded PID (angle loop + speed loop); from the description, this project at least implements PID control for the angle loop.

The execution output layer is the most distinctive part. Stepper motors require precise pulse timing; the author uses hardware timer interrupts to generate step pulses and reduces overhead by directly accessing GPIO registers. This style is uncommon in the Arduino ecosystem and is closer to low-level embedded development.

PID control is the core algorithm of a self-balancing robot. It calculates the control output based on attitude error (the deviation between the target angle and the actual angle), where the proportional term (P) determines the response strength, the integral term (I) eliminates steady-state error, and the derivative term (D) suppresses oscillation. In a self-balancing scenario, the output of the angle-loop PID is usually mapped to motor motion commands. Since stepper motors control speed by pulse frequency, the PID output needs to be converted into pulse frequency or step count, which requires pulse generation to be sufficiently real-time and smooth; otherwise, quantization jitter is introduced.

Hardware timer interrupts are a common means of achieving precise pulse timing on the ESP32. Compared with software delays, hardware timers are not affected by main loop task scheduling and can trigger interrupts with microsecond-level precision, thereby stably toggling GPIO to generate step pulses. Directly manipulating GPIO registers (instead of calling digitalWrite) bypasses the function call overhead of the Arduino framework and minimizes the execution time of the interrupt service routine, avoiding lost steps at high pulse frequencies. This low-level optimization approach is almost essential in high-speed stepper control.

Tuning Experience: Web Interface + Wireless Control

PID tuning is the most tedious part of a self-balancing project. The author has evidently suffered through this, so they added wireless control and a web interface to the robot, allowing PID parameters and angle offset to be adjusted directly in the browser.

This design decision has high practical value. Traditional tuning either involves repeatedly flashing firmware or connecting a serial cable to view data, whereas the web interface lets developers stand next to the robot with a phone, modify parameters in real time, and observe the balancing effect. The author also explicitly mentions that this feature is "very useful" during tuning.

For those who want to reproduce this project, this wireless tuning tool lowers the barrier—you can stabilize the robot in the browser without understanding every register operation.

Angle offset is a practical parameter in self-balancing control, used to compensate for the robot's mechanical zero-position deviation. Due to sensor installation errors, center-of-gravity shifts, or body asymmetry, the robot's "physical upright point" often does not coincide with the 0° read by the MPU6050. By adjusting this offset in real time through the web interface, developers can calibrate the balance point without re-flashing firmware, which is especially convenient after changing the body or adjusting battery position. Together with PID parameters, it forms the core variables of tuning: PID determines "how to correct," and angle offset determines "which target to correct to."

Standing on the Shoulders of YARB and B-ROBOT EVO2

The author admits that the project draws heavily on two open-source self-balancing robot projects: YARB and B-ROBOT EVO2. These two projects are somewhat well-known in the self-balancing control community; the former is known for its concise Arduino implementation, while the latter offers a more complete engineering solution in mechanical structure and control strategy.

This path of "learn—reproduce—modify" is worth noting. The author did not invent the algorithm from scratch but replaced the actuator (stepper motors instead of DC motors) on top of mature solutions and added the toolchain they needed (web tuning interface). This is exactly the healthiest form of collaboration in the open-source hardware community: reuse control logic and differentiate in hardware selection and engineering implementation.

The author also tested two robot bodies of different sizes, indicating that the control scheme has some adaptability to changes in mechanical parameters.

Who Is This Project For

If you are working on a self-balancing project, this repository has several parts you can directly reuse: the method of generating stepper pulses with hardware timers, optimization techniques for directly manipulating GPIO registers, and a web-based real-time PID tuning solution.

If you just want to understand the basic principles of self-balancing control, the value of this project lies in its complete presentation of the trade-offs of a non-standard approach—why stepper motors are feasible, what the costs are, and how to compensate for hardware characteristics with software. The author also recorded a lengthy video covering robot demonstration, assembly process, and code explanation, which is friendly for those who want to reproduce it hands-on.

The source code is hosted on GitHub (xamechanics/xabalancing), and the author is also soliciting feedback and suggestions. For an open-source hardware project still being iterated, community real-world feedback is often more valuable than the code itself.

#ESP32#NEMA 17#stepper motor#self-balancing robot#PID control#open-source hardware
Share:

Related articles