กลับหน้าหลัก
views
How to Use ESP8266 with Blynk IoT for LED Brightness Control and Timer
Last updated on

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.

Circuit diagram showing ESP8266 + Relay + L298N + 12V LED with all pin connections labeled

Required Components

ComponentQuantityNotes
NodeMCU ESP8266 V2 (CP2102)1 boardMain board
Relay 5V 1 Channel1 unitFor on/off control
L298N Motor Driver Module1 unitFor brightness control (PWM Dimmer)
LED 3W 12VDC1 bulbWith alligator clips
Power Adapter 12V 4A1 unitJack 5.5x2.5mm
Power Adapter Micro USB 5V 2A1 unitPower for ESP8266
Breadboard MB-102 830 Point1 board
Jumper Wires M-M, M-F, F-F40 pcs each
DC Jack Female 2.1x5.5mm1 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
Relay wiring diagram showing VCC, GND, and D5 connections to IN1 pin
L298N wiring diagram showing D1, D2, D7, D8 connections and jumper removal points

Installing Blynk Library

  1. Download Blynk_2023.rar from MediaFire
  2. Extract files using WinRAR or WinZip
  3. Copy the extracted folder to This PC > Documents > Arduino > libraries

Opening Edgent_ESP8266 Example Code

  1. Open Arduino IDE
  2. Go to File → Examples → Blynk → Blynk_Edgent → Edgent_ESP8266
  3. This code serves as the foundation for connecting ESP8266 to Blynk Cloud

Registering and Configuring Blynk IoT

Step 1: Register Blynk Account

  1. Download Blynk IoT app from App Store or Play Store
  2. Open app and tap Sign Up
  3. Enter desired email and tap Continue
  4. Check email and tap Create Password
  5. Set password and tap Continue
  6. Set profile name as desired and tap Next

Step 2: Create Template on Blynk Cloud

  1. Open blynk.cloud and login with the same email and password
  2. Go to Developer Zone menu
  3. Click + New Template
  4. Name your Template as desired
  5. Select HARDWARE as ESP8266
  6. Select CONNECTION TYPE as WiFi
  7. Click Done

Step 3: Create Datastream on Web Dashboard

  1. Select Web Dashboard menu
  2. 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

  1. Go to Devices menu
  2. Click + New Devices
  3. Select From Template
  4. Select the template you created, then click Create
  5. A Pop-up will show Template ID and Auth TokenSave 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

  1. Select the Port connected to your board
  2. Select Board as NodeMCU 1.0 (ESP-12E Module) or Generic ESP8266 Module
  3. Click Upload
  4. Wait for Done Uploading message

Configuring Widgets in Blynk App

  1. Open Blynk IoT app - you will see the Template you created
  2. Tap on the Template → tap wrench icon
  3. Tap + to add Widgets
  4. Create 2 Buttons and 1 Slider

Relay Control Switch

  • Select DatastreamV2
  • Set MODE to Switch

Brightness Confirmation Button

  • Select DatastreamV1
  • Set MODE to Push

Brightness Slider

  • Select DatastreamV0
  • Set Min = 0, Max = 255

Testing Controls

Testing Relay

  • Tap Relay switch → LED should turn on/off

Testing Brightness Control

  1. Move Slider to desired level
  2. Tap Push button (V1) to confirm
  3. 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

  1. Close Blynk app completely and reopen (Clear App for Android)
  2. You will see a sun icon with A at the top right → tap it
  3. This is Automations

Creating Schedule

  1. Tap + Create Automation
  2. Select Schedule
  3. Set the time you want the device to turn on, e.g., 18:00
  4. 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

  1. Tap + Add action → Select Control device
  2. Select Virtual PIN V2 (Relay)
  3. Toggle the switch behind it to ON
  4. Tap checkmark

Setting Stop Time

  1. Tap + between the first Action and empty space

  2. Select Wait, then do something

  3. Select Wait until… and set the time you want it to turn off, e.g., 22:00

  4. Tap checkmark

  5. Tap + between Wait Until and next Action

  6. Select Control device

  7. Do not select the switch behind it (to turn Relay off)

  8. Tap checkmark

Naming and Saving

  1. Name the Automation as desired
  2. 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:

FunctionControl MethodVirtual Pin
Turn LED on/offTap switchV2
Adjust brightnessSlider + confirmation buttonV0, V1
Set on/off timerCreate Schedule in AutomationsV2

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

จ้างทำโปรเจคเลย

ความคิดเห็น