Controlling LEDC timers of the ESP32 microcontroller using registers
Abstract
Relevance. This paper examines precise generation and control of pulse-width modulation (PWM) signals using the LEDC (LED PWM Controller) subsystem of the ESP32 microcontroller via direct register access. As embedded real-time systems increasingly require fine timing control in LED drivers, motor control and power electronics, standard high-level driver APIs can be insufficient. Direct register manipulation of LEDC enables more precise tuning of frequency, resolution and pulse timing, which is critical for synchronization-sensitive applications.
Objective. To analyze the capabilities of ESP32 LEDC timers when configured through direct register writes, to experimentally evaluate the accuracy and stability of generated PWM signals across representative configurations, and to provide practical recommendations for optimizing LEDC parameters in applied embedded projects.
Methods. The investigation employed low-level register programming under Espressif’s ESP-IDF on an ESP32-DevKitC V4 (WROOM-32D). Time-domain characteristics of the PWM outputs were measured with a Logic Analyzer (24 MHz sampling, 8 channels). The study combined theoretical derivations of PWM frequency and period based on clock source, divider (DIV) and counter resolution (RES) with implementation of direct register sequences to configure HSTIMER0 and HS channel 0, and comparative measurements for eighteen distinct configurations covering multiple RES, DIV and DUTY values.
Results. The register-based control method enabled generation of high-frequency PWM in the MHz range with close agreement between calculated and measured values. Across tested configurations the maximum relative deviation did not exceed ±0.03% for frequency and period, and ±0.6% for pulse high-time (duty width). Increasing counter resolution improved duty-cycle granularity, while the prescaler DIV produced a linear change in PWM frequency. The experimental limitations observed at the highest frequencies are attributable to the finite sampling capability of the measurement equipment.
Conclusions. Direct register access to the LEDC allows for obtaining deterministic, high-precision PWM signals with minimal parameter update latency, making them suitable for applications in robotics, power electronics, and other systems with high synchronization requirements. Further research is recommended on the influence of alternative clock sources, low-speed LEDC modes, integration with ISR/FreeRTOS, and extending the approach to other timers and channels.
Downloads
References
/References
Valvano J.W., Yerraballi R. (2014). Embedded Systems — Shape the World: A Cyber-Physical Systems Approach [e-book]. Austin, TX: The University of Texas at Austin. Available at: https://users.ece.utexas.edu/~valvano/Volume1/E-Book/
Barr M. (1999). Programming Embedded Systems in C and C++. O’Reilly. 174 p. Available at: https://archive.org/details/programmingembed0000barr
Microchip Technology Inc. (2015). ATmega328P — 8-bit AVR Microcontroller with 32K Bytes In-System Programmable Flash. Datasheet. [Electronic resource]. Available at: https://ww1.microchip.com/downloads/en/DeviceDoc/Atmel-7810-Automotive-Microcontrollers-ATmega328P_Datasheet.pdf (accessed: 22.09.2025).
Microchip Technology Inc. (2014). ATmega640/1280/1281/2560/2561 — 8-bit AVR Microcontroller. Datasheet. [Electronic resource]. Available at: https://ww1.microchip.com/downloads/en/devicedoc/atmel-2549-8-bit-avr-microcontroller-atmega640-1280-1281-2560-2561_datasheet.pdf (accessed: 22.09.2025).
Kotvytskyi, A. T. (2024). Intellectual capital as a basis for innovative development: robotic systems. In Monographic series «European Science» (Book 28, Part 1). Karlsruhe: ScientificWorld-NetAkhatAV. 168 p. ISBN 978-3-98924-041-4. DOI: 10.30890/2709-2313.2024-28-01. Available at: https://desymp.promonograph.org/index.php/sge/issue/view/sge28-01/sge28-01
Arduino Store (2025). Arduino UNO Rev3 – Official Product Page. Arduino AG, Italy. [Electronic resource]. Available at: https://store.arduino.cc/collections/uno (accessed: 07.10.2025).
Arduino Store (2025). Arduino MEGA 2560 Rev3 – Official Product Page. Arduino AG, Italy. [Electronic resource]. Available at: https://store.arduino.cc/collections/giga (accessed: 07.10.2025).
Espressif Systems Official Store (2025). Official Manufacturer Page on AliExpress. Espressif Systems. [Electronic resource]. Available at: https://www.aliexpress.com/store/1100220184 (accessed: 07.10.2025).
Arduino (n.d.). Getting Started with Arduino — official documentation (Arduino Docs). [Electronic resource]. Available at: https://docs.arduino.cc/learn/starting-guide/getting-started-arduino/ (accessed: 01.10.2025).
All About Circuits (2021). Pulse-width Modulation (PWM) Timers in Microcontrollers. [Electronic resource]. Available at: https://www.allaboutcircuits.com/technical-articles/introduction-to-microcontroller-timers-pwm-timers/ (accessed: 01.10.2025).
University of Washington (2010). Lecture 7: ATmega328 Timers and Interrupts (Course CSE P567: Embedded Systems). Seattle: University of Washington. 32 p. Available at: https://courses.cs.washington.edu/courses/csep567/10wi/lectures/Lecture7.pdf
Espressif Systems (2020). Technical Reference Manual for ESP32. Version 5.5. 661 p. [Electronic resource]. Available at: https://www.espressif.com/sites/default/files/documentation/esp32_technical_reference_manual_en.pdf (accessed: 01.10.2025).
Espressif Systems (2024). LED Control (LEDC) – Programming Guide for ESP32. ESP-IDF v5.5.1. [Electronic resource]. Available at: https://docs.espressif.com/projects/esp-idf/en/stable/esp32/api-reference/peripherals/ledc.html (accessed: 01.10.2025).
Saleae Support (2025). Using Logic. [Electronic resource]. Available at: https://support.saleae.com/user-guide/using-logic (accessed: 01.10.2025).
Valvano J.W., Yerraballi R. (2014). Embedded Systems — Shape the World: A Cyber-Physical Systems Approach [e-book]. Austin, TX: The University of Texas at Austin. Available at: https://users.ece.utexas.edu/~valvano/Volume1/E-Book/
Barr M. (1999). Programming Embedded Systems in C and C++. O’Reilly. 174 p. Available at: https://archive.org/details/programmingembed0000barr
Microchip Technology Inc. (2015). ATmega328P — 8-bit AVR Microcontroller with 32K Bytes In-System Programmable Flash. Datasheet. [Electronic resource]. Available at: https://ww1.microchip.com/downloads/en/DeviceDoc/Atmel-7810-Automotive-Microcontrollers-ATmega328P_Datasheet.pdf (accessed: 22.09.2025).
Microchip Technology Inc. (2014). ATmega640/1280/1281/2560/2561 — 8-bit AVR Microcontroller. Datasheet. [Electronic resource]. Available at: https://ww1.microchip.com/downloads/en/devicedoc/atmel-2549-8-bit-avr-microcontroller-atmega640-1280-1281-2560-2561_datasheet.pdf (accessed: 22.09.2025).
Kotvytskyi, A. T. (2024). Intellectual capital as a basis for innovative development: robotic systems. In Monographic series «European Science» (Book 28, Part 1). Karlsruhe: ScientificWorld-NetAkhatAV. 168 p. ISBN 978-3-98924-041-4. DOI: 10.30890/2709-2313.2024-28-01. Available at: https://desymp.promonograph.org/index.php/sge/issue/view/sge28-01/sge28-01
Arduino Store (2025). Arduino UNO Rev3 – Official Product Page. Arduino AG, Italy. [Electronic resource]. Available at: https://store.arduino.cc/collections/uno (accessed: 07.10.2025).
Arduino Store (2025). Arduino MEGA 2560 Rev3 – Official Product Page. Arduino AG, Italy. [Electronic resource]. Available at: https://store.arduino.cc/collections/giga (accessed: 07.10.2025).
Espressif Systems Official Store (2025). Official Manufacturer Page on AliExpress. Espressif Systems. [Electronic resource]. Available at: https://www.aliexpress.com/store/1100220184 (accessed: 07.10.2025).
Arduino (n.d.). Getting Started with Arduino — official documentation (Arduino Docs). [Electronic resource]. Available at: https://docs.arduino.cc/learn/starting-guide/getting-started-arduino/ (accessed: 01.10.2025).
All About Circuits (2021). Pulse-width Modulation (PWM) Timers in Microcontrollers. [Electronic resource]. Available at: https://www.allaboutcircuits.com/technical-articles/introduction-to-microcontroller-timers-pwm-timers/ (accessed: 01.10.2025).
University of Washington (2010). Lecture 7: ATmega328 Timers and Interrupts (Course CSE P567: Embedded Systems). Seattle: University of Washington. 32 p. Available at: https://courses.cs.washington.edu/courses/csep567/10wi/lectures/Lecture7.pdf
Espressif Systems (2020). Technical Reference Manual for ESP32. Version 5.5. 661 p. [Electronic resource]. Available at: https://www.espressif.com/sites/default/files/documentation/esp32_technical_reference_manual_en.pdf (accessed: 01.10.2025).
Espressif Systems (2024). LED Control (LEDC) – Programming Guide for ESP32. ESP-IDF v5.5.1. [Electronic resource]. Available at: https://docs.espressif.com/projects/esp-idf/en/stable/esp32/api-reference/peripherals/ledc.html (accessed: 01.10.2025).
Saleae Support (2025). Using Logic. [Electronic resource]. Available at: https://support.saleae.com/user-guide/using-logic (accessed: 01.10.2025).