PWM limited to 255

RP2040 based microcontroller boards running MicroPython.
Target audience: MicroPython users with an RP2040 boards.
This does not include conventional Linux-based Raspberry Pi boards.
Post Reply
HighHo
Posts: 2
Joined: Sun Mar 28, 2021 11:17 pm

PWM limited to 255

Post by HighHo » Sun Mar 28, 2021 11:23 pm

Is there a way to have PWM use 255 instead of 65025 (via duty_u16).
I feel like I'm missing something for using RGB led's, despite searching.
Sorry if this is a dumb question but I cant work it out or find the answer.

fdufnews
Posts: 76
Joined: Mon Jul 25, 2016 11:31 am

Re: PWM limited to 255

Post by fdufnews » Mon Mar 29, 2021 7:19 am

You can left shift the value in order to switch from 8 to 16 bits.
Say you have 0 ≤ value ≤ 255
if you make
value = value << 8
You will have 0 ≤ value ≤ 65280

Maybe you can elaborate on what your problem really is.

danjperron
Posts: 51
Joined: Thu Dec 27, 2018 11:38 pm
Location: Québec, Canada

Re: PWM limited to 255

Post by danjperron » Mon Mar 29, 2021 8:03 pm

You can left shift the value in order to switch from 8 to 16 bits.
Create your own class

Code: Select all

from machine import PWM

class PWM8(PWM):
    
    def duty(self,value=None):
        if value is None:
            return self.duty_u16() >> 8
        
        if value > 254:
            self.duty_u16(0xfffe)
        else:
            self.duty_u16((value << 8) | value)
        
    
    
if __name__ == "__main__":
    from machine import Pin
    from PWM8 import PWM8
    pwm = PWM8(Pin(10))
    pwm.duty(128)
    print("current duty is ",pwm.duty())

HighHo
Posts: 2
Joined: Sun Mar 28, 2021 11:17 pm

Re: PWM limited to 255

Post by HighHo » Tue Mar 30, 2021 12:04 pm

That's great, thank you both for taking the time to answer.

Post Reply