Page 1 of 1
PWM limited to 255
Posted: Sun Mar 28, 2021 11:23 pm
by HighHo
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.
Re: PWM limited to 255
Posted: Mon Mar 29, 2021 7:19 am
by fdufnews
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.
Re: PWM limited to 255
Posted: Mon Mar 29, 2021 8:03 pm
by danjperron
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())
Re: PWM limited to 255
Posted: Tue Mar 30, 2021 12:04 pm
by HighHo
That's great, thank you both for taking the time to answer.