Skip to content

Getting Started with ESP8266 (NodeMCU) – Your First IoT Project

Introduction

The ESP8266 NodeMCU is one of the most popular IoT boards in the world. Why? Because it’s cheap, Wi‑Fi ready, and beginner‑friendly. With just a few lines of code, you can connect to your home Wi‑Fi and control devices from your smartphone or even send data to the cloud.

In this guide, you’ll learn:

  • How to set up NodeMCU (ESP8266) in the Arduino IDE.
  • Your first Wi‑Fi connection program.
  • The classic “LED Control from Browser” IoT project.

By the end, you’ll have your first Arduino + IoT project running on Wi‑Fi.

Step 1: What You Need

  • ESP8266 NodeMCU dev board
  • Micro‑USB cable
  • Arduino IDE installed
  • A Wi‑Fi network (with SSID + password)
  • 1x LED + resistor (optional, you can also use onboard LED at pin D0/D4 depending on board)

Step 3: Connect NodeMCU to PC

  • Plug in via USB cable.
  • In Arduino IDE, go to: Tools → Port → Select the COM port for your NodeMCU.

How to Use It

  1. Upload code.
  2. Open Serial Monitor → copy the IP address shown.
  3. Enter this IP into your phone or PC browser:

You’ll see a simple webpage with “Turn ON / Turn OFF” links.
Tap → LED turns on/off instantly.

Congrats! You just made your first IoT project with ESP8266.

Step 5: First IoT Project – Control LED from Browser

Now let’s host a mini web server on ESP8266 so you can control an LED.

Wiring (if using external LED):

  • LED anode → D1 (GPIO5)
  • LED cathode → Resistor → GND

🔌 (Or just use onboard LED at pin LED_BUILTIN).

Code: ESP8266 Web Server Example

C++

#include <ESP8266WiFi.h>

#include <ESP8266WebServer.h>

 

// WiFi config

const char* ssid = “YourWiFiName”;

const char* password = “YourWiFiPassword”;

 

ESP8266WebServer server(80);  // Create a web server at port 80

int ledPin = D1;

 

void handleRoot() {

server.send(200, “text/html”,

“<h1>ESP8266 LED Control</h1> \

<p><a href=\”/on\”>Turn ON</a></p> \

<p><a href=\”/off\”>Turn OFF</a></p>”);

}

 

void handleOn() {

digitalWrite(ledPin, HIGH);

server.send(200, “text/html”, “LED is ON <br><a href=\”/\”>Back</a>”);

}

 

void handleOff() {

digitalWrite(ledPin, LOW);

server.send(200, “text/html”, “LED is OFF <br><a href=\”/\”>Back</a>”);

}

 

void setup() {

pinMode(ledPin, OUTPUT);

digitalWrite(ledPin, LOW);

 

Serial.begin(115200);

WiFi.begin(ssid, password);

 

Serial.print(“Connecting”);

while (WiFi.status() != WL_CONNECTED) {

delay(500); Serial.print(“.”);

}

Serial.print(“\nConnected! IP: “);

Serial.println(WiFi.localIP());

 

server.on(“/”, handleRoot);

server.on(“/on”, handleOn);

server.on(“/off”, handleOff);

server.begin();

Serial.println(“Web server started.”);

}

 

void loop() {

server.handleClient();

}

Tips & Troubleshooting

  • If Serial Monitor doesn’t show Wi‑Fi IP → recheck SSID/Password.
  • If board not detected → Install CH340 driver (many NodeMCUs use it).
  • Unsure of pin numbers? Use NodeMCU pinout guide (map D0–D8 to GPIO).