/*
Wokwi | general
Mau.chalco — 7/20/25 at 6:53 PM
Could you help me with this project?
I want to make the buzzer sound and also turn on an LED.
https://wokwi.com/projects/436785646621448193
*/
// ==========================
// Blynk Configuration (Unique project credentials)
// ==========================
#define BLYNK_PRINT Serial
#define BLYNK_TEMPLATE_ID "TMPL22xzOPhVa" // Template ID from Blynk.Console
#define BLYNK_TEMPLATE_NAME "Carrito sensor" // Project name in the Blynk app
#define BLYNK_AUTH_TOKEN "oO-4ijFgIIhbTBR4mY0TIfJRROyACsEM" // Authentication token
// ==========================
// Required libraries
// ==========================
#include <WiFi.h> // Enables WiFi connection for the ESP32
#include <BlynkSimpleEsp32.h> // Allows communication with Blynk
#include <Wire.h> // I2C protocol (for LCD)
#include <LiquidCrystal_I2C.h> // Library for controlling I2C LCD
#include <NewPing.h> // Library for precise HC-SR04 readings
// ==========================
// Pin and constant definitions
// ==========================
#define TRIG_PIN 5 // GPIO5 connected to HC-SR04 Trigger
#define ECHO_PIN 18 // GPIO18 connected to HC-SR04 Echo
#define LED_PIN 2 // GPIO2 used for alert LED
#define BUZZER_PIN 19 // GPIO4 used for active buzzer
#define MAX_DISTANCE 400 // Maximum distance for the sensor (cm)
#define SDA_PIN 21 // GPIO21 for I2C SDA
#define SCL_PIN 22 // GPIO22 for I2C SCL
#define BTN_PIN 27
// ==========================
// Global objects
// ==========================
LiquidCrystal_I2C lcd(0x27, 16, 2); // LCD at I2C address 0x27, 16 columns x 2 rows
NewPing sonar(TRIG_PIN, ECHO_PIN, MAX_DISTANCE); // HC-SR04 sensor object
// ==========================
// WiFi credentials
// ==========================
char ssid[] = "Wokwi-GUEST"; // Network name (Wokwi simulator)
char pass[] = ""; // Password (empty for Wokwi)
// ==========================
// Blynk virtual pins
// ==========================
#define VPIN_DISTANCE V0 // Virtual pin for displaying distance
#define VPIN_ALERT V1 // Virtual pin for sending alert
bool alerta_enviada = false; // Prevents sending repeated notifications to Blynk
// ==========================
// Function: Averaging multiple readings to avoid errors
// ==========================
int getStableDistance() {
int sum = 0;
for (int i = 0; i < 5; i++) { // Take 5 readings
sum += sonar.ping_cm(); // Add each distance reading
delay(50); // Small delay between readings
}
return sum / 5; // Return the average distance
}
// ==========================
// Initial setup
// ==========================
void setup() {
Serial.begin(115200); // Start the serial monitor
Wire.begin(SDA_PIN, SCL_PIN); // Start I2C communication with custom pins
lcd.init(); // Initialize the LCD
lcd.backlight(); // Turn on LCD backlight
pinMode(LED_PIN, OUTPUT); // Set LED pin as output
pinMode(BUZZER_PIN, OUTPUT); // Set buzzer pin as output
Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass); // Connect to Blynk server
lcd.setCursor(0, 0);
lcd.print("Connecting..."); // Show connecting message
while (!Blynk.connected()) { // Wait until connected to Blynk
delay(500);
lcd.print(".");
}
lcd.clear();
lcd.print("System OK"); // Confirm system is ready
delay(1500);
lcd.clear();
}
// ==========================
// Main loop
// ==========================
void loop() {
Blynk.run(); // Keep Blynk connection alive
int distancia = getStableDistance(); // Get averaged distance
// Display distance on LCD
lcd.setCursor(0, 0);
lcd.print("Distance: ");
if (distancia == 0 || distancia > MAX_DISTANCE) {
lcd.print("-- cm "); // Error reading
Blynk.virtualWrite(VPIN_DISTANCE, 0);
} else {
lcd.print(distancia); // Valid distance
lcd.print(" cm ");
Blynk.virtualWrite(VPIN_DISTANCE, distancia);
}
// Alert logic (object closer than 200 cm)
if (distancia > 0 && distancia < 200) {
digitalWrite(LED_PIN, HIGH); // Turn on alert LED
tone(BUZZER_PIN, 1000); // Beep the buzzer (1kHz tone for 500ms)
lcd.setCursor(0, 1);
lcd.print("ALERT! Nearby ");
Blynk.virtualWrite(VPIN_ALERT, 1);
if (!alerta_enviada) {
Blynk.logEvent("alerta_distancia", "🚨 Object at " + String(distancia) + " cm!");
alerta_enviada = true;
}
} else {
digitalWrite(LED_PIN, LOW); // Turn off LED
noTone(BUZZER_PIN); // Silence buzzer
lcd.setCursor(0, 1);
lcd.print(" "); // Clear second line
Blynk.virtualWrite(VPIN_ALERT, 0);
alerta_enviada = false;
}
delay(300); // Wait before next reading
}