5% off all items, 10% off clearance with code FESTIVE

Free Shipping for orders over ₹999

support@thinkrobotics.com | +91 8065427666

Getting Started with Blynk 2.0: Control LED over WiFi

Getting Started with Blynk 2.0: Control LED over WiFi

Controlling a physical LED from your phone over Wi-Fi sounds like a simple project. It is. But the skills it builds are exactly the ones every IoT system depends on: device registration on a cloud platform, authenticated communication between hardware and a mobile app, and real-time control of a physical output from a remote interface.

This Blynk IoT tutorial walks through the complete process of setting up Blynk 2.0 on an ESP32, wiring a single LED, and controlling it from the Blynk mobile app over your home Wi-Fi network. Every step is explained in sequence. By the end, you will have a working IoT-controlled output and a clear understanding of how the Blynk platform connects your hardware to the outside world.

If you are new to the ESP32 and want to understand the hardware before writing firmware, start with the Think Robotics guide on ESP32 vs Arduino for IoT projects, which covers what makes ESP32 the right microcontroller for connected builds like this one.

What Is Blynk 2.0 and How Is It Different from Blynk Legacy?

Blynk released a completely rebuilt version of its platform in 2021. The original Blynk, now called Blynk Legacy, used virtual pins with a simpler but less structured communication model. Blynk 2.0 introduced a new authentication system, a redesigned web dashboard alongside the mobile app, a datastream-based configuration model, and a more robust cloud infrastructure.

The practical differences for a developer are:

The authentication token system changed. Legacy used a single auth token per device. Blynk 2.0 uses a Template ID, Template Name, and Auth Token together. All three must be present in your firmware, or the device will not connect.

The virtual pin system still exists but is now mapped through Datastreams, which are defined in the Template configuration before the device is set up. You configure the data type, range, and default value of a virtual pin at the template level rather than handling them entirely in firmware.

The mobile app is separate from the web dashboard. Both can control the same device simultaneously. This guide uses the mobile app, but the same template and datastream configuration work identically for the web dashboard.

Components Required

Component

Quantity

ESP32 development board

1

LED (any colour)

1

220 ohm resistor

1

Jumper wires

2

Breadboard

1

USB cable for programming

1

You can source ESP32 development boards, LEDs, resistors, and breadboards from the Think Robotics development boards and electronics collection, which carries the components needed for this and similar ESP32 IoT builds.

Wiring the Circuit

The circuit for this project is minimal. Connect the longer leg (anode) of the LED to GPIO 26 on the ESP32 through the 220 ohm resistor. Connect the shorter leg (cathode) directly to any GND pin on the ESP32.

The 220 ohm resistor limits the current through the LED to a safe level. Connecting an LED directly to a GPIO pin without a current-limiting resistor risks drawing more current than the ESP32 GPIO can safely source, potentially damaging the pin permanently. At 3.3V with a 220 ohm resistor, the LED draws approximately 10-15 mA, which is within the safe operating range of all standard indicator LEDs.

Setting Up Your Blynk 2.0 Account

Go to blynk. cloud and create a free account. After confirming your email and logging in, you land on the Blynk Console, the web dashboard where all device and template configurations take place.

Create a Template

Navigate to Developer Zone and click New Template. Give the template a descriptive name such as "ESP32 LED Control." Set the Hardware to ESP32 and the Connection Type to Wi-Fi. Save the template.

After saving, Blynk generates a Template ID and Template Name at the top of the template page. Copy both values. They go into your firmware exactly as shown, including the BLYNK_TEMPLATE_ID and BLYNK_TEMPLATE_NAME format.

Create a Datastream

Inside the template, navigate to the Datastreams tab and click New Datastream. Select Virtual Pin as the type. Name it "LED Control." Set the Virtual Pin to V0. Set the Data Type to Integer. Set the minimum value to 0 and the maximum to 1. Save the datastream.

This datastream is the communication channel between the app button and your firmware. When the app button sends 1, the data stream carries that value to the firmware. When it sends 0, the firmware receives 0. Your code reads the value and sets GPIO 26 accordingly.

Get Your Auth Token

Navigate to the Devices section and click New Device. Select the template you just created. Give the device a name such as "Living Room LED." Blynk creates the device and generates an Auth Token. Copy the Auth Token. This credential authenticates your specific ESP32 device on the Blynk cloud and must be kept confidential.

Installing the Blynk Library

Open the Arduino IDE. Make sure the ESP32 board support package from Espressif is installed through the Boards Manager. Open the Library Manager and search for Blynk. Install the Blynk library by Volodymyr Shymanskyy. Select the most recent stable version.

The Blynk library includes example sketches for both Blynk Legacy and Blynk 2.0. When opening examples, look specifically for examples inside the Blynk folder that reference BLYNK_TEMPLATE_ID at the top, which confirms the sketch is written for Blynk 2.0 and not the legacy platform.

Writing the Firmware

Create a new sketch and write the following code. Replace all placeholder values with your actual credentials before uploading.

cpp

// Blynk 2.0 credentials — must appear before any includes

#define BLYNK_TEMPLATE_ID    "YOUR_TEMPLATE_ID"

#define BLYNK_TEMPLATE_NAME  "ESP32 LED Control"

#define BLYNK_AUTH_TOKEN     "YOUR_AUTH_TOKEN"


#include <WiFi.h>

#include <BlynkSimpleEsp32.h>


// Wi-Fi credentials

char ssid[] = "YourWiFiName";

char pass[] = "YourWiFiPassword";


// LED pin definition

#define LED_PIN 26


// Blynk Virtual Pin V0 callback

// Runs whenever the app button sends a value

BLYNK_WRITE(V0) {

  int value = param.asInt();

  digitalWrite(LED_PIN, value);

  Serial.print("LED set to: ");

  Serial.println(value);

}


void setup() {

  Serial.begin(115200);

  pinMode(LED_PIN, OUTPUT);

  digitalWrite(LED_PIN, LOW);


  // Connect to Blynk cloud

  Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);

}


void loop() {

  Blynk.run();

}

The three define statements at the very top of the sketch must appear before any include statements. Blynk 2.0 uses these definitions during the library initialisation process. Placing them after the includes causes a compilation error or a silent connection failure.

The BLYNK_WRITE(V0) macro defines a callback function that the Blynk library calls automatically whenever the cloud receives a new value on Virtual Pin V0 from the app. You do not call this function yourself. Blynk calls it for you. Inside the callback, param.asInt() converts the incoming value to an integer. If the app button sends 1, the LED turns on. If it sends 0, the LED turns off.

The loop function contains only Blynk.run(), which handles all cloud communication, incoming message processing, and connection maintenance. This single call keeps the device connected and responsive to app input.

Select your ESP32 board from the Tools menu, select the correct COM port, and upload the sketch. Open the Serial Monitor at 115200 baud. You should see Blynk connecting to Wi-Fi, then a confirmation message indicating the device is online and connected to the Blynk cloud.

Setting Up the Mobile App

Download the Blynk IoT app from the App Store or Google Play. Log in with the same account used to create the template and device on the web console.

Open your device from the app's home screen. Tap the pencil icon to enter edit mode. Tap the plus icon to add a widget. Select the Button from the widget list.

Tap the button widget to open its settings. Set the datastream to V0 (the LED Control datastream you created). Set the button mode to Switch, not Push. Switch mode sends 1 when toggled on and 0 when toggled off, maintaining state between taps. Push mode sends 1 only while the button is held and 0 on release, which behaves like a momentary contact.

Give the button a label such as "LED" and save the widget settings. Tap the back arrow to exit edit mode.

Your device must be connected to the internet before the button becomes active. Confirm that the Serial Monitor shows the "Blynk connected" message. Tap the button in the app. The LED on your breadboard should toggle on and off in response. The Serial Monitor will print the received value each time the button changes state.

Troubleshooting Common Issues

The device shows offline in the app despite Wi-Fi connecting. Confirm all three credentials (BLYNK_TEMPLATE_ID, BLYNK_TEMPLATE_NAME, BLYNK_AUTH_TOKEN) exactly match what the Blynk Console shows. A single extra space, a missing character, or an incorrect case causes authentication to fail silently.

Sketch compiles,, but a "BLYNK_TEMPLATE_ID is not defined" error appears. The three define statements must be the very first lines in the sketch file, before any include statements. Move them above the include lines and recompile.

The button in the app is greyed out and unresponsive. This means the device is not connected to the Blynk cloud. Check the Serial Monitor output for connection errors. Confirm that your Wi-Fi SSID and password are correct. Confirm the Blynk library version is current and compatible with Blynk 2.0.

LED turns on but does not turn off, or vice versa. Check the button mode setting in the widget. If it is set to Push instead of Switch, the LED will remain on only while the button is physically held. Change the mode to Switch in the widget settings.

For complete documentation on Blynk 2.0 firmware functions, the BLYNK_WRITE macro, and datastream configuration options, the Blynk 2.0 official documentation is the most accurate and up-to-date reference available directly from the platform developers.

Extending the Project

This single LED build is a clean, tested foundation that extends directly into more capable applications.

Adding more LEDs on additional GPIO pins and creating additional Virtual Pin datastreams (V1, V2, V3) in the template gives you independent control of multiple outputs from the same app dashboard. Each BLYNK_WRITE callback handles its own pin independently.

Replacing the LED with a relay module connected to GPIO 26 turns this same firmware into a functional smart switch that controls a mains-powered appliance—the relay fires on value 1 and releases on value 0, identical to the LED behaviour.

Adding a DHT22 sensor and using Blynk.virtualWrite() to push temperature and humidity readings to two display widgets in the app alongside the LED button creates a combined sensor dashboard and control panel on a single device.

For sourcing relay modules, DHT22 sensors, and additional ESP32 accessories to extend this build into a complete home automation system, the Think Robotics IoT and sensor components section carries the modules needed for the next stage of the project.

For a broader understanding of how IoT cloud platforms like Blynk, MQTT brokers, and ThingSpeak compare in terms of architecture and use cases, the IoT Analytics platform comparison guide on IoT For All provides a clear reference written for developers evaluating platforms for new projects.

Conclusion

This Blynk IoT tutorial demonstrates the complete loop of a working IoT system in one project. A physical component on the hardware responds to a command sent from a mobile app, authenticated via a cloud platform, over a standard Wi-Fi connection. Every IoT system, regardless of scale, follows this same pattern.

Blynk 2.0 handles the cloud infrastructure, the mobile app, and the authentication layer. The ESP32 handles Wi-Fi connectivity and GPIO control. Your firmware connects the two with the BLYNK_WRITE callback and Blynk.run().

Get this project running, and you have a complete, working IoT development environment. Everything that follows — multiple sensors, multiple outputs, dashboards, automations, and notifications — builds on exactly the structure demonstrated here.

Post a comment

Frequently Asked Questions Frequently Asked Questions

Frequently Asked Questions

Q1. Is the Blynk 2.0 free tier sufficient for personal projects?

The Blynk free tier allows up to 2 active devices, with limited data streams and widgets per template. For a single LED control project or a personal weather station with a few sensors, the free tier is entirely adequate. Paid plans add more devices, more datastreams, and automation features for larger deployments.

Q2. Can I use Blynk 2.0 with a NodeMCU ESP8266 instead of an ESP32?

Yes. Blynk 2.0 supports the ESP8266 through the BlynkSimpleEsp8266.h library instead of BlynkSimpleEsp32.h. The credentials, statements, BLYNK_WRITE callbacks, and template configuration are identical between the two platforms. The only firmware change is the include statement and the board selection in the Arduino IDE.

Q3. What happens to the LED state if the ESP32 loses Wi-Fi connection?

The LED retains its last known state physically because the GPIO output holds its value until changed by code. The Blynk library will attempt to reconnect automatically in the background. While reconnecting, the device shows as offline in the app, and button presses are not delivered until the connection is restored. For critical applications, add connection state monitoring logic that triggers a visual or audio indicator when the link is lost.

Q4. Can I control the LED from outside my home network without any port forwarding?

Yes. Blynk uses outbound connections from the ESP32 to the Blynk cloud server, not inbound connections to your router. The ESP32 initiates the connection, so no port forwarding or static IP is required on your home network. The app communicates with the Blynk cloud server, which relays commands to your device over the existing outbound connection. This works from anywhere with internet access on both the phone and the ESP32.

Q5. How is Blynk 2.0 different from using MQTT directly for IoT control?

MQTT is a protocol. Blynk 2.0 is a complete platform built on top of its own communication stack. With MQTT, you manage your own broker, topic structure, authentication, and mobile app interface. With Blynk 2.0, the broker, authentication, and mobile app are provided as a service. For beginners and projects where the speed of setup matters more than infrastructure control, Blynk removes significant complexity. For production deployments where data sovereignty, custom integrations, and broker control are priorities, MQTT with a self-hosted broker provides more control at the cost of more setup work.