Jump to content

IPDL - 7: LED Control Using ESP8266 and Blynk App

From cognifront

IPDL - 7: LED Control Using ESP8266 and Blynk App

Objective

To control an LED connected to an ESP8266 NodeMCU using the Blynk IoT app through a Wi-Fi network.

Required Components

  • Cognifront NodeMCU ESP8266 kit
  • Jumper wires
  • USB cable

Required Libraries

  • ESP8266Wifi
  • Blynk (installation steps below)

Circuit Diagram

Board Connections

Pin Connections

Component Connection
LED Anode (+) NodeMCU D0 (GPIO5) through 220 Ω resistor
LED Cathode (-) NodeMCU GND

Blynk Setup

Follow the steps below to create the Blynk project and connect the ESP8266 to the Blynk app.

Step 1: Create a Blynk Account

  1. Open Blynk (https://blynk.io/) on a computer.
  2. Create a Blynk account or log in using an existing account.
  3. You will be redirected to Dashboard

Blynk Dashboard: Get Started screen


Step 2: Create a Template

A Template contains the configuration that defines how the ESP8266 communicates with Blynk.

  1. In Blynk Dashboard, click the home (🏠) button.
  2. Click Create a New Template.
    Create Template screen

    Details of Template screen
  3. Enter the following details:
Setting Value
Template Name NodeMCU LED Control
Hardware ESP8266
Connectivity WiFi
Description Your choice

Click Create to create the template.

Aftr creating the Template, your screen should look like this

Step 3: Create a Virtual Pin Datastream

A Datastream is the communication channel through which data is exchanged between the ESP8266 and Blynk Cloud.

For this experiment, the Blynk button will send either 0 or 1 to the ESP8266 through Virtual Pin V0.

  1. Open the Datastreams section of the ESP8266 LED Control template.
    Datastreams screen
  2. Click Add Datastream.
    To Create Datastreams, search "LED"

    Datastreams all details
  3. Enter the following settings:
Setting Value
Datastream Name LED
Display Name LED
Virtual Pin V0
Data Type Integer
Minimum 0
Maximum 1
Default Value 0

Click Create.

Datastreams created successfully

The datastream now represents the following:

  • 0 = LED OFF
  • 1 = LED ON

Blynk's Virtual Pins are software channels and should not be confused with physical GPIO pins on the ESP8266.

Return to Home screen to create a new device

Step 4: Create the Blynk Device

The Device represents the actual NodeMCU ESP8266 board that will connect to Blynk Cloud.

  1. Open Devices in Blynk Console.
  2. Click + Add New Device.
    New Device name
  3. Give the device a name, for example:
ESP8266 LED Control

Create the device.

The device will now be associated with the template.

Important! Click Copy to Clipboard to copy the tokens
  • Important! Click Copy to Clipboard to copy the tokens

Blynk uses an individual authentication token for each device. This token allows the ESP8266 to identify and authenticate itself with Blynk Cloud.

Step 5: Get the Device Credentials (if you missed the pop-up)

If you missed to copy the tokens from the pop-up, don't worry. Follow the steps below.

  1. Open the Home of your template.
  2. On the right, you will find:
        #define BLYNK_TEMPLATE_ID "YOUR_BLYNK_TEMPLATE_ID"
        #define BLYNK_TEMPLATE_NAME "YOUR_BLYNK_TEMPLATE_NAME"
  3. Copy both. Paste them in a Notepad.
  4. Open the newly created device and find its device information.
  5. Below its name, you will find: Auth Token
  6. Click it, it will be copied. Paste it in the same Notepad.

These values are used in the Arduino program.

They will be placed in the code like this:

#define BLYNK_TEMPLATE_ID "YOUR_TEMPLATE_ID"
#define BLYNK_TEMPLATE_NAME "YOUR_BLYNK_TEMPLATE_NAME"
#define BLYNK_AUTH_TOKEN "YOUR_AUTH_TOKEN"

Keep this copied on a Notepad or any place you remember.

Do not share your Auth Token publicly.

Step 6: Dashboard configuration

  1. Open your device.
    Click on your New Device's name to open its dashboard.
  2. Open the Dashboard of your device.
    Your Device's screen. Click on the "Edit Dashboard" button.

    Your New Device's dashboard screen
  3. From the Widget Box, drag and drop a Switch into your Dashboard space on the right.
    This is the switch control widget
  4. Click on the Settings icon of the Switch.
    Settings of the switch
  5. Set the name as LED, or as per your choice.
  6. Select the Datastream as V0, what we just created.
  7. Set the rest of the settings according to your choice.
    My final settings for the switch
  8. Click Save.
    Your switch should look like this on the dashboard. Click "Save and Apply" button on the dashboard.
  9. Click Save and Apply on the Dashboard.

Step 7: Install the Required Arduino Libraries

Open Arduino IDE on the computer connected to the ESP8266.

The ESP8266 board package must be installed in Arduino IDE.

Install Blynk Library: Open:

Sketch → Include Library → Manage Libraries

Search for and install:

Blynk
Search the Library Manager for "blynk"

Click Install.

  • Important: If the screen asks you for installing dependencies, click install all.

Now Select the board:

Tools → Board → ESP8266 Boards → NodeMCU 1.0 (ESP-12E Module)

Now we are ready to write the code and upload it to the NodeMCU.

Arduino IDE Code

// © 2026, Cognifront. Member of The IoT Kits Family.

// Experiment: LED Control Using ESP8266 and Blynk
//Include the library files
#define BLYNK_PRINT Serial
#define BLYNK_TEMPLATE_ID "YOUR_BLYNK_TEMPLATE_ID"
#define BLYNK_TEMPLATE_NAME "YOUR_BLYNK_TEMPLATE_NAME"
#define BLYNK_AUTH_TOKEN "YOUR_BLYNK_AUTH_TOKEN " //Enter your blynk auth token
// make sure to KEEP the "" in template_id, name and auth_token above

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

char auth[] = BLYNK_AUTH_TOKEN;
char ssid[] = "********";//Enter your WIFI name
char pass[] = "********";//Enter your WIFI password

//Get the button value
BLYNK_WRITE(V0) {
  digitalWrite(D0, param.asInt());
}

void setup() {
  //Set the LED pin as an output pin
  pinMode(D0, OUTPUT);
  //Initialize the Blynk library
  Blynk.begin(auth, ssid, pass, "blynk.cloud", 80);
}

void loop() {
  //Run the Blynk library
  Blynk.run();
}

Step 8: Enter Wi-Fi Details

In the Arduino code, replace:

char ssid[] = "YOUR_WIFI_NAME";
char pass[] = "YOUR_WIFI_PASSWORD";

You can use your mobile hotspot here instead of WiFi.

Make sure you follow upper and lower case for wifi/mobile hotspot name, as it is case sensitive.

ESP8266 will connect to this Wi-Fi network/hotspot.

Important: The ESP8266 must have Internet access because it communicates with Blynk Cloud over the network.

Step 9: Enter Blynk Credentials

Replace the following lines with the credentials obtained from Blynk:

#define BLYNK_TEMPLATE_ID "YOUR_TEMPLATE_ID"
#define BLYNK_TEMPLATE_NAME "YOUR_TEMPLATE_NAME"
#define BLYNK_AUTH_TOKEN "YOUR_AUTH_TOKEN"

Do not change the following line:

BLYNK_WRITE(V0)

The V0 must match the Virtual Pin selected for the LED Control datastream.

Step 10: Upload the Program

  1. Connect the NodeMCU ESP8266 to the computer using a USB cable.
  2. Select the correct ESP8266 board [NodeMCU 1.0 (ESP-12E Module)] from Tools → Board.
  3. Select the correct COM port from Tools → Port.
  4. Click Upload.
  5. Wait for the upload to complete.

After successful connection, the device should appear as Online in Blynk.

Step 11: Test the LED

Open your Blynk Dashboard from template.

  1. Turn the LED Switch ON.
  2. Blynk sends 1 through Virtual Pin V0.
  3. The ESP8266 receives the value through `BLYNK_WRITE(V0)`.
  4. The ESP8266 sets D1/GPIO5 HIGH.
  5. The LED turns ON.

When the switch is turned OFF:

  1. Blynk sends 0 through Virtual Pin V0.
  2. The ESP8266 receives the value.
  3. The ESP8266 sets D1/GPIO5 LOW.
  4. The LED turns OFF.
Device Dashboard: LED is off while its Switch is off
Device Dashboard: LED will turn on when its Switch is turned on

The complete communication path is:

LED Control Switch
        ↓
 Blynk Dashboard
        ↓
  Virtual Pin V0
        ↓
   Blynk Cloud
        ↓
     ESP8266
        ↓
    D1 / GPIO5
        ↓
       LED

Understanding V0 and D1

There are two different pins used in this experiment:

Pin Meaning Purpose
V0 Blynk Virtual Pin Receives the ON/OFF command from the Blynk app
D1 / GPIO5 Physical ESP8266 GPIO Actually controls the LED

V0 does not mean GPIO0.

V0 exists inside the Blynk communication system, while D1/GPIO5 is an actual electrical pin on the ESP8266.

The Arduino code connects the two by using:

BLYNK_WRITE(V0) {
  int value = param.asInt();

  if (value == 1) {
    digitalWrite(ledPin, HIGH);
  }
  else {
    digitalWrite(ledPin, LOW);
  }
}

This means that whenever Blynk sends a new value through V0, the ESP8266 uses that value to control the physical LED.

Working Principle

The ESP8266 connects to the Internet through a Wi-Fi network and communicates with the Blynk cloud platform.

When the button in the Blynk app is pressed, the app sends a value to virtual pin V0. The ESP8266 receives this value using the BLYNK_WRITE(V0) function.

If the received value is 1, GPIO5 is set HIGH and the LED turns ON.

If the received value is 0, GPIO5 is set LOW and the LED turns OFF.

Note: The Blynk virtual pin V0 is a software communication channel. It is not the same as the physical ESP8266 GPIO pin.

Observation

  • The ESP8266 connects to the configured Wi-Fi network.
  • The Blynk app button controls the LED remotely.
  • When the button is ON, the LED turns ON.
  • When the button is OFF, the LED turns OFF.
  • The LED can be controlled from the smartphone as long as the ESP8266 and Blynk service have network connectivity.

Result

The LED was successfully controlled remotely using the ESP8266 NodeMCU and Blynk IoT app.

Conclusion

This experiment demonstrates basic cloud-based IoT control using an ESP8266. The Blynk platform provides a simple interface for sending commands from a smartphone to the ESP8266, which then controls a physical LED through GPIO5.