ESP32 SPWM Example generator

ESP32 SPWM Code Generator

Experimental ESP32 SPWM Code Generator

Value typically between 0 and 1. (0.8 = 80%)

Desired frequency of the generated sine wave output.

Number of data points in the sine lookup table for one full cycle.

The frequency of the underlying PWM signal from the ESP32 LEDC peripheral.

The ESP32 GPIO pin for PWM output (e.g., 2, 4, 16, 17, etc.).

SPWM on ESP32

Sine Pulse Width Modulation (SPWM) is a technique used to generate a sinusoidal AC waveform from a DC supply. On the ESP32, the built-in LEDC (LED Controller) peripheral is ideal for generating high-resolution PWM signals.

The core idea is to vary the duty cycle of a high-frequency PWM signal sinusoidally over time. By filtering this PWM signal (e.g., with an LC filter), a smooth sinusoidal voltage can be obtained.

How it Works:

  • Modulation Index (Ma): Controls the amplitude of the output sine wave. A higher `Ma` (up to 1.0) results in a larger output voltage.
  • Output Sine Wave Frequency: The desired frequency of the generated AC waveform (e.g., 50 Hz or 60 Hz).
  • Number of Samples per Sine Cycle: Determines the number of discrete duty cycle values that approximate one full sine wave. A higher number of samples leads to a smoother sine wave and reduced harmonic distortion, but requires more memory for the lookup table.
  • PWM Carrier Frequency: This is the frequency at which the PWM pulses themselves are generated. A higher carrier frequency results in a smaller ripple voltage and allows for smaller output filter components. The ESP32 LEDC can operate at very high frequencies.
  • PWM Resolution (bits): Defines the number of distinct duty cycle steps available. For example, 10-bit resolution means 1024 (0-1023) possible duty cycle values. Higher resolution means finer control and better sine wave fidelity.
  • GPIO Pin: The physical pin on the ESP32 where the PWM signal will be output.

Code Structure:

The generated code utilizes the ESP32’s LEDC driver.
1. A lookup table is pre-calculated based on the sine wave parameters (Modulation Index, Samples, Resolution). 2. In `setup()`, the LEDC channel is configured with the chosen PWM carrier frequency and resolution, and then attached to the specified GPIO pin. 3. In `loop()`, a timer-based mechanism (using `micros()`) is used to iterate through the lookup table at a rate that produces the desired output sine wave frequency. For each step, `ledcWrite()` is called to update the duty cycle.
This code generates a **unipolar PWM signal**. For driving a full H-bridge (to get a bipolar AC output), you would typically use this signal for one leg and a complementary signal (or another phase-shifted SPWM) for the other leg.

Note: This code provides a basic SPWM implementation. For real-world applications, consider adding dead-time insertion for H-bridge drivers, current/voltage feedback loops, overcurrent protection, and more robust timing mechanisms (e.g., hardware timers for precise sample updates).