Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
26 views4 pages

ESP32 Blynk Motor Control Code

The document describes how to control a motor using Blynk and an ESP32. It defines pins for PWM control of the motor as well as enabling/disabling outputs. It connects to WiFi and Blynk and writes virtual pins to control motor direction and speed from the Blynk app.

Uploaded by

nelson.inocente
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views4 pages

ESP32 Blynk Motor Control Code

The document describes how to control a motor using Blynk and an ESP32. It defines pins for PWM control of the motor as well as enabling/disabling outputs. It connects to WiFi and Blynk and writes virtual pins to control motor direction and speed from the Blynk app.

Uploaded by

nelson.inocente
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

#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
}

You might also like