
ESP32 + Blynk: Control Devices from Your Phone Anywhere
Want to switch lights, fans, or other electronic devices using your smartphone—even when you are away from home? With an ESP32 and the Blynk IoT platform, you can build a smart home automation system that lets you monitor and control connected devices over the internet.
In this tutorial, you will learn how to connect an ESP32 to Wi-Fi, create a Blynk project, add mobile control buttons, connect a relay module, and upload Arduino code to control an electrical device remotely.
This project is ideal for students, IoT beginners, electronics enthusiasts, and anyone interested in building a DIY smart home automation system.
Introduction
Traditional switches require you to be physically present to operate electrical appliances. Internet of Things (IoT) technology makes it possible to control compatible devices remotely through a smartphone.
The ESP32 is a powerful microcontroller with built-in Wi-Fi and Bluetooth capabilities. When connected to the Blynk IoT platform, it can communicate with a smartphone app through the internet.
For example, you can create a mobile dashboard with buttons to switch a light ON or OFF, monitor sensor readings, or control multiple devices from one interface.
The best part is that you do not need to build your own mobile application. Blynk provides a platform for creating dashboards and connecting IoT hardware.
1. What Is ESP32?
The ESP32 is a powerful microcontroller development board designed for IoT and wireless electronics projects.
It includes built-in Wi-Fi and Bluetooth, making it suitable for applications that need internet connectivity without an additional Wi-Fi module.
Main features:
-
Built-in Wi-Fi connectivity.
-
Bluetooth and Bluetooth Low Energy support on common ESP32 variants.
-
Multiple digital input/output pins.
-
ADC support for reading analog sensors.
-
PWM support for controlling LEDs, motors, and other devices.
-
Compatibility with Arduino IDE and other development environments.
2. What Is Blynk IoT?
Blynk is an IoT platform that connects hardware devices to smartphone dashboards and web interfaces.
It allows you to create buttons, sliders, charts, and other dashboard widgets without developing a mobile application from scratch.
When you press a button in the Blynk app, the platform sends a value to your ESP32 through a Datastream. The ESP32 processes that value and controls the connected hardware.
Blynk recommends Virtual Pin Datastreams for exchanging information between the app and hardware.
What can you control using Blynk?
-
Lights and lamps
-
Fans and compatible appliances
-
Relay modules
-
Sensors and temperature-monitoring systems
3. Components Required
For this beginner project, you need:
| Component | Quantity | Purpose |
|---|---|---|
| ESP32 DevKit board | 1 | Main IoT controller |
| 2-channel relay module | 1 | Switching two loads |
| USB cable | 1 | Programming and power |
| Jumper wires | As needed | Connections |
| Wi-Fi router or hotspot | 1 | Internet connectivity |
| Smartphone | 1 | Blynk app control |
| Low-voltage DC lamp | 1–2 | Demonstration loads |
For your first test, use a low-voltage DC lamp instead of household AC appliances.
4. How the ESP32 + Blynk System Works
The project works through the following steps:
-
The ESP32 connects to your Wi-Fi network.
-
The ESP32 establishes a connection with Blynk Cloud.
-
Your smartphone connects to the Blynk platform through the internet.
-
You open the Blynk dashboard and press a device-control button.
-
Blynk sends the button value through a Virtual Pin Datastream.
-
The ESP32 receives the value and switches the corresponding GPIO output.
-
The relay module changes its contact state and controls the connected load.
Can you control devices from anywhere?
Yes, provided your ESP32 and smartphone both have internet access and the device remains connected to Blynk Cloud. Your phone does not need to be connected to the same Wi-Fi network as the ESP32.
5. Create Your Blynk IoT Account and Project
Before uploading the program, configure your Blynk project.
Step 1: Create an account
-
Visit Blynk official website.
-
Open the Blynk Console.
-
Create an account or sign in.
-
Open the device management dashboard.
Step 2: Create a template
-
Select Templates.
-
Create a new template.
-
Enter the name
ESP32 Smart Home. -
Select ESP32 as the hardware.
-
Select Wi-Fi as the connection type.
Step 3: Create Datastreams
Create two Virtual Pin Datastreams:
| Setting | First Device | Second Device |
|---|---|---|
| Name | Light | Fan |
| Pin | V0 | V1 |
| Data type | Integer | Integer |
| Minimum | 0 | 0 |
| Maximum | 1 | 1 |
| Default | 0 | 0 |
Step 4: Create a device
Create a device using your new template. Open its device information and copy the Blynk Auth Token. You will need it in your Arduino code.
Keep the Auth Token private. Do not publish it in blog posts, screenshots, or public code repositories.
Step 5: Configure the mobile dashboard
Open the Blynk mobile app, select your device, and add two Button widgets:
-
Light button → Datastream V0
-
Fan button → Datastream V1
Set both buttons to Switch mode so they retain their ON/OFF state.
6. ESP32 and Relay Module Pinout
For this example, we will use a 2-channel relay module.
ESP32 to relay connections
| ESP32 | 2-Channel Relay |
|---|---|
| GPIO 26 | IN1 |
| GPIO 27 | IN2 |
| GND | GND |
| Suitable relay supply | VCC |
Important: Check the relay module’s rated coil voltage and input requirements. Use a compatible supply; do not assume every relay board can be powered directly from the ESP32 3.3V pin.
Relay output connections
A relay normally provides three contact terminals:
| Terminal | Function |
|---|---|
| COM | Common |
| NO | Normally Open |
| NC | Normally Closed |
For a simple low-voltage DC lamp:
-
Connect the power supply positive wire to COM.
-
Connect NO to the lamp’s positive terminal.
-
Connect the lamp’s negative terminal to the power supply negative.
When the relay activates, COM connects to NO and the lamp turns ON.
For this demonstration, the example code assumes an active-LOW relay module. Some relay boards are active-HIGH, so check your module and adjust the code if needed.
7. Arduino IDE Setup for ESP32 and Blynk
Before uploading the program, install the required software.
Step 1: Install Arduino IDE
Download it from the official Arduino IDE website.
Step 2: Install ESP32 board support
Open Arduino IDE:
-
Go to Preferences.
-
Add the official Espressif ESP32 board package URL to Additional Boards Manager URLs.
-
Open Boards Manager.
-
Search for
esp32. -
Install the ESP32 platform by Espressif Systems.
Official instructions: ESP32 Arduino installation guide.
Step 3: Install Blynk library
-
Open Sketch → Include Library → Manage Libraries.
-
Search for
Blynk. -
Install the Blynk library.
-
Restart Arduino IDE if necessary.
You can also explore the official Blynk Arduino library.
8. Complete ESP32 + Blynk Arduino Code
The following code controls two relay channels from your smartphone. The first button controls the light, while the second controls the fan.
Before uploading, replace the template details, Auth Token, Wi-Fi name, and password with your own information.
#define BLYNK_TEMPLATE_ID "YOUR_TEMPLATE_ID"
#define BLYNK_TEMPLATE_NAME "ESP32 Smart Home"
#define BLYNK_AUTH_TOKEN "YOUR_AUTH_TOKEN"
#define BLYNK_PRINT Serial
#include <WiFi.h>
#include <BlynkSimpleEsp32.h>
// Wi-Fi credentials
char ssid[] = "YOUR_WIFI_NAME";
char pass[] = "YOUR_WIFI_PASSWORD";
// Relay pins
#define RELAY1 26
#define RELAY2 27
// Set to LOW for active-LOW relay boards.
// Change to HIGH for active-HIGH relay boards.
#define RELAY_ON LOW
#define RELAY_OFF HIGH
// Light control: Virtual Pin V0
BLYNK_WRITE(V0) {
int value = param.asInt();
digitalWrite(RELAY1, value ? RELAY_ON : RELAY_OFF);
}
// Fan control: Virtual Pin V1
BLYNK_WRITE(V1) {
int value = param.asInt();
digitalWrite(RELAY2, value ? RELAY_ON : RELAY_OFF);
}
BLYNK_CONNECTED() {
// Start with both devices OFF
Blynk.virtualWrite(V0, 0);
Blynk.virtualWrite(V1, 0);
}
void setup() {
Serial.begin(115200);
pinMode(RELAY1, OUTPUT);
pinMode(RELAY2, OUTPUT);
// Keep relays OFF during startup
digitalWrite(RELAY1, RELAY_OFF);
digitalWrite(RELAY2, RELAY_OFF);
Serial.println("Connecting to Blynk...");
Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
Serial.println("ESP32 connected!");
}
void loop() {
Blynk.run();
}
Understanding the code
-
BLYNK_TEMPLATE_IDidentifies your Blynk template. -
BLYNK_TEMPLATE_NAMEspecifies your project name. -
BLYNK_AUTH_TOKENauthenticates your ESP32 device. -
WiFi.hprovides Wi-Fi connectivity. -
BlynkSimpleEsp32.hconnects the ESP32 to Blynk. -
BLYNK_WRITE(V0)receives the light button’s value. -
BLYNK_WRITE(V1)receives the fan button’s value. -
digitalWrite()switches the relay output. -
Blynk.run()maintains communication with Blynk Cloud.
Note: This example turns both relays OFF on a fresh connection. If you want the dashboard to restore previous device states, implement state synchronization rather than always resetting the dashboard values to zero.
9. Upload and Test the Project
-
Connect the ESP32 to your computer using a USB cable.
-
Open Arduino IDE.
-
Select your ESP32 board under Tools → Board.
-
Select the correct COM port.
-
Enter your Blynk credentials and Wi-Fi details.
-
Upload the program.
-
Open Serial Monitor at 115200 baud.
-
Check that the ESP32 connects to Wi-Fi and Blynk Cloud.
-
Open the Blynk mobile dashboard.
-
Press the Light and Fan buttons to test the relay channels.
10. Common Problems and Solutions
| Problem | Possible Cause | Solution |
|---|---|---|
| ESP32 cannot connect to Wi-Fi | Incorrect SSID or password | Check Wi-Fi credentials |
| Blynk device shows offline | Invalid token or internet issue | Verify token and connection |
| Relay does not switch | Incorrect GPIO wiring | Check IN1, IN2, VCC, and GND |
| Relay works in reverse | Active-HIGH/LOW mismatch | Change RELAY_ON and RELAY_OFF |
| ESP32 keeps restarting | Insufficient power or wiring fault | Check the power supply and connections |
| App button does not control relay | Wrong Datastream | Verify V0 and V1 assignments |
| Code does not compile | Missing library or board package | Install the correct ESP32 and Blynk libraries |
11. Safety Precautions for Relay-Based Home Automation
Relay modules can switch household AC loads, but mains electricity can cause severe injury, fire, or death.
-
Start your project with low-voltage DC loads.
-
Never touch exposed relay terminals while powered.
-
Use a relay with appropriate voltage, current, and load ratings.
-
Use an enclosed, properly insulated electrical housing.
-
Provide suitable fusing and wiring protection.
-
Do not place exposed mains wiring on a breadboard.
-
Have household AC wiring installed and checked by a qualified electrician.
Also, use a suitable regulated supply for the ESP32. Its GPIO pins operate at 3.3V logic and are not 5V tolerant. Avoid GPIOs with boot-strapping restrictions unless you understand their startup requirements.
12. Advanced ESP32 + Blynk Project Ideas
After completing the basic relay-control project, you can expand it with more IoT features.
Smart lighting system: Control multiple lights and create scheduled switching routines.
Temperature monitoring: Connect a DHT22 sensor and display temperature and humidity readings on your Blynk dashboard.
Smart irrigation system: Use a soil moisture sensor to monitor soil conditions and control a water pump.
Water tank monitoring: Add an ultrasonic sensor to measure water level and send readings to your phone.
Energy monitoring: Add a suitable energy-monitoring sensor to track electrical consumption.
Motion detection: Connect a PIR sensor to detect movement and send notifications through supported Blynk features.
Explore Arduino boards and microcontrollers at Electroway and discover other electronics components for your IoT projects. You can also explore Electroway.org and the Electroway app for your project requirements.
Frequently Asked Questions (FAQs)
1. Can I control ESP32 devices from anywhere using Blynk?
Yes. If the ESP32 and smartphone have internet access and the device is connected to Blynk Cloud, you can control it remotely.
2. Does ESP32 need Wi-Fi for Blynk?
For this tutorial, yes. The ESP32 uses Wi-Fi to connect to Blynk Cloud. Other supported connectivity options depend on the hardware and Blynk configuration.
3. Is Blynk free to use?
Blynk offers a free entry-level option, but features, device limits, and usage allowances depend on its current plan. Check the official Blynk pricing page for current details.
4. Can I control multiple appliances using one ESP32?
Yes. You can use multiple GPIO pins and compatible relay channels, provided the power supply and relay hardware are suitable.
5. Can I use ESP32 without Arduino UNO?
Yes. ESP32 is itself a microcontroller development board. It can connect to Wi-Fi and control devices without needing an Arduino UNO.
6. What is a Virtual Pin in Blynk?
A Virtual Pin is a software-based communication channel used to exchange data between the Blynk dashboard and your hardware. It does not represent a physical GPIO pin.
7. Can Blynk work when my phone and ESP32 are on different Wi-Fi networks?
Yes. Blynk Cloud enables communication over the internet, so both devices can be on different networks.
8. Can I use Blynk to control a water pump?
Yes. You can control a suitably rated relay or motor-control circuit from Blynk. Make sure the switching hardware is correctly rated for the pump’s electrical load.
