[Solved] Pin.mode() throwing TypeError

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
User avatar
thoralt
Posts: 11
Joined: Wed Jul 02, 2014 11:22 am
Location: Germany

[Solved] Pin.mode() throwing TypeError

Post by thoralt » Fri Apr 03, 2020 2:37 pm

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
Last edited by thoralt on Sat Apr 04, 2020 11:15 pm, edited 1 time in total.

DJShadow1966
Posts: 60
Joined: Sun Jun 23, 2019 4:55 am
Location: Gateshead, Tyne and Wear

Re: Pin.mode() throwing TypeError

Post by DJShadow1966 » Fri Apr 03, 2020 3:02 pm

Hello

Wouldnt it be

Code: Select all

pir = Pin('X1', Pin.IN)
regards Mike

User avatar
thoralt
Posts: 11
Joined: Wed Jul 02, 2014 11:22 am
Location: Germany

Re: Pin.mode() throwing TypeError

Post by thoralt » Fri Apr 03, 2020 4:20 pm

Wouldnt it be

Code: Select all

pir = Pin('X1', Pin.IN)
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.

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

Re: Pin.mode() throwing TypeError

Post by jimmo » Sat Apr 04, 2020 11:16 am

thoralt wrote:
Fri Apr 03, 2020 2:37 pm
the corresponding documentation (https://docs.micropython.org/en/latest/ ... e.Pin.html).
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.

User avatar
thoralt
Posts: 11
Joined: Wed Jul 02, 2014 11:22 am
Location: Germany

Re: Pin.mode() throwing TypeError

Post by thoralt » Sat Apr 04, 2020 11:14 pm

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! :)

Post Reply