Skip to content

Tutorial

NodeMCU ESP8266 Blynk 2.0 Tutorial: Control an LED from Your Phone

Get a NodeMCU ESP8266 online with Blynk IoT (2.0): template and datastream setup, a full sketch with state sync after reboot, sending data back, and fixes.

  • 5 min read
  • Updated November 2, 2026
NodeMCU ESP8266 Blynk 2.0 Tutorial: Control an LED from Your Phone

Quick answer: to use a NodeMCU ESP8266 with Blynk IoT (Blynk 2.0), create a free account at blynk.cloud, make a template with a datastream on virtual pin V0 (integer, 0–1), add a Switch widget, then create a device to get its Template ID, Template Name and Auth Token. In the Arduino IDE install the ESP8266 board package and the Blynk library, paste those three lines at the top of the sketch below, and handle the switch in BLYNK_WRITE(V0) with digitalWrite(). Upload, open the Serial Monitor at 115200 baud, and the device shows Online in the console. Flip the switch and the LED follows.

Blynk gives you a phone app and web dashboard without writing any app code: the NodeMCU keeps a connection to the Blynk cloud, and widgets talk to it through virtual pins (V0, V1 …) that you map to real GPIOs in your sketch. This guide gets the first LED working, then adds the two things most tutorials skip: syncing the state after a reboot and sending data back to the dashboard.

Blynk 2.0 vs Blynk Legacy: the old Blynk app (with "energy" points and a QR-code auth token) has been retired. Old tutorials that use only Blynk.begin(auth, ssid, pass) with no Template ID will not work with the current platform. Everything here uses the current Blynk IoT platform.

What you needNotes
NodeMCU ESP8266 (v2/v3)Wemos D1 mini works the same
LED + 220–330 Ω resistor, breadboard, jumpersOr use the onboard LED on GPIO2 (active-low)
Micro-USB data cableCharge-only cables power the board but won't upload
Arduino IDE 2.xWith the ESP8266 board package
Blynk account (free plan) and the Blynk IoT phone appCheck current plan limits at blynk.io
2.4 GHz Wi-Fi with internetThe ESP8266 has no 5 GHz radio

Wiring

NodeMCU pinGPIOConnects to
D5GPIO14330 Ω resistor → LED anode (long leg)
GND–LED cathode (short leg)

D5 is used because it plays no role at boot. Avoid D3 (GPIO0), D4 (GPIO2) and D8 (GPIO15) for external loads: they are boot-strapping pins and the wrong load can stop the board starting. At 3.3 V, 330 Ω keeps a red LED around 4 mA. Full pin details: NodeMCU ESP8266 pinout.

1. Set up Blynk (template, datastream, widget, device)

Blynk's structure is: Template (the blueprint) → Datastreams (the data channels) → Dashboard widgets → Devices created from the template. If those terms are new, read What is Blynk 2.0? first.

  1. Log in to blynk.cloud → Developer Zone → My Templates → New Template. Name it NodeMCU LED, hardware ESP8266, connection WiFi.
  2. Datastreams → New Datastream → Virtual Pin. Create:
NamePinData typeMinMaxDefault
LEDV0Integer010
UptimeV1Integer010000000
  1. Web Dashboard: drag in a Switch and link it to LED (V0); drag in a Label and link it to Uptime (V1). Click Save.
  2. Devices → New Device → From template → NodeMCU LED. Blynk shows three lines: BLYNK_TEMPLATE_ID, BLYNK_TEMPLATE_NAME and BLYNK_AUTH_TOKEN. Copy them.
  3. On your phone, open the Blynk IoT app, sign in with the same account, open the device and add a Button widget on V0 (mobile dashboards are set up separately from the web dashboard).

Treat the Auth Token like a password: don't paste it into public repos, forum posts or screenshots. Blynk also supports provisioning Wi-Fi from the app with Blynk.Edgent, but the manual token method below is the easiest way to learn.

2. Upload the sketch

  1. Arduino IDE → Library Manager → search Blynk → install the library by Volodymyr Shymanskyy (version 1.x).
  2. Select NodeMCU 1.0 (ESP-12E Module) and the right COM port. If no port appears, install the CH340 or CP2102 USB driver that matches your board.
  3. Replace the three BLYNK_ lines with the ones from your device, add your Wi-Fi name and password, and upload.

The three #define lines must stay above the #include lines, or the library won't see them.

sketch.ino
// Paste the three lines from Blynk.Console (Device Info) here. Keep them ABOVE the includes.
#define BLYNK_TEMPLATE_ID   "TMPLxxxxxxxxx"
#define BLYNK_TEMPLATE_NAME "NodeMCU LED"
#define BLYNK_AUTH_TOKEN    "REPLACE_WITH_DEVICE_AUTH_TOKEN"

#define BLYNK_PRINT Serial          // prints connection status to the Serial Monitor
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

char ssid[] = "REPLACE_WITH_WIFI_NAME";
char pass[] = "REPLACE_WITH_WIFI_PASSWORD";

const uint8_t LED_PIN = D5;         // GPIO14
BlynkTimer timer;

// Runs whenever the V0 switch changes (and after syncVirtual below)
BLYNK_WRITE(V0) {
  int value = param.asInt();        // 0 or 1
  digitalWrite(LED_PIN, value ? HIGH : LOW);
  Serial.printf("V0 = %d\n", value);
}

// Runs every time the board (re)connects to Blynk
BLYNK_CONNECTED() {
  Blynk.syncVirtual(V0);            // ask the server for the last switch value
}

// Sends uptime in seconds to V1 every 5 s
void sendUptime() {
  Blynk.virtualWrite(V1, millis() / 1000);
}

void setup() {
  Serial.begin(115200);
  pinMode(LED_PIN, OUTPUT);
  digitalWrite(LED_PIN, LOW);
  Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);   // connects Wi-Fi + Blynk (blocks until connected)
  timer.setInterval(5000L, sendUptime);
}

void loop() {
  Blynk.run();                      // keep this fast: no delay() in loop()
  timer.run();
}

How it works

PieceWhat it does
BLYNK_WRITE(V0)Called when the V0 value changes on the server; param.asInt() gives 0 or 1
BLYNK_CONNECTED() + Blynk.syncVirtual(V0)After a reboot or Wi-Fi drop, fetches the last switch value so the LED matches the app
Blynk.virtualWrite(V1, …)Sends data from the board to a widget (sensor readings, status, uptime)
BlynkTimerRuns functions on an interval without delay(), which would break the cloud connection
Blynk.begin()Simple, but waits until it connects. For a device that must keep working offline, use Blynk.config() + Blynk.connect() and handle Wi-Fi yourself

Blynk limits how often a device may send data, so push sensor values on a timer (every second or slower is a good start), never on every pass through loop().

3. Test it

  1. Open the Serial Monitor at 115200 baud. You should see the Blynk logo in ASCII art, then Connecting to <your Wi-Fi>, Connected to WiFi, and Ready (ping: …ms).
  2. In Blynk.Console the device shows a green Online badge, and the Uptime label starts counting.
  3. Flip the V0 switch on the web or phone dashboard: the LED turns on and the Serial Monitor prints V0 = 1.
  4. Turn the switch on, press the NodeMCU's RST button, and watch: after it reconnects the LED comes back on by itself. That is syncVirtual at work.

If the switch moves but nothing prints, the widget is probably linked to a different datastream; if V0 = 1 prints but the LED stays dark, it's wiring.

Troubleshooting

SymptomLikely causeFix
'BLYNK_TEMPLATE_ID' was not declared or "Please specify template"#define lines below the includes, or missingPut all three BLYNK_ defines at the very top
Serial shows Invalid auth tokenToken from a different device or mistypedCopy it again from Device Info
Stuck at Connecting to …Wrong password, 5 GHz network, or weak signalUse a 2.4 GHz network; move closer to the router
Connected to Wi-Fi, device stays OfflineOld Blynk library, or network blocks outgoing connectionsUpdate the Blynk library; try a phone hotspot to rule out the router
Switch moves, nothing in Serial MonitorWidget linked to the wrong datastream, or device is a different oneCheck the widget's datastream and the device name
V0 = 1 prints but LED stays offLED reversed, wrong pin, missing GNDLong leg to the resistor/D5 side
Onboard LED works invertedGPIO2 LED is active-lowWrite value ? LOW : HIGH
Device keeps going offline after a whiledelay() in loop(), or too many virtualWrite callsUse BlynkTimer; send data no faster than about once a second
Upload fails / no COM portCharge-only cable, missing CH340/CP2102 driver, or a load on boot pinsTry another cable, install the driver, unplug wiring on D3/D4/D8
Token was exposedIt was posted publiclyDelete the device (or generate a new token) in Blynk.Console and update the sketch

FAQ

Is Blynk free for the NodeMCU ESP8266? Yes, Blynk has a free plan suitable for learning and small personal projects, with limits on devices and features. Check blynk.io/pricing for the current limits.

Does Blynk 2.0 still support the ESP8266? Yes. Use the ESP8266 board package with BlynkSimpleEsp8266.h. New designs are often built on the ESP32, which has more memory, but the ESP8266 works well for switches and simple sensors.

What is a virtual pin? A numbered data channel (V0–V255) between the app and your sketch. It isn't a physical pin: your code decides what V0 does, which might be switching a GPIO, setting a PWM value or sending a sensor reading.

Can I control a relay instead of an LED? Yes. Same code, with a relay module on the GPIO. Many relay modules are active-low (LOW = on), so invert the logic and choose a startup state that keeps the load off. Never wire mains on a breadboard. See ESP32 Blynk relay control with state sync.

Why does my LED turn off after the board restarts? Without Blynk.syncVirtual(V0) in BLYNK_CONNECTED(), the board starts with the LED off and doesn't ask the server for the last value.

Blynk or MQTT? Blynk gives you a ready-made phone app and cloud. MQTT keeps everything on your own network and works with Node-RED and Home Assistant. Compare them in Blynk vs Node-RED vs Home Assistant, or try the ESP8266 MQTT LED tutorial.

Next steps

Official reference: Blynk documentation.

Continue learning

Related articles

All articles

Signal In

Built from what makers search for

The lookups piling up in SolderHub's search bar every day shape what we publish next — new component pages, board guides, and firmware notes, sent out as they go live.

By subscribing you agree to our Privacy Policy. Unsubscribe anytime.