Difficulty: Beginner
Time Required: 1 hour
Cost: $8-12
ESP Board: ESP8266 (NodeMCU)
What You’ll Need
| Component | Approximate Cost | Where to Buy |
|---|---|---|
| NodeMCU ESP8266 | $4-6 | Amazon / AliExpress |
| 5V Relay Module | $2-4 | Amazon |
| Jumper Wires | $2 | Amazon |
| Breadboard | $2 | Amazon |
Circuit Diagram
| NodeMCU Pin | Component | Notes |
|---|---|---|
| 3.3V | Relay VCC | Power |
| GND | Relay GND | Ground |
| D1 (GPIO 5) | Relay IN | Control signal |
| Vin (5V) | Device to control | AC device |
Step 1: Install Blynk Library
Open Arduino IDE and install:
- Blynk Library – Official Docs
- ESP8266 Board Package – Installation Guide
Step 2: Create Blynk App
- Download Blynk App from Play Store/App Store
- Create new project
- Select ESP8266 as hardware
- Add Button Widget
- Set button to Digital Pin D1
- Copy Auth Token (sent to email)
Step 3: The Complete Code
/*********************************************************************
* ESP8266 Home Automation Relay with Blynk App
*
* Control any AC device from your phone
* Hardware: NodeMCU ESP8266, 5V Relay
*
* Libraries Required:
* - Blynk (install from Library Manager)
* - ESP8266 WiFi (built-in)
*
* Pin: D1 (GPIO 5) for relay control
*********************************************************************/
#define BLYNK_TEMPLATE_ID "YOUR_TEMPLATE_ID"
#define BLYNK_TEMPLATE_NAME "Home Automation"
#define BLYNK_AUTH_TOKEN "YOUR_AUTH_TOKEN"
#include
#include
// ============== WIFI CONFIGURATION ==============
const char* wifi_ssid = "YOUR_WIFI_NAME";
const char* wifi_password = "YOUR_WIFI_PASSWORD";
// ============== RELAY PIN ==============
#define RELAY_PIN D1 // GPIO 5
#define LED_PIN D4 // Built-in LED (GPIO 2)
// ============== STATE VARIABLES ==============
bool relayState = false;
bool wifiConnected = false;
// ============== SETUP ==============
void setup() {
Serial.begin(115200);
Serial.println(F("\n======================================"));
Serial.println(F("ESP8266 Home Automation Starting..."));
Serial.println(F("======================================"));
// Configure pins
pinMode(RELAY_PIN, OUTPUT);
pinMode(LED_PIN, OUTPUT);
// Initial state - relay OFF
digitalWrite(RELAY_PIN, HIGH); // Relay is active LOW
digitalWrite(LED_PIN, HIGH); // LED OFF
// Connect to WiFi
connectToWiFi();
// Initialize Blynk
Blynk.begin(BLYNK_AUTH_TOKEN, wifi_ssid, wifi_password);
Serial.println(F("Setup complete!"));
Serial.println(F("Use Blynk app to control the relay."));
}
// ============== MAIN LOOP ==============
void loop() {
// Run Blynk
Blynk.run();
// Check WiFi connection
if (WiFi.status() != WL_CONNECTED) {
wifiConnected = false;
digitalWrite(LED_PIN, LOW); // LED ON when disconnected
} else {
wifiConnected = true;
}
}
// ============== BLYNK CONNECTED ==============
BLYNK_CONNECTED() {
Serial.println(F("Blynk connected!"));
Blynk.syncVirtual(V0); // Sync relay state
}
// ============== VIRTUAL PIN V0 - RELAY CONTROL ==============
BLYNK_WRITE(V0) {
int pinValue = param.asInt();
if (pinValue == 1) {
digitalWrite(RELAY_PIN, LOW); // Turn ON
relayState = true;
digitalWrite(LED_PIN, LOW); // LED ON
Serial.println(F("Relay: ON"));
} else {
digitalWrite(RELAY_PIN, HIGH); // Turn OFF
relayState = false;
digitalWrite(LED_PIN, HIGH); // LED OFF
Serial.println(F("Relay: OFF"));
}
}
// ============== VIRTUAL PIN V1 - TOGGLE BUTTON ==============
BLYNK_WRITE(V1) {
// Toggle relay
relayState = !relayState;
digitalWrite(RELAY_PIN, relayState ? LOW : HIGH);
digitalWrite(LED_PIN, relayState ? LOW : HIGH);
// Update Blynk
Blynk.virtualWrite(V0, relayState ? 1 : 0);
Serial.print(F("Toggle: "));
Serial.println(relayState ? F("ON") : F("OFF"));
}
// ============== HELPER FUNCTION ==============
void connectToWiFi() {
Serial.print(F("Connecting to WiFi"));
WiFi.begin(wifi_ssid, wifi_password);
int attempts = 0;
while (WiFi.status() != WL_CONNECTED && attempts < 20) {
delay(500);
Serial.print(F("."));
attempts++;
}
if (WiFi.status() == WL_CONNECTED) {
Serial.println(F(" Connected!"));
Serial.print(F("IP Address: "));
Serial.println(WiFi.localIP());
wifiConnected = true;
} else {
Serial.println(F(" Connection failed!"));
}
}
// ============== END OF CODE ==============
Step 4: Configure Blynk App
- Open Blynk app
- Add Button Widget → Set to Virtual Pin V0
- Add Button Widget → Set to Virtual Pin V1 → Mode: Switch
- Add LED Widget → Set to Virtual Pin V2
- Set button ON/OFF labels
Step 5: Upload and Test
- Connect NodeMCU via USB
- Select Tools → Board → NodeMCU 1.0
- Select correct port
- Click Upload
- Open Serial Monitor
- Check connection status
- Control relay from Blynk app
Troubleshooting
| Problem | Solution |
|---|---|
| Blynk not connecting | Check Auth Token; ensure WiFi is 2.4GHz only |
| Relay always ON | Relay is active LOW; use digitalWrite(HIGH) to turn OFF |
| NodeMCU not detected | Install CH340 drivers |
| WiFi connection failed | Verify SSID/password; check signal strength |
| Relay clicking but no power | Check device wiring; ensure AC device is plugged in |
Official Documentation
- Blynk Getting Started - Official guide
- Blynk GitHub - Library source
- ESP8266 Arduino Documentation - Official reference
Enhancement Ideas
- Add multiple relays for whole-home control
- Integrate with Home Assistant
- Add MQTT for voice assistants
- Add timer/schedule functionality in Blynk
Frequently Asked Questions
Q: Can I control 220V devices?
A: Yes, but use proper isolation. The relay handles up to 250V AC/10A. Always use caution with AC voltage.
Q: Does Blynk have a monthly fee?
A: The original Blynk has subscription tiers. Blynk IoT offers a generous free tier for personal projects.
Q: Can I use ESP32 instead?
A: Yes. Use the same code with BlynkSimpleEsp32.h library.
Q: How do I add multiple relays?
A: Use different pins (D2, D3, etc.) and create additional virtual pins in Blynk.
Q: Is this secure?
A: For home use, it's reasonably secure. For public networks, consider adding authentication.

