#define BLYNK_TEMPLATE_ID "TMPL3Ee-3m9ow"
#define BLYNK_TEMPLATE_NAME "Smart Trash Can Tracker"
#define BLYNK_AUTH_TOKEN "rWJkD5-TglvcFW5qESVtxgb0Pyxao_gT"
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <HX711.h>
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
// Blynk authentication and Wi-Fi credentials
char auth[] = BLYNK_AUTH_TOKEN;
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
// Pin configurations for ultrasonic sensors
#define trigPin1 13
#define echoPin1 14
#define trigPin2 18
#define echoPin2 19
// Pin configurations for weight sensors
#define weightDt1 16
#define weightSck1 4
#define weightDt2 17
#define weightSck2 23
// Virtual pins for Blynk
#define button1_vpin V1
#define button2_vpin V2
#define button3_vpin V3
#define button4_vpin V4
// LCD setup
LiquidCrystal_I2C lcd(0x27, 20, 4);
// Calibration factor for load cell
const int conversionFactor = 420;
// Fill level threshold
const int fillLevelThreshold = 80;
// Weight sensor objects
HX711 scale1;
HX711 scale2;
void setup() {
Serial.begin(9600);
pinMode(trigPin1, OUTPUT);
pinMode(echoPin1, INPUT);
pinMode(trigPin2, OUTPUT);
pinMode(echoPin2, INPUT);
// Initialize Wi-Fi connection
Serial.print("Connecting to Wi-Fi...");
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("WiFi connected!");
// Initialize LCD display
lcd.init();
lcd.backlight();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Bio Waste:");
lcd.setCursor(0, 2);
lcd.print("Non-Bio Waste:");
// Initialize weight sensors
scale1.begin(weightDt1, weightSck1);
scale2.begin(weightDt2, weightSck2);
// Connect to Blynk server
Blynk.begin(auth, ssid, pass);
}
void loop() {
Blynk.run();
fillLevelDetection();
}
void fillLevelDetection() {
// Read fill level and weight from Biodegradable Waste
int fillLevel1 = readUltrasonicSensor(trigPin1, echoPin1);
int weight1 = scale1.get_units(10) / conversionFactor;
// Read fill level and weight from Non-Biodegradable Waste
int fillLevel2 = readUltrasonicSensor(trigPin2, echoPin2);
int weight2 = scale2.get_units(10) / conversionFactor;
// Update LCD display
lcd.setCursor(0, 1);
lcd.print(" ");
lcd.setCursor(0, 1);
lcd.print(fillLevel1);
lcd.print("% filled, ");
lcd.print(weight1);
lcd.print(" kg");
lcd.setCursor(0, 3);
lcd.print(" ");
lcd.setCursor(0, 3);
lcd.print(fillLevel2);
lcd.print("% filled, ");
lcd.print(weight2);
lcd.print(" kg");
// Send data to Blynk
Blynk.virtualWrite(button1_vpin, fillLevel1);
Blynk.virtualWrite(button2_vpin, weight1);
Blynk.virtualWrite(button3_vpin, fillLevel2);
Blynk.virtualWrite(button4_vpin, weight2);
// Notify when either trash can reaches 80 percent or exceeds 40kg
if (fillLevel1 >= fillLevelThreshold || weight1 >= 40) {
Blynk.logEvent("bio_trigger");
Serial.println("Biodegradable waste almost full ! Kindly dispose.");
delay(1000);
}
if (fillLevel2 >= fillLevelThreshold || weight2 >= 40) {
Blynk.logEvent("non_bio_trigger");
Serial.println("Non-Biodegradable waste almost full ! Kindly dispose.");
delay(1000);
}
}
int readUltrasonicSensor(int trigPin, int echoPin) {
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
int duration = pulseIn(echoPin, HIGH);
int distance = duration / 58;
int fill = 100 - map(distance, 0, 100, 0, 100);
int fillLevel = 0;
// Incase value of fill exceeds 100 (Here height of trash can is 100cm)
if (fill >= 0) {
fillLevel = fill;
} else if (fill < 0) {
fillLevel = 0;
}
return fillLevel;
}
Bio Waste Fill Level
Non-Bio Waste Fill Level
Bio Waste Weight
Non-Bio Waste Weight
Displays Fill Levels and Weights of Trash Cans