# Changing Max PWM Frequency for ESP32S3

**URL:** https://community.simplefoc.com/t/changing-max-pwm-frequency-for-esp32s3/3432
**Category:** hardware support
**Tags:** esp32
**Created:** [June 3, 2023, 1:13am UTC](https://community.simplefoc.com/t/changing-max-pwm-frequency-for-esp32s3/3432 "2023-06-03T01:13:00Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Charlie\_Chi](https://community.simplefoc.com/user_avatar/community.simplefoc.com/charlie_chi/32/4317_2.png) [@Charlie\_Chi](https://community.simplefoc.com/u/Charlie_Chi)
#### Post date: [June 3, 2023, 1:13am UTC](https://community.simplefoc.com/t/changing-max-pwm-frequency-for-esp32s3/3432/1 "2023-06-03T01:13:00Z")

</div>

Hello,

I am using an ESP32S3 micro-controller board driving a faulharber motor using LM6324 driver. Per the Faulharber, the ideal PWM frequency is 80k-100kHz. I believe ESP32S3 can generate PWM frequencies up to 40Mhz. How do I change the max frequency from 50kHz to 100kHz in SimpleFOC? I saw under the folder hardware\_specifics there is a file called esp32\_driver\_mcpwm.h where the max frequency of 50000 is defined. Changing this value doesn’t change the max frequency. Where should I modify the files to allow higher frequencies \> 50kHz? Thanks, Charles

---

<div class="post-metadata">

### Author: ![Antun\_Skuric](https://community.simplefoc.com/user_avatar/community.simplefoc.com/antun_skuric/32/21_2.png) [@Antun\_Skuric](https://community.simplefoc.com/u/Antun_Skuric)
#### Post date: [June 3, 2023, 1:47pm UTC](https://community.simplefoc.com/t/changing-max-pwm-frequency-for-esp32s3/3432/2 "2023-06-03T13:47:44Z")

</div>

Hey @Charlie_Chi,

50kHz has been set by the library and in order to go higher you need to modify the line:

> <https://github.com/simplefoc/Arduino-FOC/blob/05954cb9691534dd478407505d810a68f1673a5e/src/drivers/hardware_specific/esp32/esp32_driver_mcpwm.h#L28>

If you set it to 80000, then if in your driver you specify `driver.pwm_frequency=80000;` it should work.

Soo the file is:

```auto
/src/drivers/hardware_specific/esp32/esp32_driver_mcpwm.h

```

---

<div class="post-metadata">

### Author: ![Charlie\_Chi](https://community.simplefoc.com/user_avatar/community.simplefoc.com/charlie_chi/32/4317_2.png) [@Charlie\_Chi](https://community.simplefoc.com/u/Charlie_Chi)
#### Post date: [June 3, 2023, 7:06pm UTC](https://community.simplefoc.com/t/changing-max-pwm-frequency-for-esp32s3/3432/3 "2023-06-03T19:06:41Z")

</div>

Thanks for the quick response. I noticed the max frequency is 26.6kHz if I set the driver.pwm\_frequency above 25kHz. I tried 30k, 40k and 50k Hz and the output is always 26.6kHz. It works fine below 25kHz. I think there is a problem with how simplefoc configuring the timer frequency for ESP32. My simple code for testing the 3pwm:

// BLDC driver standalone example  
#include \<SimpleFOC.h\>

// BLDC driver instance  
BLDCDriver3PWM driver = BLDCDriver3PWM(37, 33, 26, 21);

void setup() {

// pwm frequency to be used [Hz]  
// for atmega328 fixed to 32kHz  
// esp32/stm32/teensy configurable  
driver.pwm\_frequency = 40000;  
// power supply voltage [V]  
driver.voltage\_power\_supply = 8.4;  
// Max DC voltage allowed - default voltage\_power\_supply  
driver.voltage\_limit = 8.4;

// driver init  
driver.init();

// enable driver  
driver.enable();

\_delay(1000);  
}

void loop() {  
// setting pwm  
// phase A: 3V  
// phase B: 6V  
// phase C: 5V  
driver.setPwm(8,6,5);

 ![IMG_3281](https://community.simplefoc.com/uploads/default/original/2X/c/c47a6f47099bc2f9b9e2bbadb8162d235b26ca06.jpeg)

---

<div class="post-metadata">

### Author: ![AXU](https://community.simplefoc.com/user_avatar/community.simplefoc.com/axu/32/8945_2.png) [@AXU](https://community.simplefoc.com/u/AXU)
#### Post date: [June 5, 2024, 1:03pm UTC](https://community.simplefoc.com/t/changing-max-pwm-frequency-for-esp32s3/3432/4 "2024-06-05T13:03:53Z")

</div>

Sorry to bad English  
I’m also working on this, and you need to downgrade the following values

```auto
#define _PWM_RES_MIN 300

```

according to：\Simple FOC\src\drivers\hardware\_specific\esp32\esp32\_mcu.cpp

```auto
  int prescaler = ceil((double)_MCPWM_FREQ / (double)_PWM_RES_DEF / 2.0f / (double)pwm_frequency) - 1;
  // constrain prescaler
  prescaler = _constrain(prescaler, 0, 128);
  // now calculate the real resolution timer period necessary (pwm resolution)
  // pwm_res = bus_freq / (pwm_freq * (prescaler + 1))
  int resolution_corrected = (double)_MCPWM_FREQ / 2.0f / (double)pwm_frequency / (double)(prescaler + 1);
  // if pwm resolution too low lower the prescaler
  if(resolution_corrected < _PWM_RES_MIN && prescaler > 0 )
    resolution_corrected = (double)_MCPWM_FREQ / 2.0f / (double)pwm_frequency / (double)(--prescaler + 1);
  resolution_corrected = _constrain(resolution_corrected, _PWM_RES_MIN, _PWM_RES_MAX);

```

---

<div class="post-metadata">

### Author: ![Antun\_Skuric](https://community.simplefoc.com/user_avatar/community.simplefoc.com/antun_skuric/32/21_2.png) [@Antun\_Skuric](https://community.simplefoc.com/u/Antun_Skuric)
#### Post date: [June 7, 2024, 7:50am UTC](https://community.simplefoc.com/t/changing-max-pwm-frequency-for-esp32s3/3432/5 "2024-06-07T07:50:21Z")

</div>

You’re absolutely right, I completely forgot about that!

---

<div class="post-metadata">

### Author: ![Charlie\_Chi](https://community.simplefoc.com/user_avatar/community.simplefoc.com/charlie_chi/32/4317_2.png) [@Charlie\_Chi](https://community.simplefoc.com/u/Charlie_Chi)
#### Post date: [June 18, 2024, 4:07pm UTC](https://community.simplefoc.com/t/changing-max-pwm-frequency-for-esp32s3/3432/6 "2024-06-18T16:07:23Z")

</div>

I was able to solve the issue by re-initiating the timer again. I will try your solution. Thanks!
