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

The official pyboard running MicroPython.
This is the reference design and main target board for MicroPython.
You can buy one at the store.
Target audience: Users with a pyboard.
Post Reply
bittware
Posts: 45
Joined: Mon Aug 18, 2014 3:27 am

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

Post by bittware » Tue May 17, 2022 3:15 am

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"?

davef
Posts: 811
Joined: Thu Apr 30, 2020 1:03 am
Location: Christchurch, NZ

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

Post by davef » 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

bittware
Posts: 45
Joined: Mon Aug 18, 2014 3:27 am

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

Post by bittware » Tue May 17, 2022 5:29 am

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

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

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

Post by pythoncoder » Tue May 17, 2022 6:45 am

It's worth noting that the pyb module includes a number of useful STM-specific enhancements.
Peter Hinch
Index to my micropython libraries.

User avatar
scruss
Posts: 360
Joined: Sat Aug 12, 2017 2:27 pm
Location: Toronto, Canada
Contact:

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

Post by scruss » Tue May 17, 2022 3:15 pm

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)

Post Reply