#define BLYNK_TEMPLATE_ID "TMPL6ej14n0Vo"
#define BLYNK_TEMPLATE_NAME "Quickstart Template"
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include <NewPing.h>
// Your Wi-Fi credentials
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
// Your Blynk authentication token
char auth[] = "A-TrV61D62-nRZMRp-f9FGypdohD46YG";
// Define the GPIO pins for the ultrasonic sensor
#define trigPin 2
#define echoPin 4
#define buzzer 18
#define ledred 19
// Create an instance of the NewPing library
NewPing sonar(trigPin, echoPin);
void setup() {
Serial.begin(115200);
Blynk.begin(auth, ssid, pass);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(buzzer, OUTPUT);
pinMode(ledred, OUTPUT);
}
void controlBuzzerAndLEDs(long distance) {
if (distance < 201) {
// If the distance is less than 199
digitalWrite(ledred, HIGH);
digitalWrite(buzzer, HIGH);
delay(100); // Delay for buzzer ON duration
digitalWrite(buzzer, LOW);
delay(100); // Delay for buzzer OFF duration
} else {
// If the distance is greater than or equal to 199 cm, turn off the red LED and turn off the buzzer
digitalWrite(ledred, LOW);
digitalWrite(buzzer, LOW);
}
}
void loop() {
Blynk.run();
long duration, distance;
// Trigger ultrasonic sensor
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Read the echo pulse
duration = pulseIn(echoPin, HIGH);
// Calculate distance in centimeters
distance = (duration / 2) / 29.1;
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
// Check the distance and control LEDs and buzzer
controlBuzzerAndLEDs(distance);
// Send the distance data to Blynk
Blynk.virtualWrite(V4, distance); // Use V4 as a display widget in your Blynk app
delay(1000); // Delay between measurements
}
esp:0
esp:2
esp:4
esp:5
esp:12
esp:13
esp:14
esp:15
esp:16
esp:17
esp:18
esp:19
esp:21
esp:22
esp:23
esp:25
esp:26
esp:27
esp:32
esp:33
esp:34
esp:35
esp:3V3
esp:EN
esp:VP
esp:VN
esp:GND.1
esp:D2
esp:D3
esp:CMD
esp:5V
esp:GND.2
esp:TX
esp:RX
esp:GND.3
esp:D1
esp:D0
esp:CLK
ultrasonic1:VCC
ultrasonic1:TRIG
ultrasonic1:ECHO
ultrasonic1:GND
led1:A
led1:C
bz1:1
bz1:2