Pin numbering for WeMos D1 Mini (ESP8266)
Here I explain the difference in pin numbering for the WeMos D1 Mini compared to an Arduino and include a sample blink sketch.
Setting up the Arduino IDE for the WeMos D1 mini
This tutorial assumes that you have set up the Arduino IDE to able to compile the Wemos D1 mini.
You can follow the steps as outlined in the post Configuring the Wemos D1 Mini Pro ESP8266 for Arduino IDE. Once it is set up as described in the tutorial the Arduino IDE needs to be set to compile for the WeMos D1 Mini
Difference between the Arduino and WeMos D1 mini pins
One of the problems you will encounter when programming the WeMos D1 is that pin 1 on the WeMos isnt pin 1 in the Arduino IDE. The pin numbers in the WeMos D1 Pro (the ESP8266) are differently numbered than the Arduino.
This means that if you want to turn on pin 1 on the WeMos D1 mini you will need to use a different pin in the IDE. For example to enable the pin labelled D1 for output for the WeMos you would need to use the following code:
void setup() {
Serial.begin(115200);
pinMode(5, OUTPUT); //Digital pin 1 is pin 5 on the ESP8266
}
The full list of output pins to identifiers are as follows, taken from the source code:
Labelled WeMos Pin Number | Arduino Pin Number constant to use | “Real” Microcontroller Pin Number |
Digital Pin 0 | D0 | 16 |
Digital Pin 1 | D1 | 5 |
Digital Pin 2 | D2 | 4 |
Digital Pin 3 | D3 | 0 |
Digital Pin 4 | D4 | 2 |
Digital Pin 5 | D5 | 14 |
Digital Pin 6 | D6 | 12 |
Digital Pin 7 | D7 | 13 |
Digital Pin 8 | D8 | 15 |
TX | TX | 1 |
RX | RX | 3 |
These can be used to program the WeMos D1 mini using the Arduino IDE.
However there is an easier way. The ESP8266 Arduino library provides a number of constants to program the WeMos electronics easier. These constants are named as same as the pins, so digital pin one labelled on the WeMos as D1 can be used with the constant D1
.
So our above example can be made easier using these constants
void setup() {
Serial.begin(115200);
pinMode(D1, OUTPUT); //Digital pin 1 is pin 4 on the ESP8266 and D1 as a constant
}
Using these constants mean we don’t need to refer to the table to find which pin number relates to Arduino pin.
Example Blink Sketch for WeMos D1 mini
To demonstrate using these constants I have rewritten the Arduino blink sketch using the WeMos D1 mini. The code is as follows:
void setup() {
Serial.begin(115200);
pinMode(D1, OUTPUT);
}
void loop() {
//We use D1 as a constant instead of 1 since the port mapping is different to the Arduino.
digitalWrite(D1, HIGH);
Serial.println("Blink On");
delay(2000);
digitalWrite(D1, LOW);
Serial.println("Blink Off");
delay(2000);
}
Here I initialize the Serial connection so we can see progress of the program. Using this I have added the additional logging to the program.
The main difference to the standard Arduino Blink sketch is that I am using the constant D1
as discussed earlier instead of 5
as the pin number.
Using these constants you can easily program the WeMos D1 mini and other WeMos microcontrollers.
If you have any questions about the WeMos or using the constants above feel free to ask below.
Thanks for posting this information; it was exactly what I was looking for. Understanding that the ports on the Arduino and the D1 were numbered differently was causing all sorts of errors.
Thanks a lot for your description 🙂
Thank you so much! When you use the port pins in a loop, you need the actual pin number, not the constants provided. They are very misleading.
Your table is very useful, but the headings seem to be the wrong way around. Column 1 is the Arduino IDE Constant and column 2 is the actual D1 Mini GPIO pin number that the constant maps to. The comment in the code snippet is correct.
It was a bit confusing so I have updated it. Thanks for commenting.