Non-used PWM object make other PWM objects malfunctioned

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
meebox
Posts: 11
Joined: Sat Feb 01, 2020 4:41 am

Non-used PWM object make other PWM objects malfunctioned

Post by meebox » Thu Mar 26, 2020 4:27 am

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.

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: Non-used PWM object make other PWM objects malfunctioned

Post by jimmo » Thu Mar 26, 2020 5:51 am

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.

meebox
Posts: 11
Joined: Sat Feb 01, 2020 4:41 am

Re: Non-used PWM object make other PWM objects malfunctioned

Post by meebox » Thu Mar 26, 2020 11:36 am

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.

meebox
Posts: 11
Joined: Sat Feb 01, 2020 4:41 am

Re: Non-used PWM object make other PWM objects malfunctioned

Post by meebox » Fri Mar 27, 2020 2:34 pm

By the way, I did try deferenct frequency, but no different.

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: Non-used PWM object make other PWM objects malfunctioned

Post by jimmo » Sat Mar 28, 2020 5:52 am

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.

meebox
Posts: 11
Joined: Sat Feb 01, 2020 4:41 am

Re: Non-used PWM object make other PWM objects malfunctioned

Post by meebox » Mon Mar 30, 2020 11:01 am

Thanks for your reply. I'll test all the suggestions.

EddieParis
Posts: 3
Joined: Thu Jan 04, 2018 7:16 pm

Re: Non-used PWM object make other PWM objects malfunctioned

Post by EddieParis » Mon May 04, 2020 8:14 pm

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.

meebox
Posts: 11
Joined: Sat Feb 01, 2020 4:41 am

Re: Non-used PWM object make other PWM objects malfunctioned

Post by meebox » Sun Aug 30, 2020 5:27 am

Sorry that I just see your reply now.Thanks for your patch, I've tested it with daily build and it works correctly.

Post Reply