#define BLYNK_TEMPLATE_ID "TMPL3V6lRdlXC"
#define BLYNK_TEMPLATE_NAME "SMART WASTE MANAGEMENT"
#define BLYNK_AUTH_TOKEN "ZpOOGgEraGUjK-jkDBm2TgheujvaKzw8"
#define TRIGGER_PIN_1 12 // GPIO pin connected to the trigger pin of the first ultrasonic sensor
#define ECHO_PIN_1 14 // GPIO pin connected to the echo pin of the first ultrasonic sensor
#define TRIGGER_PIN_2 16 // GPIO pin connected to the trigger pin of the second ultrasonic sensor
#define ECHO_PIN_2 17 // GPIO pin connected to the echo pin of the second ultrasonic sensor
#define ECHO_PIN_3 25 // GPIO pin connected to the echo pin of the third ultrasonic sensor
#define TRIGGER_PIN_3 27 //GPIO pin connected to the trigger pin of the third ultrasonic sensor
#define LED_PIN_1_ON 2 // GPIO pin connected to the first LED ON pin
#define LED_PIN_1_OFF 0 // GPIO pin connected to the first LED OFF pin
#define LED_PIN_2_ON 4 // GPIO pin connected to the second LED ON pin
#define LED_PIN_2_OFF 5 // GPIO pin connected to the second LED OFF pin
#define LED_PIN_3_ON 19//GPIO pin connected to the third LED ON pin
#define LED_PIN_3_OFF 18// GPIO pin connected to the second LED OFF pin
#define SERVO_PIN_1 13// GPIO pin connected to the servo1 pin for the first ultrasonic sensor
#define SERVO_PIN_2 22//GPIO pin connected to the servo2 pin for the second ultrasonic sensor
#define SERVO_PIN_3 32//GPIO pin connected to the servo3 pin for the third ultrasonic sensor
#include <ESP32Servo.h>
#include <Ultrasonic.h>
#include<BlynkSimpleEsp32.h>
Ultrasonic ultrasonic1(TRIGGER_PIN_1, ECHO_PIN_1);
Ultrasonic ultrasonic2(TRIGGER_PIN_2, ECHO_PIN_2);
Ultrasonic ultrasonic3(TRIGGER_PIN_3,ECHO_PIN_3);
int pos=0;
Servo servo1;
Servo servo2;
Servo servo3;
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
char auth[] = "ZpOOGgEraGUjK-jkDBm2TgheujvaKzw8";
void setup() {
Serial.begin(9600); // Initialize serial communication
pinMode(LED_PIN_1_ON, OUTPUT); // Set LED 1 ON pin as an output
pinMode(LED_PIN_1_OFF, OUTPUT); // Set LED 1 OFF pin as an output
pinMode(LED_PIN_2_ON, OUTPUT); // Set LED 2 ON pin as an output
pinMode(LED_PIN_2_OFF, OUTPUT);//Set LED 2 OFF pin as an output
pinMode(LED_PIN_3_ON, OUTPUT);//Set LED 3 ON pin as an output
pinMode(LED_PIN_3_OFF, OUTPUT); // Set LED 2 OFF pin as an output
servo1.attach(SERVO_PIN_1);//
servo2.attach(SERVO_PIN_2);
servo3.attach(SERVO_PIN_3);
Blynk.begin(auth, ssid, pass);
}
void loop() {
Blynk.run();
long distance1 = measureDistance(TRIGGER_PIN_1, ECHO_PIN_1);
long distance2 = measureDistance(TRIGGER_PIN_2, ECHO_PIN_2);
long distance3 = measureDistance(TRIGGER_PIN_3, ECHO_PIN_3);
long a = 399 - distance1;//Read distance from the first ultrasonic sensor
long b = 399 - distance2; // Read distance from the second ultrasonic sensor
long c = 399 - distance3;//Read distance from the third ultrasonic sensor
Serial.print("Bin1 fill level: ");
Serial.print(a);
Serial.print(" cm | Bin2 fill level: ");
Serial.print(b);
Serial.print(" cm |Bin3 fill level: ");
Serial.print(c);
Serial.println(" cm");
Blynk.virtualWrite(V1, a ); // Send distance 1 to Blynk app
Blynk.virtualWrite(V2, b); // Send distance 2 to Blynk app
Blynk.virtualWrite(V3, c);// Send distance 3 to Blynk app
if(distance1<100 && distance1>5){
Serial.println("bin1 is going to fill !!!");
}
if(distance2< 100 && distance2>5){
Serial.println("bin2 is going to fill !!!");
}
if(distance3<100 && distance3>5){
Serial.println("bin3 is going to fill !!!");
}
// Control LED 1 based on distance from sensor 1
if (distance1 < 5) {
Serial.println("Bin1 is full!!. Don't put waste on it!!");
digitalWrite(LED_PIN_1_ON, HIGH); // Turn on LED 1
digitalWrite(LED_PIN_1_OFF, LOW);//Turn off LED 1
pos=0;// Turn the servo1 angle 0
servo1.write(pos);
Serial.println("dustbin1 is closed");
delay(1000);
}
else {
digitalWrite(LED_PIN_1_ON, LOW); // Turn off LED 1
digitalWrite(LED_PIN_1_OFF, HIGH); // Turn on LED 1
pos=90;// Turn the servo1 angle 90
servo1.write(pos);
Serial.println("dustbin1 is open");
delay(1000);
}
// Control LED 2 based on distance from sensor 2
if (distance2 < 5) {
Serial.println("Bin2 is full!!. Don't put waste on it!!");
digitalWrite(LED_PIN_2_ON, HIGH); // Turn on LED 2
digitalWrite(LED_PIN_2_OFF, LOW);
pos=0;// Turn the servo2 angle 0
servo2.write(pos);
Serial.println("dustbin2 is closed");
delay(1000);// Turn off LED 2
}
else
{
digitalWrite(LED_PIN_2_ON, LOW); // Turn off LED 2
digitalWrite(LED_PIN_2_OFF, HIGH); // Turn on LED 2
pos=90;// Turn the 2 angle 90
servo2.write(pos);
Serial.println("dustbin2 is open");
delay(1000);
}
// Control LED 3 based on distance from sensor 3
if (distance3 < 5) {
Serial.println("bin3 is full!!. Don't put waste on it!!");
digitalWrite(LED_PIN_3_ON, HIGH); // Turn on LED 2
digitalWrite(LED_PIN_3_OFF, LOW); // Turn off LED 2
pos=0;// Turn the servo3 angle 0
servo3.write(pos);
delay(1000);
}
else {
digitalWrite(LED_PIN_3_ON, LOW); // Turn off LED 2
digitalWrite(LED_PIN_3_OFF, HIGH); // Turn on LED 2
Serial. println("dustbin3 is open");
pos=90;//Turn the servo3 angle 90
servo3.write(pos);
delay(1000);
}
delay(1000);// Delay for stability
}
long measureDistance(int tp, int ep)
{
digitalWrite(tp, LOW);
delay(2);
digitalWrite(tp, HIGH);
delay(10);
digitalWrite(tp, LOW);
long duration = pulseIn(ep, HIGH);
long distance = duration * 0.034 / 2 ;
return distance;
}