1

The MicroPython docs at https://docs.micropython.org/en/latest/library/machine.Pin.html feature the following example:

from machine import Pin

# create an output pin on pin #0
p0 = Pin(0, Pin.OUT)

And then later:

# reconfigure pin #0 in input mode with a pull down resistor
p0.init(p0.IN, p0.PULL_DOWN)

Is there a difference between using machine.Pin.init and just running the constructor again?

p0 = Pin(0, Pin.PULL_DOWN)  # instead of p0.init(p0.IN, p0.PULL_DOWN) from above
the busybee
  • 2,140
  • 8
  • 20
finefoot
  • 171
  • 7
  • 1
    Wasn't 100% sure if this question is on-topic, but https://electronics.stackexchange.com/help/on-topic says it's okay to ask questions about "the writing of firmware for bare-metal or RTOS applications". – finefoot Oct 12 '21 at 01:33

2 Answers2

1

Well, I have been using Win10 Thonny MicroPython IDE on Rpi Pico for a couple of months. I just do the following:

(1) from machine import Pin,

then I say something like:

(2) p0 = Pin(0, Pin.OUT)

(3) I can, in the middle of a running Pico MicroPython program, without first resetting anything, just change GP0 to Pin.IN (for input or interrupt detection) or PWM (PWM(Pin(0))) (but need to first import the PWM module)

I never used the p0.init() thing. I always think that the imported machine module takes care of everything.


References

(1) class Pin – control I/O pins (Docs 1.17 » MicroPython libraries » machine — functions related to the hardware »


tlfong01
  • 2,766
  • 1
  • 9
  • 17
1

You are not running the contructor of the old object again, you are constructing a new object and replacing the reference of the old object by the reference of the new object. This will take more time, most probably, and need more memory until the garbage collector frees the old object.

the busybee
  • 2,140
  • 8
  • 20