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

Codecoding

This document contains code for controlling a gate motor using Blynk and ESP32. It defines pins for motor control and limit switches, includes WiFi and Blynk libraries, and has functions to open and close the gate when virtual buttons are pressed in 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)
18 views4 pages

Codecoding

This document contains code for controlling a gate motor using Blynk and ESP32. It defines pins for motor control and limit switches, includes WiFi and Blynk libraries, and has functions to open and close the gate when virtual buttons are pressed in 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

#include <WiFi.

h>
#include <BlynkSimpleEsp32.h>

// WiFi credentials
char ssid[] = "YourWiFiSSID";
char pass[] = "YourWiFiPassword";

// Blynk authentication token


char auth[] = "YourAuthToken";

// Pins for motor control


#define RPWM_PIN 2 // GPIO pin for PWM speed control (RPWM) of Motor
#define LPWM_PIN 14 // GPIO pin for PWM speed control (LPWM) of Motor
#define R_EN_PIN 12 // GPIO pin for enabling/disabling right motor output on BTS7960
#define L_EN_PIN 13 // GPIO pin for enabling/disabling left motor output on BTS7960

// Pins for limit switches


#define LIMIT_SWITCH_OPEN_PIN 18 // GPIO pin for limit switch indicating gate is fully
open
#define LIMIT_SWITCH_CLOSE_PIN 19 // GPIO pin for limit switch indicating gate is fully
closed

// Blynk Virtual Pins


#define VIRTUAL_OPEN_BTN V0 // Virtual button to open the gate
#define VIRTUAL_CLOSE_BTN V1 // Virtual button to close the gate

void setup() {
pinMode(RPWM_PIN, OUTPUT);
pinMode(LPWM_PIN, OUTPUT);
pinMode(R_EN_PIN, OUTPUT);
pinMode(L_EN_PIN, OUTPUT);

pinMode(LIMIT_SWITCH_OPEN_PIN, INPUT_PULLUP);
pinMode(LIMIT_SWITCH_CLOSE_PIN, INPUT_PULLUP);

Blynk.begin(auth, ssid, pass);

while (WiFi.status() != WL_CONNECTED) {


delay(500);
}
}

void loop() {
Blynk.run();
}

// Function to enable/disable motor outputs


void enableMotors(bool enable) {
digitalWrite(R_EN_PIN, enable ? HIGH : LOW);
digitalWrite(L_EN_PIN, enable ? HIGH : LOW);
}

BLYNK_WRITE(VIRTUAL_OPEN_BTN) {
int openButtonState = param.asInt();
if (openButtonState == HIGH) {
if (digitalRead(LIMIT_SWITCH_OPEN_PIN) == LOW) {
// Gate is already fully open
return;
}
// Move the gate in the open direction
enableMotors(true); // Enable motors
analogWrite(RPWM_PIN, 255); // Adjust PWM value for desired speed
analogWrite(LPWM_PIN, 255); // Adjust PWM value for desired speed
} else {
// Stop the motor
analogWrite(RPWM_PIN, 0);
analogWrite(LPWM_PIN, 0);
enableMotors(false); // Disable motors
}
}

BLYNK_WRITE(VIRTUAL_CLOSE_BTN) {
int closeButtonState = param.asInt();
if (closeButtonState == HIGH) {
if (digitalRead(LIMIT_SWITCH_CLOSE_PIN) == LOW) {
// Gate is already fully closed
return;
}
// Move the gate in the close direction
enableMotors(true); // Enable motors
analogWrite(RPWM_PIN, 255); // Adjust PWM value for desired speed
analogWrite(LPWM_PIN, 255); // Adjust PWM value for desired speed
} else {
// Stop the motor
analogWrite(RPWM_PIN, 0);
analogWrite(LPWM_PIN, 0);
enableMotors(false); // Disable motors
}
}

You might also like