from machine import Pin

Questions and discussion about running MicroPython on a micro:bit board.
Target audience: MicroPython users with a micro:bit.
Post Reply
joshuachu3
Posts: 1
Joined: Fri Jul 17, 2020 6:10 am

from machine import Pin

Post by joshuachu3 » Fri Jul 17, 2020 6:18 am

Hi,
I'm trying to import Pin from machine but get the error cannot import name Pin. I am trying to write a event handler for when there is a rising edge, is there any other way to do this?

lujo
Posts: 17
Joined: Sat May 11, 2019 2:30 pm

Re: from machine import Pin

Post by lujo » Fri Jul 17, 2020 7:08 am

Hi,

MicroPython v1.9.2-34-gd64154c73 on 2017-09-01; micro:bit v1.0.1 with nRF51822
Type "help()" for more information.
>>>
>>> import microbit
>>> [p for p in dir(microbit) if p.startswith('pin')]
['pin0', 'pin1', 'pin2', 'pin3', 'pin4', 'pin5', 'pin6', 'pin7', 'pin8', 'pin9', 'pin10', 'pin11', 'pin12', 'pin13', 'pin14', 'pin15', 'pin16', 'pin19', 'pin20']
>>>

User avatar
deshipu
Posts: 1364
Joined: Thu May 28, 2015 5:54 pm

Re: from machine import Pin

Post by deshipu » Fri Jul 17, 2020 10:59 am

Hi @joshuachu3. The MicroPython on Micro:bit is different than MicroPython on all other platforms, and the examples you find for other platforms will not work on it. In particular, there are no advanced functions such as pin interrupts. I hope that helps.

User avatar
jimmo
Posts: 1768
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: from machine import Pin

Post by jimmo » Mon Jul 20, 2020 4:40 am

joshuachu3 wrote:
Fri Jul 17, 2020 6:18 am
I'm trying to import Pin from machine but get the error cannot import name Pin
On the micro:bit port, the Pin class is in the "microbit" module instead, and as lujo has pointed out, rather than constructing them, there's a predefined set of pre-initialised pin instances. As deshipu said, the micro:bit port is a bit different -- partly for simplification, and partly because it predates a lot of the work to standardise the hardware APIs across ports.
joshuachu3 wrote:
Fri Jul 17, 2020 6:18 am
I am trying to write a event handler for when there is a rising edge, is there any other way to do this?
There's no direct way to do this, but depending on your use case, there is at least a way to time pulses via https://microbit-micropython.readthedoc ... e_pulse_us (and the pin object you can pass to this is any of the pinN instances from the microbit module).

But unfortunately, no easy way to write a generic low-latency edge detection (other than polling).

Post Reply