you do it like in any arduino, with pinMode and digitalRead for pin 2
Due to the way the esp8266 does the internal pin mappings vs the way it is done on the AVR and many other cores, there can be some differences when using Arduino pin numbers that can create issues for certain esp8266 boards.
The esp8266 core implements the pin mappings for the digital i/o routines in a much more efficient way than what was done in the AVR code that most other cores also adopted.
While the way the mappings are done in the esp8266 core this makes the code simpler, smaller, and MUCH faster, it does have a few issues on a few of the esp8266 boards.
The Arduino pin number for pinMode(), digitalRead(), and digitalWrite(), uses the GPIO bit number directly.
i.e. if you say digitalRead(2) you are reading GPIO2
Where things get messy is that some esp8266 boards provide mapping define symbols like Dn or more specifically D0, D1, D2, .... D14, D15 etc...
This was done to provide some compatibility with Arduino pin numbers used on AVR based boards.
The issue is that Dn is the same as n
in other words digitalRead(2) is not necessarily the same as digitalRead(D2) - usually it will not be the same.
Complicating matters is that only some of the boards in the esp8266 core use/provide these Dn symbols.
The boards that use this are:
- WeMos D1 R2 & mini
- WeMos D1 mini Pro
- WeMos D1 mini Lite
- WeMos D1 R1
- NodeMCU 0.9 (ESP-12 Module)
- NodeMCU 1.0 (ESP-12E Module)
Of these boards, most label the pins on the board using the Dn number not the n number.
i.e. if you look at the board you will often see D0, D1, D2 etc... on the board.
that means that in order to use a pin that is labeled "D2" you must use D2 in the digital i/o functions not 2
i.e. digitalWrite(D2) not digitalWrite(2)
Some boards like the LinkNode D1 which is an UNO form factor board have multiple labels on the pins.
They show the Dn numbers on the top and GPIOn on the bottom of the PCB.
The one board I would recommend that you do not ever buy is the Wemos D1 R2
That board has a totally messed pin mapping. It shifted all the pins over which makes a wreck of things.
So even though that board is UNO form factor and uses Dn pin symbols, all the symbols are shifted over.
The entire point of having the Dn symbols was so you could use the Dn symbol on an UNO form factor board
and get the n pin. i.e if you used D8 you get the physical pin on the header for pin 8 on an UNO board.
With the WeMos D1 R2 that isn't the case so it can get confusing.
The main thing to keep in mind is that depending on the board you have it may use Dn pin mappings, and if so,
pin n is not the same as pin Dn so you will generally need to use the Dn pin in order to match was is labeled on the PCB.
Also you must be aware that there are certain pins that you need to stay away from or at least be very careful when using as they have special meaning and powerup/reset time.
GPIO 2 happens to be one of them.
Here is a note I created about it:
* GPIO2 is used to control the built in LED.
* It is recommended to avoid using GPIO0, GPIO2, & GPIO15 for inputs (see below)
* GPIO0, GPIO2, and GPIO15 are special inputs used at reset and power up.
* They control the boot mode:
* GPIO15 GPIO0 GPIO2 MODE Description
* L L H UART Download Code from Uart
* L H H FLASH Boot from SPI flash (normal)
* H x x SDIO Boot from SD card
* Programing flash using uart0 requires a boot mode of "boot from uart0"
* with boot_sel=b001, that is, it must make sure
* GPIO15=0, GPIO0=0, and GPIO2=1 during reset
* Because of this, GPIO15 has an external pulldown.
* This makes these GPIO pins unsuitable for simple grounding switches especially
* when switches can be pressed at powerup to set modes.
* GPIO15 and GPIO0 do appear to work correctly after boot and GPIO0 does
* not appear to have a pull down on it.
You can read more about this here: