# Open-Source ESP32 Autofocus & 3D Deep-Engraving Z-Axis Controller for Galvo Fiber Lasers (MKS SERVO57D / Step-Dir)

**URL:** <https://forum.lightburnsoftware.com/t/open-source-esp32-autofocus-3d-deep-engraving-z-axis-controller-for-galvo-fiber-lasers-mks-servo57d-step-dir/194251>\
**Category:** Finished Creations\
**Tags:** rotary, homing, firmware, galvo\
**Created:** [November 29, 2026, 9:29pm UTC](https://forum.lightburnsoftware.com/t/open-source-esp32-autofocus-3d-deep-engraving-z-axis-controller-for-galvo-fiber-lasers-mks-servo57d-step-dir/194251 "2026-11-29T21:29:48Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![oz1sms](https://forum.lightburnsoftware.com/letter_avatar_proxy/v4/letter/o/b5ac83/32.png) [@oz1sms](https://forum.lightburnsoftware.com/u/oz1sms)\
**Post date:** [November 29, 2026, 9:29pm UTC](https://forum.lightburnsoftware.com/t/open-source-esp32-autofocus-3d-deep-engraving-z-axis-controller-for-galvo-fiber-lasers-mks-servo57d-step-dir/194251/1 "2026-11-29T21:29:48Z")

</div>

Hi everyone,

I wanted to share an open-source ESP32 controller I built for automating the Z-axis on my Galvo fiber laser workstation (Raycus 30W with LightBurn).

Switching between different F-Theta lenses, varying workpiece thicknesses, and dialing in repeatable defocus offsets for stainless steel colors used to require manual cranking and measuring every time. To solve this, I wrote a modular C++ / PlatformIO firmware for the **ESP32-WROOM-DA** paired with a **2.42" OLED display** , an **EC11 rotary encoder** , and an **MKS SERVO57D** closed-loop motor over RS485 (standard Step/Dir drivers are also supported).

**Main features:**

- **Multi-Lens & Material Focus:** Stores calibrated focal distances for up to 6 F-Theta lenses (`70x70` up to `300x300`), calculates $Z = H\_{\text{material}} + F\_{\text{lens}} + \Delta Z\_{\text{color}}$ automatically, and includes built-in crash limits.

- **Stainless Steel Color Offsets:** 4 quick-select defocus presets (`C1`–`C4`) for repeatable color marking.

- **3D Deep Relief Engraving Mode:** Enter the total job duration (`HH:MM:SS`) and desired Z-depth (`mm`) — the controller sends an isolated relay start pulse to LightBurn’s pedal input, linearly lowers the Z-axis throughout the run to keep the focal plane inside the engraving, and retracts automatically when done.

- **Power-Loss Z Persistence:** Saves Z-height to NVRAM and syncs with the MKS SERVO57D 48-bit encoder at boot so the controller remembers its exact Z-position across full power cycles.

- **Two-Stage Precision Homing & In-Menu Calibration:** Includes a first-boot setup wizard, live focus calibration, and built-in motor calibration.

**GitHub Repository (Firmware, Pinouts & Documentation):**

> **[GitHub - oz1sms/Galvo-Z: ESP32 motorized Z-axis and autofocus controller...](https://github.com/oz1sms/Galvo-Z)**
>
> ESP32 motorized Z-axis and autofocus controller for Galvo fiber lasers with MKS SERVO57D (RS485), multi-lens presets, stainless steel color offsets, and LightBurn 3D engraving trigger.

Next step is designing a dedicated KiCad PCB to house everything cleanly inside the control box. Feedback, ideas, or pull requests are very welcome!
