Installing ESP8266 Board in Arduino IDE
A "Blinker" program is a bare minimum to start with ESP8266. It allows you to turn a light diode on and off. Of course, to master this tutorial, you have to lear the board's pinouts, as well as installation and programming. In other words, if you this subject is new for you, do not miss this tutorial.
We are going to blink the on-board as well as an external diode; they are connected to the same output pin, so they'll blink together.
If you’re using an ESP-12E NodeMCU Kit, uploading the sketch (a program compiled with Arduino IDE) is very simple, since both programmer and USB port are part of this device.
Plug your board to your computer. In Arduino IDE, select Tools / Board / Node MCU 1.0 (in our case, unless you have a different one).
Select the port: Arduino IDE / Tools / Port.
Click the Upload arrow icon on top-left of Arduino IDE, and wait for your code to be compiled and uploaded to the chip.
int pin = 2;
void setup()
{
// initialize Pin 2 as an output.
pinMode(pin, OUTPUT);
}
void loop()
{
// turn the LED on
digitalWrite(pin, HIGH);
delay(1000);
// turn the LED off
digitalWrite(pin, LOW);
delay(1000);
}
The code is self-explainatory. The only question is: how do we know, that Pin 2 (GPIO 2) is at D4? This link has pinouts for different types of boards: https://randomnerdtutorials.com/getting-started-with-esp8266-wifi-transceiver-review/
Sometimes, you get an error: "esptool.FatalError: Failed to connect to ESP8266: Timed out waiting for packet header". It means that your ESP8266 is not in flashing/uploading mode.
Make sure you have selected the right board name in the list provided by Arduino IDE. Then press and hold the "BOOT/FLASH" button in your ESP8266 board; press the "Upload" button in the Arduino IDE to upload your sketch.
As soon as you see the "Connecting..." message in your Arduino IDE, release the finger from the "BOOT/FLASH" button.