#define BLYNK_TEMPLATE_ID "TMPL3Ba_0DVnp"
#define BLYNK_TEMPLATE_NAME "NM ST IOT005"
#define BLYNK_AUTH_TOKEN "64d8G0lRy61DZcPB6gvBRDLabmTNxoQn"
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <BlynkSimpleEsp32.h>
// WiFi credentials
char auth[] = BLYNK_AUTH_TOKEN;
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
// Pin definitions
#define TRIG_PIN 5
#define ECHO_PIN 18
#define RED_PIN 14
#define GREEN_PIN 27
#define BLUE_PIN 26
#define BUZZER_PIN 25
// Display objects
LiquidCrystal_I2C lcd(0x27, 16, 2);
// Function declarations
double readDistance();
void setRGBColor(int red, int green, int blue);
void setup() {
Serial.begin(115200);
// Initialize Blynk
Blynk.begin(auth, ssid, pass);
// Initialize LCD
lcd.init(); // Use init() instead of begin() for LiquidCrystal_I2C library
lcd.backlight();
lcd.print("LCD Initialized...");
delay(2000); // Display the initialization message for 2 seconds
lcd.clear();
// Initialize ultrasonic sensor
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
// Initialize RGB LED pins
pinMode(RED_PIN, OUTPUT);
pinMode(GREEN_PIN, OUTPUT);
pinMode(BLUE_PIN, OUTPUT);
// Initialize Buzzer
pinMode(BUZZER_PIN, OUTPUT);
// Set up PWM channels for the RGB LED
ledcSetup(0, 5000, 8); // Channel 0, 5 kHz, 8-bit resolution
ledcSetup(1, 5000, 8); // Channel 1, 5 kHz, 8-bit resolution
ledcSetup(2, 5000, 8); // Channel 2, 5 kHz, 8-bit resolution
ledcAttachPin(RED_PIN, 0);
ledcAttachPin(GREEN_PIN, 1);
ledcAttachPin(BLUE_PIN, 2);
}
void loop() {
Blynk.run();
// Read distance from ultrasonic sensor
double distance = readDistance();
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
// Update RGB LED and Buzzer based on distance
if (distance < 50) {
// Turn on red light and buzzer, turn off other lights
setRGBColor(255, 0, 0);
digitalWrite(BUZZER_PIN, HIGH);
Blynk.virtualWrite(V1, "STOP");
lcd.setCursor(0, 1);
lcd.print("STOP "); // Adding spaces to clear previous value
} else if (distance >= 50 && distance <= 100) {
// Turn on blue light
setRGBColor(0, 0, 255);
digitalWrite(BUZZER_PIN, LOW);
Blynk.virtualWrite(V1, "CAUTION");
lcd.setCursor(0, 1);
lcd.print("Get Ready "); // Adding spaces to clear previous value
} else {
// Turn on green light
setRGBColor(0, 255, 0);
digitalWrite(BUZZER_PIN, LOW);
Blynk.virtualWrite(V1, "GO");
lcd.setCursor(0, 1);
lcd.print("GO " ); // Adding spaces to clear previous value
}
// Update LCD with distance
lcd.setCursor(0, 0);
lcd.print("Distance: ");
lcd.print(distance);
lcd.print(" cm "); // Adding extra spaces to clear the previous value
// Send data to Blynk
Blynk.virtualWrite(V2, distance); // Measured distance
delay(500); // Adjust the delay as needed
}
double readDistance() {
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
long duration = pulseIn(ECHO_PIN, HIGH);
double distance = duration * 0.034 / 2;
return distance;
}
void setRGBColor(int red, int green, int blue) {
ledcWrite(0, red);
ledcWrite(1, green);
ledcWrite(2, blue);
}
BLYNK_WRITE(V1) {
// Here, you can add functionality to set the setpoint if needed
}