Page 1 of 1

from machine import Pin

Posted: Fri Jul 17, 2020 6:18 am
by joshuachu3
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?

Re: from machine import Pin

Posted: Fri Jul 17, 2020 7:08 am
by lujo
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']
>>>

Re: from machine import Pin

Posted: Fri Jul 17, 2020 10:59 am
by deshipu
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.

Re: from machine import Pin

Posted: Mon Jul 20, 2020 4:40 am
by jimmo
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).