#define BLYNK_TEMPLATE_ID "TMPL6w-LXOZKv"
#define BLYNK_TEMPLATE_NAME "Control Motor"
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
// Blynk authentication token
char auth[] = "u8SxrnsuDHmCWwZJkFLdsJzn68FwcrEV";
// Wi-Fi credentials
char ssid[] = "ENGG SIMULAB 2.4G";
char pass[] = "EngineeringCEIT106!";
// Pin assignments
const int RPWM_PIN = 4; // GPIO pin for PWM control of motor (RPWM)
const int LPWM_PIN = 5; // GPIO pin for PWM control of motor (LPWM)
const int R_EN_PIN = 14; // GPIO pin for enabling/disabling right motor output (R_EN)
const int L_EN_PIN = 15; // GPIO pin for enabling/disabling left motor output (L_EN)
const int FORWARD_LIMIT_SWITCH_PIN = 16; // GPIO pin for forward limit switch
const int REVERSE_LIMIT_SWITCH_PIN = 17; // GPIO pin for reverse limit switch
int speed = 0; // Motor speed (0-255)
void setup() {
// Setup Serial Monitor
Serial.begin(115200);
// Connect to Wi-Fi
Blynk.begin(auth, ssid, pass);
// Setup GPIO pins
pinMode(RPWM_PIN, OUTPUT);
pinMode(LPWM_PIN, OUTPUT);
pinMode(R_EN_PIN, OUTPUT);
pinMode(L_EN_PIN, OUTPUT);
pinMode(FORWARD_LIMIT_SWITCH_PIN, INPUT_PULLUP);
pinMode(REVERSE_LIMIT_SWITCH_PIN, INPUT_PULLUP);
// Initialize Blynk virtual pins
Blynk.virtualWrite(V1, LOW); // Forward motor status
Blynk.virtualWrite(V2, LOW); // Reverse motor status
Blynk.virtualWrite(V5, speed); // Motor speed
}
void loop() {
Blynk.run();
// Check the status of the limit switches
if (digitalRead(FORWARD_LIMIT_SWITCH_PIN) == LOW) {
// Forward limit switch is pressed, stop motor
stopMotor();
Blynk.virtualWrite(V1, LOW); // Update Blynk app
}
if (digitalRead(REVERSE_LIMIT_SWITCH_PIN) == LOW) {
// Reverse limit switch is pressed, stop motor
stopMotor();
Blynk.virtualWrite(V2, LOW); // Update Blynk app
}
}
void stopMotor() {
analogWrite(RPWM_PIN, 0); // Stop motor
analogWrite(LPWM_PIN, 0); // Stop motor
}
// Blynk app functions
BLYNK_WRITE(V3) {
// Forward button pressed on Blynk app
int forwardButtonState = param.asInt();
digitalWrite(R_EN_PIN, HIGH); // Enable right motor output
digitalWrite(L_EN_PIN, HIGH); // Enable left motor output
analogWrite(RPWM_PIN, forwardButtonState * speed); // Set motor speed
analogWrite(LPWM_PIN, 0); // Set motor speed to 0
Blynk.virtualWrite(V1, forwardButtonState); // Update Blynk app
}
BLYNK_WRITE(V4) {
// Reverse button pressed on Blynk app
int reverseButtonState = param.asInt();
digitalWrite(R_EN_PIN, HIGH); // Enable right motor output
digitalWrite(L_EN_PIN, HIGH); // Enable left motor output
analogWrite(RPWM_PIN, 0); // Set motor speed to 0
analogWrite(LPWM_PIN, reverseButtonState * speed); // Set motor speed
Blynk.virtualWrite(V2, reverseButtonState); // Update Blynk app
}
BLYNK_WRITE(V5) {
// Slider value changed on Blynk app (for motor speed control)
speed = param.asInt();
Blynk.virtualWrite(V5, speed); // Update Blynk app
}