📘Real-Time Control (Detailed Explanation)
🎯 Lesson Objective
In this lesson, students will learn:
- How complete system integration works
- How ESP32 reads commands from Blynk
- How functions control robot movement
- How real-time control is achieved
By the end of this lesson, students will understand both working AND internal logic of the system.
🤖 What We Are Doing in This Lesson
We are combining:
- Motor control logic
- WiFi connectivity
- Blynk communication
👉 Into one complete working system
🔄 Complete System Flow
Button Press → Blynk Cloud → ESP32 → Function Call → Motor Movement
💻 Complete Code (Reference)
#define BLYNK_TEMPLATE_NAME “WiFi Robot”
#define BLYNK_AUTH_TOKEN “YourAuthToken”
#include <WiFi.h>
#include <BlynkSimpleEsp32.h>
char ssid[] = “YourWiFiName”;
char pass[] = “YourPassword”;
// Motor Pins
int IN1 = 26;
int IN2 = 27;
int IN3 = 14;
int IN4 = 12;
🔍 SECTION 1: Movement Functions (CORE LOGIC)
These functions control the actual movement of the robot.
🔹 1. stopRobot()
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, LOW);
}
👉 Explanation:
- All pins LOW → No current → Motors stop
- This is a safety function
🔹 2. forward()
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
}
👉 Explanation:
- Motor A → Forward
- Motor B → Forward
- Robot moves straight
🔹 3. backward()
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);
}
👉 Explanation:
- Both motors reverse direction
- Robot moves backward
🔹 4. left()
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
}
👉 Explanation:
- Left motor → backward
- Right motor → forward
- Robot turns left
🔹 5. right()
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);
}
👉 Explanation:
- Left motor → forward
- Right motor → backward
- Robot turns right
🔍 SECTION 2: BLYNK_WRITE() (MOST IMPORTANT PART)
This is where ESP32 receives data from mobile app
🧠 How Button Works Internally
In Blynk:
- Button ON → sends 1
- Button OFF → sends 0
👉 This value is received in ESP32
🔹 Forward Button (V0)
int value = param.asInt();
if (value == 1) {
forward();
} else {
stopRobot();
}
}
🔍 Explanation:
BLYNK_WRITE(V0)→ triggers when V0 button changesparam.asInt()→ reads value from app
Case 1:
- Button pressed → value = 1
- Calls →
forward()
Case 2:
- Button released → value = 0
- Calls →
stopRobot()
🔹 Backward Button (V1)
int value = param.asInt();
if (value == 1) {
backward();
} else {
stopRobot();
}
}
🔹 Left Button (V2)
int value = param.asInt();
if (value == 1) {
left();
} else {
stopRobot();
}
}
🔹 Right Button (V3)
int value = param.asInt();
if (value == 1) {
right();
} else {
stopRobot();
}
}
🧠 IMPORTANT CONCEPT: EVENT-BASED PROGRAMMING
👉 Unlike loop-based control:
- Code runs only when button is pressed
- This is called event-driven system
🔍 SECTION 3: setup() FUNCTION
Serial.begin(9600);
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
pinMode(IN3, OUTPUT);
pinMode(IN4, OUTPUT);
stopRobot();
Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
}
🔍 Explanation:
Serial.begin()→ debuggingpinMode()→ set pins as outputstopRobot()→ safety startBlynk.begin()→ connects ESP32 to:- WiFi
- Blynk cloud
🔍 SECTION 4: loop() FUNCTION
Blynk.run();
}
🔍 Explanation:
- Keeps connection alive
- Listens for incoming commands
- Without this → nothing works
🧪 REAL-TIME FLOW (VERY IMPORTANT)
- User presses button in app
- App sends value (1) to Blynk Cloud
- ESP32 receives it
- Corresponding
BLYNK_WRITE()runs - Function is called
- Motors move
⚠️ IMPORTANT BEHAVIOR
👉 If two buttons are pressed together:
- Commands may conflict
- Robot may behave unexpectedly
👉 Solution:
- Use only one button at a time
🧠 Key Learning Concepts
- Event-driven programming
- Function-based design
- Real-time communication
- Input → Processing → Output system
📝 Lesson Summary
In this lesson, students deeply understood how a WiFi-controlled robot works internally. They learned how mobile app commands are received, processed, and converted into physical movement using structured programming.
📌 Practice Task
- Add Serial print inside each function
- Display which button is pressed
- Try pressing multiple buttons and observe behavior