GPIO state on Pin.OUT initialization

All ESP32 boards running MicroPython.
Target audience: MicroPython users with an ESP32 board.
Post Reply
itarill
Posts: 4
Joined: Sat Jul 18, 2020 10:26 pm

GPIO state on Pin.OUT initialization

Post by itarill » Sat Jul 18, 2020 10:44 pm

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.

User avatar
Roberthh
Posts: 3668
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: GPIO state on Pin.OUT initialization

Post by Roberthh » Sun Jul 19, 2020 5:48 am

You can add a value=x keyword to set the initial level, like:

Code: Select all

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

itarill
Posts: 4
Joined: Sat Jul 18, 2020 10:26 pm

Re: GPIO state on Pin.OUT initialization

Post by itarill » Sun Jul 19, 2020 6:55 am

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

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

Re: GPIO state on Pin.OUT initialization

Post by jimmo » Mon Jul 20, 2020 4:20 am

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

itarill
Posts: 4
Joined: Sat Jul 18, 2020 10:26 pm

Re: GPIO state on Pin.OUT initialization

Post by itarill » Mon Jul 20, 2020 8:37 pm

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.

Post Reply