Page 1 of 1

Configuring MCPWM in ESP32

Posted: Sat Jan 17, 2026 10:43 pm
by Pascual
Hi I was trying to configure the MCPWM in ESP32 DEV V3. At first I was trying to configure it manually through the registers and the data sheet, no success. Then I tried to use the "driver/mcpwm.h" library (code below) and it still didn't work.It should give me a both 1 and 0 on the serial or at least something similar to a PWM on the plotter; however it returns me only 0 or 1. Any tips? Kinda new to the ESPs chips. I am using PROGRAM.IO with the arduino framework.

Code: Select all

#include "driver/mcpwm.h"
#include "soc/mcpwm_struct.h"
#include "soc/gpio_struct.h"
#include <Arduino.h>

// Definições
#define PIN_PWM 25
#define UNIT MCPWM_UNIT_0  
#define TIMER MCPWM_TIMER_0 
#define OPERATOR MCPWM_OPR_A 

void setup() {
  Serial.begin(115200);

  mcpwm_gpio_init(UNIT, MCPWM0A, PIN_PWM);

  mcpwm_config_t pwm_config;
  pwm_config.frequency = 1000;         
  pwm_config.cmpr_a = 50;                
  pwm_config.counter_mode = MCPWM_UP_COUNTER;
  pwm_config.duty_mode = MCPWM_DUTY_MODE_0; 

  mcpwm_init(UNIT, TIMER, &pwm_config);

  Serial.println("MCPWM Inicializado!");
}

void loop() {

    Serial.println(((GPIO.in>>25)&0x01));


    delay(100);
  }
}

Re: Configuring MCPWM in ESP32

Posted: Mon Jan 19, 2026 11:11 am
by Minatel
Hi

Have you tried the examples on the Arduino-FOC?

Re: Configuring MCPWM in ESP32

Posted: Mon Jan 19, 2026 5:26 pm
by lbernstone
There's simply no way that you can use a serial output to try to capture PWM activity, as the CPU is only going to run that every 10-20msec (at best). If you want to see the output, use an oscilloscope or an LED attached to the pin. The link Minatel posted has good examples if you really need the MCPWM feature. If you just need a simple PWM, there are examples at https://github.com/espressif/arduino-es ... /AnalogOut

Re: Configuring MCPWM in ESP32

Posted: Mon Jan 19, 2026 7:41 pm
by Pascual
I mean, im sure i wont be able to see the actual PWM wave, however I should be able to see if it is at least working right? If I set the duty cycle to 100 I should always have HIGH as an output, no?

Re: Configuring MCPWM in ESP32

Posted: Tue Jan 20, 2026 3:56 am
by lbernstone
You would need to have the pin set to I/O, and the PWM device will override that to be OUTPUT. Run a wire to another pin and read from there, after setting the pin to INPUT.