In the earlier ESP32 lessons, your phone had to be on the same Wi-Fi as the ESP32. Blynk removes that limit. The ESP32 connects to the Blynk cloud, and the Blynk app talks to it from anywhere: at school, on a bus, or on holiday. In this first lesson you set up Blynk step by step, switch an LED from your phone and send the light level from a sensor back to the app.
What you'll learn
- How Blynk templates, datastreams and devices fit together
- How virtual pins carry data between the app and the ESP32
- How to react to the app with
BLYNK_WRITE()and send data withBlynk.virtualWrite() - Why IoT sketches use timers instead of
delay()
What you need
- ESP32 board, expansion board and USB cable (ESP32 IoT Starter Kit)
- Photoresistor module (45-in-1 Sensor Kit)
- 1 LED and 1 resistor, 220 Ω (ESP32 IoT Starter Kit)
- Breadboard and jumper wires (ESP32 IoT Starter Kit)
- A free Blynk account and the Blynk IoT app on a phone
- A 2.4 GHz Wi-Fi network
How Blynk works
A template describes a kind of device: which data it has and how its dashboard looks. Each piece of data is a datastream on a virtual pin (V0, V1, …). Virtual pins aren't real pins on the board. They are numbered mailboxes in the cloud: the app writes a value into V0, and the ESP32 gets it; the ESP32 writes into V1, and the app shows it. A device is one real ESP32 made from a template, with its own secret auth token.
Wiring

| From | To |
|---|---|
| Photoresistor module S | ESP32 GPIO 34 |
| Photoresistor module + | 3V3 |
| Photoresistor module − | GND |
| ESP32 GPIO 26 | 220 Ω resistor, then the LED's long leg |
| LED short leg | GND |
Sensor modules from different makers can label and order their pins differently. Always follow the labels printed on your module.
Step 1: create your Blynk account
- On a computer, go to blynk.cloud and sign up for a free account. If you are under the age required by Blynk's terms, ask a parent or teacher to create the account.
- On your phone, install the Blynk IoT app and log in with the same account.
Step 2: create a template
- In the web console, open Developer Zone → My Templates and click + New Template.
- Name it Blynk lesson 1, choose hardware ESP32 and connection type WiFi, then click Done.
- Open the Datastreams tab and add two Virtual Pin datastreams from the table below.
- Click Save in the top right corner. Blynk ignores changes until the template is saved.
| Virtual pin | Name | Data type | Range | Phone widget |
|---|---|---|---|---|
| V0 | LED | Integer | 0 – 1 | Button (switch mode) |
| V1 | Light | Integer | 0 – 100, units % | Gauge |
Blynk updates its menus from time to time. If a button has a slightly different name, look for the closest match.
Step 3: create a device
- Open Devices, click + New Device, choose From template and pick Blynk lesson 1.
- The device page shows a Firmware configuration box with three lines that start with
#define BLYNK_. Copy them; you need them in the sketch.
Keep your auth token secret. Anyone who has it can control your device. Don't post it online or share screenshots of it.
Step 4: build the phone dashboard
- In the Blynk IoT app, open your new device and tap the wrench icon to edit the dashboard.
- Add a Button widget, link it to the LED (V0) datastream and set its mode to Switch.
- Add a Gauge widget and link it to Light (V1).
- Close the editor.

Step 5: the sketch
In the Arduino IDE, open Tools → Manage Libraries and install Blynk (by Volodymyr Shymanskyy).
Replace the three BLYNK_… lines at the top with the ones from your device's Device info page, and add your Wi-Fi name and password (2.4 GHz only). The BLYNK_… lines must stay above #include <BlynkSimpleEsp32.h>.
#define BLYNK_TEMPLATE_ID "YOUR_TEMPLATE_ID"
#define BLYNK_TEMPLATE_NAME "Blynk lesson 1"
#define BLYNK_AUTH_TOKEN "YOUR_AUTH_TOKEN"
#define BLYNK_PRINT Serial // show connection messages in the Serial Monitor
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
char ssid[] = "YOUR_WIFI_NAME";
char pass[] = "YOUR_WIFI_PASSWORD";
const int LED_PIN = 26;
const int LIGHT_PIN = 34;
BlynkTimer timer;
// Runs every time the app's switch on V0 changes
BLYNK_WRITE(V0) {
int on = param.asInt(); // 1 = on, 0 = off
digitalWrite(LED_PIN, on);
Serial.println(on ? "LED on" : "LED off");
}
// Runs every time the ESP32 connects to Blynk
BLYNK_CONNECTED() {
Blynk.syncVirtual(V0); // ask for the switch's last value
}
void sendLight() {
int raw = analogRead(LIGHT_PIN); // 0 ... 4095
int percent = map(raw, 0, 4095, 100, 0); // bright room = high %
Blynk.virtualWrite(V1, percent);
}
void setup() {
Serial.begin(115200);
pinMode(LED_PIN, OUTPUT);
Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass); // connects to Wi-Fi and Blynk
timer.setInterval(1000L, sendLight); // once per second
}
void loop() {
Blynk.run();
timer.run();
}
Upload the sketch and open the Serial Monitor at 115200 baud. After a few seconds you see the Blynk logo in text and "Ready". The device turns Online in the app. Tap the switch: the LED follows. Cover the photoresistor with your hand and watch the gauge drop.
How it works
Blynk.begin() joins your Wi-Fi and opens a connection to the Blynk cloud. When you tap the switch, the cloud sends the new value of V0 to the ESP32, and BLYNK_WRITE(V0) runs. param.asInt() gives you the value. In the other direction, Blynk.virtualWrite(V1, percent) sends a number up to the cloud, which passes it to the gauge.
Notice that loop() has no delay(). Blynk.run() has to run constantly to keep the connection alive and receive messages, so a BlynkTimer calls sendLight() once per second instead. Sending once per second is plenty; Blynk asks you not to flood it with hundreds of values per second. Blynk.syncVirtual(V0) asks the cloud for the last switch position after a restart, so the LED comes back in the state you left it.
Try this next
- Add a Slider on a new datastream V2 (0–255) and use
analogWrite(LED_PIN, …)to dim the LED. - Switch the LED on automatically when the light level drops below 20 %.
- Open the web dashboard on blynk.cloud and add the same widgets there.
Troubleshooting
-
The device shows "Offline" in the app: check the Serial Monitor at 115200 baud. "Invalid auth token" means the
BLYNK_AUTH_TOKENline is wrong; if it never connects, check the Wi-Fi name and password, and that your network is 2.4 GHz. -
The gauge goes down when you shine a light on the sensor: your module is wired the other way inside. Swap
100, 0to0, 100in themap()line. - Compile error about BlynkSimpleEsp32.h: the Blynk library isn't installed (see step 5).
- The switch does nothing: check that the widget is linked to V0 and that you saved the template after adding the datastreams.