#include <LiquidCrystal.h>
#include <Servo.h>
// Initialize LCD without I2C
LiquidCrystal lcd(2, 4, 14, 12, 13, 27);
// Define the Servo motors
Servo servoSelector;
Servo servoLid;
// Define sensor pins (using potentiometers in Wokwi)
const int soilMoisturePin = 32; // Potentiometer for soil moisture simulation
const int proximitySensorPin = 33; // Potentiometer for proximity simulation
const int trigPin = 27;
const int echoPin = 26;
// Define buzzer pin
const int buzzerPin = 25;
// Define LED pins
const int greenLEDPin = 14; // Green LED for dry waste
const int blueLEDPin = 12; // Blue LED for wet waste
const int yellowLEDPin = 13; // Yellow LED for metal waste
const int redLEDPin = 15; // Red LED for full bin indicator
// Threshold values
const int soilMoistureThreshold = 500; // Adjust based on calibration
const int proximityThreshold = 100; // Adjust based on calibration
const int distanceThreshold = 100; // Threshold for ultrasonic sensor
void setup() {
// Initialize LCD
lcd.begin(16, 2);
// Initialize servos
servoSelector.attach(19); // GPIO 19 for selector servo
servoLid.attach(18); // GPIO 18 for lid servo
// Initialize ultrasonic sensor pins
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
// Initialize buzzer pin
pinMode(buzzerPin, OUTPUT);
// Initialize LED pins
pinMode(greenLEDPin, OUTPUT);
pinMode(blueLEDPin, OUTPUT);
pinMode(yellowLEDPin, OUTPUT);
pinMode(redLEDPin, OUTPUT);
// Display startup message
lcd.setCursor(0, 0);
lcd.print("Smart Dustbin");
lcd.setCursor(0, 1);
lcd.print("Initializing...");
delay(2000);
lcd.clear();
// Set initial positions for servos
servoSelector.write(0);
servoLid.write(0);
// Initial LED states
digitalWrite(redLEDPin, LOW); // Bin not full
}
void loop() {
// Ultrasonic sensor
long duration, distance;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration / 2) / 29.1;
// Check if bin is full
if (distance <= distanceThreshold) {
lcd.setCursor(0, 0);
lcd.print("BIN IS FULL!");
digitalWrite(buzzerPin, HIGH);
digitalWrite(redLEDPin, HIGH);
digitalWrite(greenLEDPin, LOW);
digitalWrite(blueLEDPin, LOW);
digitalWrite(yellowLEDPin, LOW);
return;
} else {
digitalWrite(buzzerPin, LOW);
digitalWrite(redLEDPin, LOW);
}
// Proximity sensor (force sensor replacement)
int proximityValue = analogRead(proximitySensorPin);
if (proximityValue > proximityThreshold) {
lcd.setCursor(0, 0);
lcd.print("Metal Detected");
lcd.setCursor(0, 1);
lcd.print("Bin: Metal");
digitalWrite(yellowLEDPin, HIGH);
delay(2000);
digitalWrite(yellowLEDPin, LOW);
lcd.clear();
moveServo(180);
} else {
// Soil moisture sensor (replaced with potentiometer)
int soilMoistureValue = analogRead(soilMoisturePin);
if (soilMoistureValue > soilMoistureThreshold) {
lcd.setCursor(0, 0);
lcd.print("Wet Waste Detected");
lcd.setCursor(0, 1);
lcd.print("Bin: Wet");
digitalWrite(blueLEDPin, HIGH);
delay(2000);
digitalWrite(blueLEDPin, LOW);
lcd.clear();
moveServo(90);
} else {
lcd.setCursor(0, 0);
lcd.print("Dry Waste Detected");
lcd.setCursor(0, 1);
lcd.print("Bin: Dry");
digitalWrite(greenLEDPin, HIGH);
delay(2000);
digitalWrite(greenLEDPin, LOW);
lcd.clear();
moveServo(0);
}
}
delay(100);
}
void moveServo(int angle) {
servoSelector.write(angle); // Selector servo to section
delay(1000);
servoLid.write(90); // Open lid
delay(2000);
servoLid.write(0); // Close lid
}