How to Use ESP8266 with Blynk IoT for LED Brightness Control and Timer
How to Use ESP8266 with Blynk IoT for LED Brightness Control and Timer
This tutorial covers using ESP8266 with Blynk IoT to adjust LED brightness and set automatic on/off schedules through the Blynk mobile app. Content includes circuit wiring, Blynk Cloud configuration, and creating Automations for timed control.
Required Components
| Component | Quantity | Notes |
|---|---|---|
| NodeMCU ESP8266 V2 (CP2102) | 1 board | Main board |
| Relay 5V 1 Channel | 1 unit | For on/off control |
| L298N Motor Driver Module | 1 unit | For brightness control (PWM Dimmer) |
| LED 3W 12VDC | 1 bulb | With alligator clips |
| Power Adapter 12V 4A | 1 unit | Jack 5.5x2.5mm |
| Power Adapter Micro USB 5V 2A | 1 unit | Power for ESP8266 |
| Breadboard MB-102 830 Point | 1 board | |
| Jumper Wires M-M, M-F, F-F | 40 pcs each | |
| DC Jack Female 2.1x5.5mm | 1 unit |
Circuit Wiring for ESP8266 with Relay and L298N
Step 1: Connect ESP8266 to Relay 5V
ESP8266 → Relay 5V 1 Channel
━━━━━━━━━━━━━━━━━━━━━━━━━━━
Vin (5V) → VCC
GND → GND
D5 → IN1
Step 2: Connect ESP8266 to L298N
ESP8266 → L298N
━━━━━━━━━━━━━━━━━━━━━━━━━━━
GND → +5V (Must connect common ground)
D8 → ENB (Remove jumper first)
D7 → ENA (Remove jumper first)
D2 → IN2
D1 → IN1
Note: ENB and ENA jumpers must be removed to allow L298N to receive PWM signals from ESP8266
Step 3: Connect Relay to 12V Power Supply
Relay → 12V Power Supply
━━━━━━━━━━━━━━━━━━━━━━━━━━
NO → +12V
COM → Positive 12V DC
Step 4: Connect LED to L298N
12V LED → L298N
━━━━━━━━━━━━━━━━━━━━━━━━━━━
Positive → IN1
Negative → IN2
Installing Blynk Library
- Download Blynk_2023.rar from MediaFire
- Extract files using WinRAR or WinZip
- Copy the extracted folder to
This PC > Documents > Arduino > libraries
Opening Edgent_ESP8266 Example Code
- Open Arduino IDE
- Go to File → Examples → Blynk → Blynk_Edgent → Edgent_ESP8266
- This code serves as the foundation for connecting ESP8266 to Blynk Cloud
Registering and Configuring Blynk IoT
Step 1: Register Blynk Account
- Download Blynk IoT app from App Store or Play Store
- Open app and tap Sign Up
- Enter desired email and tap Continue
- Check email and tap Create Password
- Set password and tap Continue
- Set profile name as desired and tap Next
Step 2: Create Template on Blynk Cloud
- Open blynk.cloud and login with the same email and password
- Go to Developer Zone menu
- Click + New Template
- Name your Template as desired
- Select HARDWARE as ESP8266
- Select CONNECTION TYPE as WiFi
- Click Done
Step 3: Create Datastream on Web Dashboard
- Select Web Dashboard menu
- Create 1 Slider and 2 Switches
Slider (Brightness Control)
- Click gear icon → + Create Datastream
- Select Virtual Pin
- Name as desired
- Set PIN to V0
- Click Create
- Set MAX to 255
- Click Create then Save
Switch 1 (Brightness Confirmation Button)
- Create new Datastream, select Virtual Pin
- Set PIN to V1
- Click Create then Save
Switch 2 (Relay Control)
- Create new Datastream, select Virtual Pin
- Set PIN to V2
- Click Create then Save
Step 4: Create Device
- Go to Devices menu
- Click + New Devices
- Select From Template
- Select the template you created, then click Create
- A Pop-up will show Template ID and Auth Token → Save immediately (shown only once)
[image: Pop-up window showing Template ID and Auth Token that must be saved]
Editing and Uploading Code
Step 1: Enter Template ID and Auth Token
Open Edgent_ESP8266.cpp and replace the following:
// Replace with data from Pop-up
#define BLYNK_TEMPLATE_ID "TMPLxxxxxx"
#define BLYNK_TEMPLATE_NAME "YourTemplateName"
Step 2: Replace Code Section (from BLYNK_FIRMWARE_VERSION downward)
#define BLYNK_FIRMWARE_VERSION "0.1.0"
#include <BlynkEdgent.h>
// Define GPIO pins
#define RELAY_PIN D5 // Relay control pin
#define ENA_PIN D7 // PWM brightness control pin
#define ENB_PIN D8 // PWM control pin (not used in this code)
#define IN1_PIN D1 // L298N direction pin
#define IN2_PIN D2 // L298N direction pin
// Variables
int brightness = 0; // Brightness value 0-255
bool relayState = false; // Relay state
// Function to control LED brightness
void setBrightness(int value) {
brightness = constrain(value, 0, 255);
// Send PWM signal to ENA pin
analogWrite(ENA_PIN, brightness);
// Set direction for L298N to output power
digitalWrite(IN1_PIN, HIGH);
digitalWrite(IN2_PIN, LOW);
}
// Function to control Relay
void setRelay(bool state) {
relayState = state;
digitalWrite(RELAY_PIN, relayState ? HIGH : LOW);
}
// V0 - Brightness Slider
BLYNK_WRITE(V0) {
// Value is stored but not sent to LED yet
// Waiting for confirmation button from V1
brightness = param.asInt();
}
// V1 - Brightness confirmation button (Push Button)
BLYNK_WRITE(V1) {
if (param.asInt() == 1) {
// Button pressed, send brightness value to LED
setBrightness(brightness);
}
}
// V2 - Relay control switch
BLYNK_WRITE(V2) {
setRelay(param.asInt() == 1);
}
void setup() {
// Set GPIO pins
pinMode(RELAY_PIN, OUTPUT);
pinMode(ENA_PIN, OUTPUT);
pinMode(IN1_PIN, OUTPUT);
pinMode(IN2_PIN, OUTPUT);
// Turn off Relay by default
digitalWrite(RELAY_PIN, LOW);
// Turn off LED by default
digitalWrite(IN1_PIN, LOW);
digitalWrite(IN2_PIN, LOW);
analogWrite(ENA_PIN, 0);
Serial.begin(115200);
BlynkEdgent.begin();
}
void loop() {
BlynkEdgent.run();
}
Step 3: Edit WiFi Settings
// Enter your 2G or 3G WiFi
// Note: 5G WiFi is not supported
char auth[] = "YourAuthToken";
char ssid[] = "YourWiFiName";
char pass[] = "YourWiFiPassword";
Step 4: Upload Code
- Select the Port connected to your board
- Select Board as NodeMCU 1.0 (ESP-12E Module) or Generic ESP8266 Module
- Click Upload
- Wait for Done Uploading message
Configuring Widgets in Blynk App
- Open Blynk IoT app - you will see the Template you created
- Tap on the Template → tap wrench icon
- Tap + to add Widgets
- Create 2 Buttons and 1 Slider
Relay Control Switch
- Select Datastream → V2
- Set MODE to Switch
Brightness Confirmation Button
- Select Datastream → V1
- Set MODE to Push
Brightness Slider
- Select Datastream → V0
- Set Min = 0, Max = 255
Testing Controls
Testing Relay
- Tap Relay switch → LED should turn on/off
Testing Brightness Control
- Move Slider to desired level
- Tap Push button (V1) to confirm
- LED brightness will change according to the value set
Note: Moving the Slider alone will not change brightness. You must press the confirmation button.
[image: Blynk app screen showing all 3 Widgets: brightness Slider and 2 Buttons]
Setting On/Off Timer with Automations
Enabling Automations
- Close Blynk app completely and reopen (Clear App for Android)
- You will see a sun icon with A at the top right → tap it
- This is Automations
Creating Schedule
- Tap + Create Automation
- Select Schedule
- Set the time you want the device to turn on, e.g., 18:00
- Tap OK
Setting Days and Timezone
- Days of Week: Select days you want this function to run
- Timezone: Select Asia/Bangkok
- Tap checkmark
Configuring Action
- Tap + Add action → Select Control device
- Select Virtual PIN V2 (Relay)
- Toggle the switch behind it to ON
- Tap checkmark
Setting Stop Time
-
Tap + between the first Action and empty space
-
Select Wait, then do something
-
Select Wait until… and set the time you want it to turn off, e.g., 22:00
-
Tap checkmark
-
Tap + between Wait Until and next Action
-
Select Control device
-
Do not select the switch behind it (to turn Relay off)
-
Tap checkmark
Naming and Saving
- Name the Automation as desired
- Tap checkmark
[image: Automations screen showing the created Schedule with ON time 22:00 and OFF time 06:00]
Summary
When all settings are complete, the system will operate as follows:
| Function | Control Method | Virtual Pin |
|---|---|---|
| Turn LED on/off | Tap switch | V2 |
| Adjust brightness | Slider + confirmation button | V0, V1 |
| Set on/off timer | Create Schedule in Automations | V2 |
ESP8266 acts as a command receiver from Blynk Cloud via WiFi, then sends signals to control Relay and L298N as configured.
อยากทำโปรเจคแบบนี้?
รับทำโปรเจค Arduino / IoT จบงานไว ส่งงานครบ พร้อมสอน
If you need Arduino project service or urgent IoT development, see full service details on the home page
จ้างทำโปรเจคเลย