Page 1 of 1
[Solved] Pin.mode() throwing TypeError
Posted: Fri Apr 03, 2020 2:37 pm
by thoralt
Dear forum,
I stumbled across a strange behaviour of MicroPython on my PyBoard v1.0: I want to switch the GPIO mode of a pin and had a look at the corresponding documentation (
https://docs.micropython.org/en/latest/ ... e.Pin.html). I tried my best, but the PyBoard insists on me doing it wrong:
Code: Select all
MicroPython v1.12-331-ge97bb58f0 on 2020-04-02; PYBv1.0 with STM32F405RG
Type "help()" for more information.
>>>
>>> pin = pyb.Pin('X1')
>>> pin.mode(Pin.IN)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: function takes 1 positional arguments but 2 were given
How do I set he GPIO mode of a pin correctly? I'm a bit lost here...
Thoralt
Re: Pin.mode() throwing TypeError
Posted: Fri Apr 03, 2020 3:02 pm
by DJShadow1966
Hello
Wouldnt it be
regards Mike
Re: Pin.mode() throwing TypeError
Posted: Fri Apr 03, 2020 4:20 pm
by thoralt
This does indeed work and I have used it before. I guess my question was a bit misleading: I do know how to initialise a pin's direction using Pin(...) but I wonder why I am not able to set the pin direction using the .mode() method (as per the documentation). I get a TypeError (regarding the arguments of .mode()) and am not sure why this is the case. To be able to change the direction of the pin would be nice since the pin is part of a bidirectional data bus. I wanted to avoid having to create a new Pin object every time.
Re: Pin.mode() throwing TypeError
Posted: Sat Apr 04, 2020 11:16 am
by jimmo
Unfortunately that's not the documentation for the class you're using. You're using "pyb.Pin", but that's the docs for "machine.Pin".
Historically, when there was just the pyboard, the pyb module provided access to hardware functionality. Over time, more STM32 boards were added, and also used the pyb module. Then when other ports were added (e.g. ESP8266), the hardware functionality was generalised into the machine module.
Then the machine module was added to the STM32 ports (including pyboard), but not all the APIs are exactly the same.
So the documentation you want is
https://docs.micropython.org/en/latest/ ... b.Pin.mode (mode is just a "getter" function -- you can use pin.init(...) to change the mode).
But probably your best bet is to use "import machine" and "machine.Pin" wherever possible. There isn't much left in "pyb" that doesn't have a replacement in "machine" these days (and many of the classes are actually the exact same).
Longer term it would be great to remove the pyb module and just standardise the machine module across all ports, but backwards compatibility is hard.
Re: Pin.mode() throwing TypeError
Posted: Sat Apr 04, 2020 11:14 pm
by thoralt
Unfortunately that's not the documentation for the class you're using. You're using "pyb.Pin", but that's the docs for "machine.Pin".
That explains everything. Thank you for clarifying!
