Page 1 of 1

Document Reference for machine.PWM

Posted: Fri Mar 19, 2021 3:18 pm
by AArick
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.

Re: Document Reference for machine.PWM

Posted: Fri Mar 19, 2021 7:53 pm
by danjperron
Looking at the code in micropython gihub and the help

Code: Select all

object <class 'PWM'> is of type type
  deinit -- <function>
  freq -- <function>
  duty_u16 -- <function>
  duty_ns -- <function>
>>> 
To set a PWM pin you have to simply do

Code: Select all

from machine import  PWM,Pin
p = PWM(Pin(16))  
p.freq(50)               
p.duty_u16(32768) 
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

Code: Select all

def PercentToU16(x):
    return (x * 65535) // 100
Using nano second could be good with servo.

Daniel