Page 1 of 1
Non-used PWM object make other PWM objects malfunctioned
Posted: Thu Mar 26, 2020 4:27 am
by meebox
I have write a program as followed
Code: Select all
from machine import Pin, PWM
import utime
# R:D5 G:D6 B:D7
r = PWM(Pin(14, Pin.OUT), freq=500, duty=0)
# this line would make PWM work in a strange way
g = PWM(Pin(12, Pin.OUT), freq=500, duty=0)
b = PWM(Pin(13, Pin.OUT), freq=500, duty=0)
r.duty(0)
g.duty(0)
b.duty(0)
while True:
for i in range(512):
b.duty(i)
r.duty(i)
utime.sleep_ms(1)
for i in reversed(range(512)):
b.duty(i)
r.duty(i)
utime.sleep_ms(1)
It should make the leds breathing. But when I run it on a D1 mini or NodeMCU board, the led controlled by r only blink in a strange frequence and the led controlled by b blink in another way.
If I comment the line for creating g object and g.duty(0), it works perfectly.
Can any one tell me why?Thanks.
Re: Non-used PWM object make other PWM objects malfunctioned
Posted: Thu Mar 26, 2020 5:51 am
by jimmo
I think I'd need to test this to give a more useful answer, but sort of idle curiosity, have you tried running at a higher PWM frequency (or longer delay between iterations of your loop)? I think the limit on ESP8266 is 1000Hz
With PWM at 500Hz and setting the duty cycle every 1ms, you're never really giving it a chance to make a full waveform. And with a third channel activated there's more work for it to do each tick which might explain what you see?
Perhaps you could try:
- Higher PWM frequency (1000Hz)
- Longer sleep (maybe 32ms (that's still 30Hz updates)) and increment/decrement the value by 32 each iteration.
Re: Non-used PWM object make other PWM objects malfunctioned
Posted: Thu Mar 26, 2020 11:36 am
by meebox
You can try the program posted. I think it's not the situation about frequency as you mentioned. If you let the non-used object g to breathe together, it will work fine.
Re: Non-used PWM object make other PWM objects malfunctioned
Posted: Fri Mar 27, 2020 2:34 pm
by meebox
By the way, I did try deferenct frequency, but no different.
Re: Non-used PWM object make other PWM objects malfunctioned
Posted: Sat Mar 28, 2020 5:52 am
by jimmo
I did briefly test it with 1000Hz and 32ms delays. It definitely worked a lot better but there were occasional glitches. The animation was still very smooth even with the 32ms delays.
There's definitely something weird going on though with the PWM driver. I read through the implementation and it's quite complicated but nothing obvious jumped out at me, other than that it's definitely not going to be very happy with having the duty cycle modified at a high rate (it restarts all channels every time any channel's duty cycle is modified).
On ESP8266 the PWM is done in software (I'm not aware that the hardware has any PWM support -- certainly Arduino's implementation is also software). --
https://github.com/arduino/esp8266/blob ... waveform.c -- This is why it has all these crazy limitations like 1000Hz and same frequency for all channels, etc.
One thing I noticed was that I saw better behavior if the unused channel wasn't zero (set to something low but non-zero, and similarly if I never set the other channels below 10 either). But still occasional glitches.
Re: Non-used PWM object make other PWM objects malfunctioned
Posted: Mon Mar 30, 2020 11:01 am
by meebox
Thanks for your reply. I'll test all the suggestions.
Re: Non-used PWM object make other PWM objects malfunctioned
Posted: Mon May 04, 2020 8:14 pm
by EddieParis
I got same kind of problem. I fixed it you can have a look here :
https://github.com/micropython/micropython/pull/5966
Either you pull my clone if you're in a hurry, either you wait for the merge to be done.
If you have a chance to test it on your side, please add a note to the pull request to give your results.
Re: Non-used PWM object make other PWM objects malfunctioned
Posted: Sun Aug 30, 2020 5:27 am
by meebox
Sorry that I just see your reply now.Thanks for your patch, I've tested it with daily build and it works correctly.