E8-1ESP8266Beginner

ESP8266 First Program with the Onboard LED: Slow Blink and Heartbeat on a NodeMCU

Upload your first code to an ESP8266. All you need is a NodeMCU V3 and a USB cable, no other parts. The blue LED on the board first blinks slowly three times, then beats four times like a heart with a double flash, and starts over. This LED is inverted: writing LOW to the pin turns it on, HIGH turns it off. The Serial Monitor also shows which part is running.

0.0(0)
ESP8266 First Program with the Onboard LED: Slow Blink and Heartbeat on a NodeMCU
The shortest way to meet the ESP8266: plug in the board, upload the code, watch the blue light. You learn to pick the NodeMCU board and its port in the Arduino IDE, upload code, make a pin an output and switch an LED on and off, see why the onboard LED works backwards, build a rhythm from waiting times, move repeated work into your own function and send text to the Serial Monitor. This is the first step of every ESP8266 project that follows.

Video

Circuit Diagram

ESP8266 First Program with the Onboard LED: Slow Blink and Heartbeat on a NodeMCU - Circuit Diagram

Source Code

C++ (Arduino IDE)
1// ─────────────────────────────────────────
2//   BlueGrays · bluegrays.com
3//   ESP8266 First Program with the Onboard LED: Slow Blink and Heartbeat on a NodeMCU
4// ─────────────────────────────────────────
5// The blue LED on the NodeMCU first blinks slowly three times, then beats
6// four times like a heart with a double flash, and starts over. No extra
7// parts are needed, just the USB cable. The Serial Monitor (115200) shows
8// which part is running.
9//
10// Pins:
11//   D4 (GPIO2) -> Onboard blue LED
12
13// The onboard LED is inverted: writing LOW turns it on, HIGH turns it off. The names LED_ON and
14// LED_OFF keep that from getting confusing.
15const uint8_t PIN_LED = LED_BUILTIN;      // GPIO2, labelled D4 on board
16const uint8_t LED_ON = LOW;
17const uint8_t LED_OFF = HIGH;
18
19// ============ SETTINGS ============
20const int SLOW_BLINKS = 3;                // Number of slow blinks
21const unsigned long SLOW_MS = 500;        // On time and off time (ms)
22const int HEARTBEATS = 4;                 // Number of heartbeats
23const unsigned long BEAT_MS = 100;        // Length of one beat (ms)
24const unsigned long BEAT_GAP_MS = 150;    // Gap between the two beats (ms)
25const unsigned long BEAT_REST_MS = 650;   // Rest between heartbeats (ms)
26
27// Turns the LED on for onMs, then off for offMs.
28void flash(unsigned long onMs, unsigned long offMs) {
29  digitalWrite(PIN_LED, LED_ON);
30  delay(onMs);
31  digitalWrite(PIN_LED, LED_OFF);
32  delay(offMs);
33}
34
35void setup() {
36  Serial.begin(115200);
37  pinMode(PIN_LED, OUTPUT);
38  digitalWrite(PIN_LED, LED_OFF);         // Start with the LED off
39  Serial.println();
40  Serial.println("ESP8266 is ready");
41}
42
43void loop() {
44  Serial.println("Slow blink");
45  for (int i = 0; i < SLOW_BLINKS; i++) {
46    flash(SLOW_MS, SLOW_MS);
47  }
48
49  // Heartbeat: two short beats, then a longer rest.
50  Serial.println("Heartbeat");
51  for (int i = 0; i < HEARTBEATS; i++) {
52    flash(BEAT_MS, BEAT_GAP_MS);
53    flash(BEAT_MS, BEAT_REST_MS);
54  }
55}
56

FAQ

What is the difference between a NodeMCU and a Wemos D1 mini?

Both carry the same ESP8266 chip and this code runs on both without changes; the onboard LED is on GPIO2 (D4) on each. The NodeMCU V3 is bigger, brings out more pins and has two buttons (RST, FLASH). The D1 mini is small, has only an RST button and draws less current in battery projects. For a D1 mini, pick LOLIN(WEMOS) D1 R2 & mini as the board in the Arduino IDE.

How is the ESP8266 different from the ESP32?

The ESP8266 is the older and cheaper board. It has Wi-Fi but no Bluetooth, a single core, only one analog input (A0) and fewer pins. The ESP32 is faster and adds Bluetooth and many analog inputs. For simple projects that connect to Wi-Fi the ESP8266 is more than enough. The code looks alike, but the library names differ (ESP8266WiFi.h instead of WiFi.h, for example).

Why does the LED turn on when I write LOW?

One end of the LED on the board is tied to 3.3 V, the other end goes through a resistor to GPIO2. When the pin is LOW (0 V), current flows from 3.3 V to the pin and the LED lights up. When the pin is HIGH (3.3 V), both ends sit at the same voltage, no current flows and the LED is off. This is called inverted logic or active low. Writing LED_ON = LOW in the code settles the confusion in one place.

Can I connect something else to pin D4?

You can, but take care. D4 (GPIO2) has to be HIGH while the board starts; a button or module that pulls it to GND at power-up keeps the board from starting. Also, whatever you connect to D4, the onboard LED blinks along with it. For external LEDs, buttons and relays in your first projects, D1, D2, D5, D6 and D7 are the safest pins.