PWM troubleshooting

Hello everyone. I’m having trouble if PWM examples…

With npulses parameter activated, the code:

pwm.write(D21.PWM,1000,500,npulses=5)

generates the following error:

Thread 1 exited with exception UnsupportedError @[0028:001D:002C:0017:00DA:001F:0000:018A]
ets Jun  8 2016 00:22:57

rst:0xc (SW_CPU_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:2
load:0x3fff0018,len:4
load:0x3fff001c,len:3648
ho 0 tail 12 room 4
load:0x40078000,len:9928
ho 0 tail 12 room 4
load:0x40080400,len:5788
entry 0x400806c0
E (32) boot: OTA: -1/-1  -1/-1  -1/-4
E (33) boot: Invalid ota, returning default vm 0
E (33) boot: Starting vm 0

(is the same code of example in PWM documentation page).

Another problem that I’m having is with resolution. I saw that the maximum frequency in ESP32 (I’m using DevKit v1) is about 2.5 kHz. But if I put:

pwm.write(D21.PWM,5000,500)

I got the following error:

E (18892) ledc: freq_hz=0 duty_resolution=15
Thread 1 exited with exception InvalidHardwareStatusError @[0028:001D:002B:0050:0000:03A7:0000:0000]
ets Jun  8 2016 00:22:57

rst:0xc (SW_CPU_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:2
load:0x3fff0018,len:4
load:0x3fff001c,len:3648
ho 0 tail 12 room 4
load:0x40078000,len:9928
ho 0 tail 12 room 4
load:0x40080400,len:5788
entry 0x400806c0
E (32) boot: OTA: -1/-1  -1/-1  -1/-4
E (33) boot: Invalid ota, returning default vm 0
E (33) boot: Starting vm 0

The code works with period=1000 and pulse=50. So… for what reason? My only info sources is the docs page and this forum, and maybe I have missed something.

Hi @Nysix
Unfortunately, npulses is not supported for ESP32 based-boards currently, but we’ll be working on this soon.

As for the second question, You have to choose the time_unit.
try this:

pwm.write(buzzerpin,5000,500,MICROS)
1 Like

Thank you @karimhamdy1. Great, I didn’t know that this feature is not supported in ESP32. Good to know!

Using time_unit = MICROS I’ll got a PWM with 5 ms of period, right? I want a PWM if 5 s of period and 500 ms of puse (duty cycle = 10%). Is possible?

Yes, I think this would have no problem, Try the code and measure the output using oscilloscope and let me know the results :slight_smile:

Hello @karimhamdy1. Using

pwm.write(buzzerpin,5000,500,MICROS)

I get a PWM with period of 5 ms and pulse = 0.5 ms. It isn’t what I looking for. I trying to get a wave with period of 5 seconds and 0.5 seconds of pulse.
I’m using the hardware PWM to bip a buzzer for 500 ms every 5 seconds. I’m trying to avoid threads, so I though that PWM is a good choice.

hi @Nysix
a period of 5 seconds would have frequency of 0.2HZ which due to some hardware settings will be seen to the pwm driver as 0 HZ.
You can dig deep in the pwm VHAL driver and try to have that 0.2Hz signal,
However, A far easier and more practical approach would be using threads.

1 Like

Hello @karimhamdy1

Great! I understood now :grin: Thank you so much for this help.