Page 1 of 1

Why ImportError: can't import name PWM on pyboard 1.1?

Posted: Tue May 17, 2022 3:15 am
by bittware
Hi,
I tried to run "from machine import PWM" on my pyboard 1.1 that follows the guideline at https://docs.micropython.org/en/latest/ ... e.PWM.html
But why dose it give such error prompt of "ImportError: can't import name PWM"?

Re: Why ImportError: can't import name PWM on pyboard 1.1?

Posted: Tue May 17, 2022 3:19 am
by davef
First thing to do in repl is:

Code: Select all

help('modules')
then

Code: Select all

import machine
dir(machine)
I think
then

Code: Select all

dir(machine.PWM)
maybe quotes around machine and machine.PWM

Re: Why ImportError: can't import name PWM on pyboard 1.1?

Posted: Tue May 17, 2022 5:29 am
by bittware
davef wrote:
Tue May 17, 2022 3:19 am
First thing to do in repl is:

Code: Select all

help('modules')
then

Code: Select all

import machine
dir(machine)
I think
then

Code: Select all

dir(machine.PWM)
maybe quotes around machine and machine.PWM
Thanks for the help.
It seems to be an STM32 port specific quirk, see the answer in following post
viewtopic.php?f=15&t=11310&p=61888

Re: Why ImportError: can't import name PWM on pyboard 1.1?

Posted: Tue May 17, 2022 6:45 am
by pythoncoder
It's worth noting that the pyb module includes a number of useful STM-specific enhancements.

Re: Why ImportError: can't import name PWM on pyboard 1.1?

Posted: Tue May 17, 2022 3:15 pm
by scruss
The pyboard has different methods. See PWM, particularly pyb.Pin and pyb.Timer:

Code: Select all

from pyb import Pin, Timer

p = Pin('X1') # X1 has TIM2, CH1
tim = Timer(2, freq=1000)
ch = tim.channel(1, Timer.PWM, pin=p)
ch.pulse_width_percent(50)