Page 1 of 1

GPIO state on Pin.OUT initialization

Posted: Sat Jul 18, 2020 10:44 pm
by itarill
Hello all,

First and foremost please excuse for the trivial question - I did use google but apparently I'm not finding the appropriate keywords to find my answer. I'm not native English and completely new to microcontrollers.

My issue is that whenever I initialize a pin as output after boot, like

Code: Select all

relay = Pin(15, Pin.OUT)
the pin will change state. I am not talking about boot state, which I found some borderline relevant hits for (but not for micropython either), but specifically after this initialization.

I could play around and not initialize pins until the 1st time I want them switched on (or even flip the GPIO value right after its init (which will blink the led on the relay, but will not yet cause the the relay to be toggled - not an optimal solution), but I would like to know if there is a better, tried and tested solution for this.

On my raspberry I did not face such issues, there are some GPIOs that come up high on boot, but again, in the case of ESP32 am not talking strictly about boot, I am taking about pin state when setting the pin to out mode.

Thanks - and sorry for the trivial question.

Re: GPIO state on Pin.OUT initialization

Posted: Sun Jul 19, 2020 5:48 am
by Roberthh
You can add a value=x keyword to set the initial level, like:

Code: Select all

relay = Pin(15, Pin.OUT, value=0)

Re: GPIO state on Pin.OUT initialization

Posted: Sun Jul 19, 2020 6:55 am
by itarill
Thanks, this is exactly what I was looking for. Wonder why I have not came across it in the first place. Thanks for the patience :)

Re: GPIO state on Pin.OUT initialization

Posted: Mon Jul 20, 2020 4:20 am
by jimmo
itarill wrote:
Sun Jul 19, 2020 6:55 am
Wonder why I have not came across it in the first place.
The place to look for this sort of thing is in the MicroPython documentation. It's not perfect, but at least in this case it does explain the possible arguments to the Pin constructor.

http://docs.micropython.org/en/latest/l ... achine.Pin

Re: GPIO state on Pin.OUT initialization

Posted: Mon Jul 20, 2020 8:37 pm
by itarill
Indeed you're right.
I found out sometime later that I was looking at the pyboard section, which does not have this chunk in the documentation (hope I'm not writing something stupid and I'm recalling it correctly). In the ESP32 section I did find it, and facepalmed.