ESP8266 LED Blink

In this tutorial we will show how to program ESP8266 directly using Arduino IDE fro LED Blinking. That’s how we will get Arduino simplicity and power of ESP8266. In this case we do not need Arduino, just ESP8266 module (Node MCU).

This is our first program for ESP8266 Getting Started series. Assuming that you have prepared hardware as per Figure (Complete Hardware setup for ESP8266) or you can use node MCU, ESP witty.

In this program we will blink on board LED i.e. present on GPIO2 (TX) pin.

Step 1: Hardware Requirement and Setup

USB to Serial converter

USB to serial converter is required if you are using your own build board, Node MCU and ESP witty have in build USB to serial converter.

USB to Serial Converter
FT232 USB to Serial Converter

Set jumper position to 3.3V if you want to directly connect this serial converter to ESP, for 5V you can use 1N4148 (D1) diode and 4.7K (R3) resistor for level conversion.

We need to connect only three lines to ESP Rx, Tx and GND. You can use +5V from USB as input to the LM1117-3.3V regulator. There is no need to connect reset pin to serial converter.

ESP witty is good choice for beginner to learn many things. It is having on board RGB LED, LDR and some switches.

ESP8266 Complete Circuit
ESP8266 Complete Circuit

Step 2: Software

Arduino with ESP packages installed as we discussed in previous post “Getting Started with ESP8266. In this tutorial we will go in depth.

Start you Arduino IDE, select board Generic ESP8266 Module from

Tools >> Boards >> Generic ESP8266 as shown in figure. You can select ESPino (ESP-12 Module) also, I observed that on some versions of Arduino uploading issue comes if Generic is selected, if you have some problem you can try with ESPino (ESP-12), Programming steps and code remains same for all.

ESP8266 Board Selection
ESP8266 Board Selection

Assuming that you have installed USB to serial converter drivers, Select proper comport from Tools >> Port >> Com. Com port number depends on your laptop, to identify which is the correct one you need to unplug USB cable and observe that which com port number is absent and reconnect  it and see which is new com port appears.

ESP8266 Com port Selection
ESP8266 Com port Selection

Software for LED blink

ESP-12 and ESP-01 has blue color on board LED. Which is connected in reverse i.e. Anode(+ve) of the LED is connected to VCC and cathode (-ve) is connected to ESP-12 GPIO2. It means that LED becomes on when we output LOW and off when we output HIGH. This pin is also Tx, you cannot use serial when using this pin as LED.

Programs are divided in three parts.

Define your connections and global variables.

Defining connection makes program understandable as we use names. The number after LED indicates that LED is on GPIO2.

#define LED 2   //Define connection of LED

Power on setup (runs only once at power on or reset)

At power on we initialize all IOs and Serial. We used LED as Output pin so initialize its pin Mode to output.

void setup() {
  pinMode(LED, OUTPUT);     // Initialize the LED_BUILTIN pin as an output
}

Continuously running code (Main program)

In the third part we run a loop continuously. In this part we operate LED. To blink the LED we must turn it on and off for some time. Loop this LED on wait LED off wait.

void loop() {
  digitalWrite(LED, LOW);   // Turn the LED on (Note that LOW is the voltage level
                                    // but actually the LED is on; this is because 
                                    // it is active low on the ESP-12)
  delay(1000);                      // Wait for a second
  digitalWrite(LED, HIGH);  // Turn the LED off by making the voltage HIGH
  delay(1000);                      // Wait for two seconds (to demonstrate the active low LED)
}

Complete Program of LED blinking

Copy paste or write this code in Arduino IDE

/*
 ESP8266 Blink
 Blink the blue LED on the ESP-12 module
 
 The blue LED on the ESP-12 module is connected to GPIO2 
 (which is also the TXD pin; so we cannot use Serial.print() at the same time) 
*/

#define LED 2   //Define connection of LED

void setup() {
  pinMode(LED, OUTPUT);     // Initialize the LED_BUILTIN pin as an output
}

// the loop function runs over and over again forever
void loop() {
  digitalWrite(LED, LOW);   // Turn the LED on (Note that LOW is the voltage level
 // but actually the LED is on; this is because 
 // it is active low on the ESP-12)
  delay(1000);              // Wait for a second
  digitalWrite(LED, HIGH);  // Turn the LED off by making the voltage HIGH
  delay(1000); // Wait for two seconds (to demonstrate the active low LED)
}

Compile the code by pressing this button

Program upload to the board

To upload program to ESP on some boards you need to hold flash button while uploading and some boards require reset with holding of flash button.

To put device in serial programming mode

  1. Press and hold FLASH (S2) Button.
  2. Press and release RESET (S1) button while S2 is in pressed condition.
  3. Release FLASH(S2) button after device reset or on some boards hold it during program upload.
  4. These steps put ESP8266 in serial programming mode.

For ESP witty you have to hold the Flash button only while programming, No need to reset. Few boards may not require any user actions.

Press upload button  If you selected incorrect comport you may get this errors

ESP8266 Com error
ESP8266 Com error

Below error may occurs due to incorrect comport or Flash button is not pressed properly (i.e. device is not in serial programming mode) or may be no connection in usb to serial converter and ESP Serial.

ESP8266 sync error
ESP8266 sync error

If all is ok, you will see “Uploading…”, blue led will flash and finally “Done uploading”

ESP8266 program uploading
ESP8266 program uploading

Once the uploading is successful press RST button on board and observe that on board blue LED blinks.

Leave a Reply