ESP8266 using GPIO0 / GPIO2 / GPIO15 for input or output

Hi all,

I'm currently working on an ESP8266 project using the LilyGO TTGO T-Base hardware (which basically is a Lolin Wemos D1 Mini clone).

For this project I need every pin I can get. Currently only three pins are still free for use: GPIO 0, 2 and 15.

I read a lot about ESP8266 GPIO behavior and it seems these three pins (0, 2 and 15) are somewhat sensitive or require special attention when trying to use them for input or output?

Apparently:

  • When it comes to use as input, GPIO 0 and 2 feature a pull-up resistor that causes particular behavior or requires special handling that one needs to be aware of? For instance they can not be pulled low or boot will fail. But after boot is complete are there any special things to be aware of when using them?
  • When it comes to use as output, GPIO 0 can be used like any other pin without limitations or special handling?
  • GPIO 2 as output is a bit "problematic" because the on-board LED is connected to it and therefore features a permanent resistor causing its GPIO voltage to be less than 3.3V?
  • GPIO 15 should apparently not be used for anything else than SPI CS/SS (chip select/slave select)? Unfortunately I didn't find any explanation as to why this is the case. Can anyone explain or debunk? Is it for instance possible to use this pin as input for a DHT22 data line? Or output for let's say a small LED?

For my project I need at least 2 more pins to serve as input/output (one for a DHT22 and one for an LED) but am still not sure what is the wise thing to do here:

  • Does it matter which pins (0, 2 or 15) I choose? And if yes: why?
  • Is the info summary above true, complete or partially false/misunderstood?
  • Are there other special things to be aware of when using pins 0, 2 and 15 for general input or output?

Any info is much appreciated as I don't want to cause damage to the board. Thanks!

Study this page.
Leo..

No that is basically it. Mind you when connecting any hardware to them, you should make sure that they are not pulled LOW through that. A LED should be connected active LOW. (eg Vcc -> LED -> Resistor -> GPIO)

Not really, the Resistor value is quite high, it will work just fine for most things. If it is really an issue, you could pull the current limiting resistor of the board to disconnect the circuit. (or the LED if you want)

Again this is not true. GPIO15 needs to be pulled LOW at boot or the ESP won't work at all, There is a 10K pulldown resistor making sure of this, but for the rest it can be used as normal. It is also an SPI pin and can also be used as part of the alternate UART pins, but will serve also without issue as long as it is is pulled LOW at boot.

@Wawa I did; read all of it. That is one of the linked pages in my original post..

Thank you @Deva_Rishi for you insight. That was very helpful!

So to summarize:

  • GPIO 0 and 2 need to be HIGH at boot, so one must make sure that any connected hardware is not pulling them LOW at boot (hardware must be connected active LOW). After boot is complete they function like any other GPIO pin.
    Question 1: are these pins "hard wired" INPUT_PULLUP (connected to VCC) because of the HIGH state they need to be in at boot? Is that what the table hints at?

  • GPIO 15 needs to be LOW at boot, so one must make sure that any connected hardware is not pulling them HIGH at boot (hardware must be connected active HIGH). After boot is complete they function like any other GPIO pin.
    Question 2: are there any special things to be aware of when using GPIO 15 as output? In the table it is marked orange ("problematic") but no explanation is given as to why this pin apparently can't be used in a regular way..

  • GPIO 1 and 3 are used for Serial (TX0 / RX0) when Serial.begin(); is called.
    Question 3: if Serial.begin(); is not called, do these pins function like any other GPIO pin? So they each do support both input and output without trouble? Here too I have no idea why these functions are marked "problematic" and "not possible" in the table.

There is a lot of confusion out there regarding these pins so I hope we can clear that up.

Actually no, They can not be LOW, but free-floating is also OK.

No. though on some boards there are pullup resistors. If there is a LED connected to GPIO 2 then usually that ED is acting as a pullup. (i have an ESP32 that uses a PNP transistor to bypass the internal LED maiing it active HIGH, but that also pulls up GPIO 2) LED On boards with a 'flash' button, there is many times also a 10K pullup on GPIO 0. On an ESP-01 though these pins are just free-floating (and the LED is on GPIO 1 (TX) which can also not be pulled LOW at boot.

No not really though most boards have a 10K pulldown on it (an ESP-01 does, though GPIO15 is not exposed)
Unlike GPIO 0 & 2 (and 1) GPIO15 can not be free-floating, it must be pulled LOW. If there isn't a pulldown resistor on it, like on a barebones ESP-12F or ESP-07, you should add one. Mind you a 10K pulldown has almost no effect when using the pin as an OUTPUT (not does a 10K Pullup on the other pins) Even the LED on GPIO 2 has 6almost no effect on it's usage.

Yes. In fact there is a table which shows which pin modes are available for each pin. You can re-route the UART to GPIO 13 & 15. you could also use just GPIO 1 as TX (or even GPIO 2) for UART 0 or use GPIO 2 for UART 1 TX (UART 1 is TX only) or use just UART 0 RX on GPIO 3. The last pinMode() that is assigned to any GPIO pin is the active version.

pinMode(gpio, OUTPUT);

or

pinMode(gpio, INPUT); // or INPUT_PULLUP

will just be translated to it's respective FUNCTION_ relevant to the gpio.

Well GPIO 1 (TX) can also not be pulled LOW at boot just like GPIO 0 & 2, but for the rest yes.

We did not make the table. In fact they have extra 'special' functions and some small limitations.

There is one more thing that does come up here from time to time. Some pins behave with a short pulse or oscillation at boot. (apparently so do a couple of pins on an ATmega328p but that is a different matter) And this can cause some issues in certain applications like controlling a relay. If the ESP goes through a power cycle, some pins may go though a temporary state change.

I have no confusion on the matter.

Thank you very much @Deva_Rishi for explaining the ESP pin behavior in detail; much appreciated!

I knew about the oscillating behavior at boot of mainly GPIO 0. For controlling relays GPIO 4 and 5 are indeed your best friend; definitely.

The ESP for my current project mainly reads from a couple of sensors, combined with some other inputs. It takes online instructions, uploads processed data to the cloud and outputs a summary via I2C and several LEDs. I have wired pins 0 and 2 active LOW and pin 15 active HIGH. All pins are now in use and everything works like a charm.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.