Configuring MCPWM in ESP32

Pascual
Posts: 2
Joined: Sat Jan 17, 2026 10:38 pm

Configuring MCPWM in ESP32

Postby Pascual » Sat Jan 17, 2026 10:43 pm

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);
  }
}

Minatel
Espressif staff
Espressif staff
Posts: 433
Joined: Mon Jan 04, 2021 2:06 pm

Re: Configuring MCPWM in ESP32

Postby Minatel » Mon Jan 19, 2026 11:11 am

Hi

Have you tried the examples on the Arduino-FOC?

lbernstone
Posts: 1132
Joined: Mon Jul 22, 2019 3:20 pm

Re: Configuring MCPWM in ESP32

Postby lbernstone » Mon Jan 19, 2026 5:26 pm

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

Pascual
Posts: 2
Joined: Sat Jan 17, 2026 10:38 pm

Re: Configuring MCPWM in ESP32

Postby Pascual » Mon Jan 19, 2026 7:41 pm

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?

lbernstone
Posts: 1132
Joined: Mon Jul 22, 2019 3:20 pm

Re: Configuring MCPWM in ESP32

Postby lbernstone » Tue Jan 20, 2026 3:56 am

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.

Who is online

Users browsing this forum: PerplexityBot and 3 guests