#define BLYNK_PRINT Serial
#define BLYNK_TEMPLATE_ID "TMPL6ZULtN3U3"
#define BLYNK_TEMPLATE_NAME "Social Distance Sensor"
#define BLYNK_AUTH_TOKEN "QnTVWVZfqGUGbs3PRSuCq_egnZRHDuMF"
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#define echoPin 18 // attach pin ESP32 GPIO18 to pin Echo of HC-SR04
#define trigPin 5 // attach pin ESP32 GPIO5 to pin Trig of HC-SR04
#define buzzerPin 22 // attach pin ESP32 GPIO22 to the buzzer
#define LEDPin 23 // attach pin ESP32 GPIO23 to the red LED
long duration; // Variable to store time taken by the pulse to reach the receiver
int distance; // Variable to store distance calculated using the formula
char auth[] = BLYNK_AUTH_TOKEN;
char ssid[] = "Wokwi-GUEST"; // Replace with your WiFi SSID
char pass[] = ""; // Replace with your WiFi password
BlynkTimer timer;
int buzzerState = 0;
// Blynk virtual write function to control the buzzer and LED manually
BLYNK_WRITE(V0)
{
buzzerState = param.asInt(); // Get the state of the button (1 or 0)
if (buzzerState == 1) {
tone(buzzerPin, 1200); // Start the buzzer with 1200Hz frequency
digitalWrite(LEDPin, HIGH); // Turn on the LED when the buzzer is on
Blynk.virtualWrite(V1, 255); // Turn on the LED widget in the Blynk app
Serial.println("Buzzer ON, LED ON");
} else {
noTone(buzzerPin); // Stop the buzzer
digitalWrite(LEDPin, LOW); // Turn off the LED when the buzzer is off
Blynk.virtualWrite(V1, 0); // Turn off the LED widget in the Blynk app
Serial.println("Buzzer OFF, LED OFF");
}
}
// Function to measure distance using the ultrasonic sensor
void measureDistance()
{
// Send a 10us pulse to trigger the sensor
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Read the duration of the echo pulse
duration = pulseIn(echoPin, HIGH);
// Calculate the distance in centimeters
distance = duration * 0.0344 / 2;
Serial.print("Distance: ");
Serial.print(distance); // Print the distance on the serial monitor
Serial.println(" cm");
// Trigger the buzzer and LED if the distance is 2 cm or less
if (distance <= 200)
{
tone(buzzerPin, 1200); // Start the buzzer
digitalWrite(LEDPin, HIGH); // Turn on the LED
Blynk.virtualWrite(V1, 255); // Turn on the LED widget in the Blynk app
Serial.println("Buzzer ON, LED ON (Triggered by distance)");
}
else if (buzzerState == 0) // Ensure manual control does not interfere
{
noTone(buzzerPin); // Stop the buzzer
digitalWrite(LEDPin, LOW); // Turn off the LED
Blynk.virtualWrite(V1, 0); // Turn off the LED widget in the Blynk app
Serial.println("Buzzer OFF, LED OFF (Distance safe)");
}
}
// Setup function
void setup()
{
pinMode(trigPin, OUTPUT); // Sets the trigPin as an OUTPUT
pinMode(echoPin, INPUT); // Sets the echoPin as an INPUT
pinMode(buzzerPin, OUTPUT); // Sets the buzzerPin as an OUTPUT
pinMode(LEDPin, OUTPUT); // Sets LEDPin as an OUTPUT
Serial.begin(9600); // Start serial communication at 9600 baudrate
Serial.println("Distance measurement using ESP32");
Blynk.begin(auth, ssid, pass); // Connect to Blynk server
// Setup a function to measure distance every 100ms
timer.setInterval(100L, measureDistance);
}
// Main loop function
void loop()
{
Blynk.run(); // Run Blynk
timer.run(); // Run Blynk timer to measure distance
}