ESP32 - What is the minimum PWM frequency?

Hi mprowe,

Here's an example using the ESP32's MCPWM peripheral to generate a 0.1Hz PWM signal, 50% duty-cycle on GPIO pin 27 with timer 0:

// ESP32: Output PWM 0.1Hz (10 second period), 50% duty-cycle on GPIO pin 27
#include "soc/mcpwm_reg.h"
#include "soc/mcpwm_struct.h"
#include "driver/mcpwm.h"

#define GPIO_PWM0A_OUT 27

void setup() {
  mcpwm_gpio_init(MCPWM_UNIT_0, MCPWM0A, GPIO_PWM0A_OUT);     // Initialise channel MCPWM0A on GPIO pin 27
  MCPWM0.clk_cfg.prescale = 199;                // Set the 160MHz clock prescaler to 199 (160MHz/(199+1)=800kHz)
  MCPWM0.timer[0].period.prescale = 199;        // Set timer 0 prescaler to 199 (800kHz/(199+1))=4kHz)
  MCPWM0.timer[0].period.period = 39999;        // Set the PWM period to 0.1Hz (4kHz/(39999+1)=0.1Hz)  
  MCPWM0.channel[0].cmpr_value[0].val = 20000;  // Set the counter compare for 50% duty-cycle
  MCPWM0.channel[0].generator[0].utez = 2;      // Set the PWM0A ouput to go high at the start of the timer period
  MCPWM0.channel[0].generator[0].utea = 1;      // Clear on compare match
  MCPWM0.timer[0].mode.mode = 1;                // Set timer 0 to increment
  MCPWM0.timer[0].mode.start = 2;               // Set timer 0 to free-run
}

void loop() {}

Note that "clk_cfg" and "period" prescaler register bifields are 8-bit (0 to 255 + 1), while "period" is 16-bit (0 to 65535 + 1).

1 Like