I am wondering if I am looking in the wrong spot, but I am not able to find a reference in the Micropython Docs page to the machine.PWM library and class.
https://docs.micropython.org/en/latest/ ... chine.html
Is there a reference out there for it? Specifically I am working on a Raspberry Pi Pico. There seem to be enough differences between the ESP boards and the RP2040 boards that it would warrant some documentation to explain it.
Document Reference for machine.PWM
-
- Posts: 51
- Joined: Thu Dec 27, 2018 11:38 pm
- Location: Québec, Canada
Re: Document Reference for machine.PWM
Looking at the code in micropython gihub and the help
To set a PWM pin you have to simply do
PWM(Pin) initialisation
freq(x) set frequency to x .
freq() return frequency
duty_u16(x) set duty to (x/65535)
duty_u16() return duty x from(x/65535)
duty_ns(n) set duty on for n nanosecond
duty_ns() return duty on in nanosecond
deinit() Release and turn off PWM
duty_ns is a crude approximation of the value in nano second.
if you want percentage then you need to define a function
Using nano second could be good with servo.
Daniel
Code: Select all
object <class 'PWM'> is of type type
deinit -- <function>
freq -- <function>
duty_u16 -- <function>
duty_ns -- <function>
>>>
Code: Select all
from machine import PWM,Pin
p = PWM(Pin(16))
p.freq(50)
p.duty_u16(32768)
freq(x) set frequency to x .
freq() return frequency
duty_u16(x) set duty to (x/65535)
duty_u16() return duty x from(x/65535)
duty_ns(n) set duty on for n nanosecond
duty_ns() return duty on in nanosecond
deinit() Release and turn off PWM
duty_ns is a crude approximation of the value in nano second.
if you want percentage then you need to define a function
Code: Select all
def PercentToU16(x):
return (x * 65535) // 100
Daniel