PWM in 1.18 has changed

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
dahult
Posts: 5
Joined: Mon Feb 27, 2017 5:16 am

PWM in 1.18 has changed

Post by dahult » Fri Jan 21, 2022 10:34 pm

The 1.18 update is great but I am having trouble getting PWM to work on esp32's with 1.18. I looked at the source and see that it has gone through a major update/upgrade from 1.17 to 1.18 but am not a c++ programmer. I am using the following simple code to test PWM:

import machine
xx = machine.Pin(23, machine.Pin.OUT)
yy = machine.PWM(xx, freq=1, duty=512)

On two separate esp32's with 1.17 that I have this code produces a nice square wave on the pin 23. On both of the esp32's the pins flat-line with the same code when updated to 1.18.

I have looked at the doc (great job with the update) and see that options have been added/changed for 'duty' on PWM with 1.18. One piece of info that may help figuring this out is that no matter what/how I try to change the duty value it always comes up zero:

PWM(Pin(23), freq=1, duty_u16=0, resolution=19, (duty=0.00%, resolution=0.000%), mode=0, channel=0, timer=0)

I have tried yy.duty_u16(16384), yy.duty(512) and the yy.duty() always shows 0.

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: PWM in 1.18 has changed

Post by Roberthh » Sat Jan 22, 2022 7:10 am

The 1.18 Version of PWM has a bug, in that it does not work for low frequencies. See https://github.com/micropython/micropython/issues/8189. Until that is fixed, you should keep using v1.17.

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: PWM in 1.18 has changed

Post by Roberthh » Sun Jan 23, 2022 1:04 pm

I made a PR which fixes the low frequency issue: https://github.com/micropython/micropython/pull/8200
I tested it down to 1 Hz on ESP32 generic, ESP32S2 and ESP32C3. Once the PR is merged, you can use the daily builds.

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: PWM in 1.18 has changed

Post by Roberthh » Fri Feb 04, 2022 9:51 am

The PWM has been fixed now in the daily builds.

dahult
Posts: 5
Joined: Mon Feb 27, 2017 5:16 am

Re: PWM in 1.18 has changed

Post by dahult » Thu Feb 10, 2022 6:02 pm

Thanks

Post Reply