Overview
Unsorted household waste cross-contaminates recycling streams and ruins compost batches. Manual segregation at the point of disposal is the cheapest place to fix it — and the most neglected.
This prototype closes the loop mechanically: an ultrasonic sensor watches the disposal chute, a resistive moisture probe samples the item, and a servo-driven flap routes it to the wet or dry compartment.
Key focus areas:
- Distance gating so the probe only reads while an item is actually present
- A deterministic wet/dry decision from the LM393 comparator output
- Repeatable actuation sequence with a neutral rest position between cycles
- A shared 5V/GND distribution rail instead of daisy-chained sensor power
How It Works
System Architecture
Sensing and Sorting Loop
One polling loop, two sensors, one actuator. The servo position is the only state the system carries between cycles.
Hardware Build
Wiring & Pinout
The moisture board exposes both a raw analog output (A0, 10-bit ADC) and a comparator-driven digital output (D0). The shipped firmware reads D0 so the wet/dry decision is already thresholded in hardware — one digital read, no float comparison in the loop.
Firmware
#include <Servo.h>
// --- Pin Definitions (matching the breadboard diagram) ---
const int echoPin = 7; // HC-SR04 Echo (Orange)
const int trigPin = 6; // HC-SR04 Trig (Green)
const int servoPin = 4; // Servo PWM signal (Maroon)
const int moisturePin = 5; // Moisture sensor D0 (Blue)
// --- Operating Parameters ---
const int DETECTION_DISTANCE = 20; // Trigger threshold in centimeters
const int POS_NEUTRAL = 90; // Center / rest position (flap level)
const int POS_WET = 0; // Tilt flap toward wet bin
const int POS_DRY = 180; // Tilt flap toward dry bin
Servo segregatorServo;
void setup() {
Serial.begin(9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(moisturePin, INPUT);
segregatorServo.attach(servoPin);
segregatorServo.write(POS_NEUTRAL);
delay(1000);
Serial.println("Smart Dustbin Ready.");
}
void loop() {
long distance = getDistance();
// Check if an object / hand is within the detection zone
if (distance > 0 && distance <= DETECTION_DISTANCE) {
Serial.print("Object detected at: ");
Serial.print(distance);
Serial.println(" cm");
// Brief pause to allow trash to rest on the sensor probe
delay(500);
// Standard LM393 modules pull D0 LOW when wet (conductive)
int moistureState = digitalRead(moisturePin);
if (moistureState == LOW) {
Serial.println("Classification: WET WASTE -> Tilting to Wet Bin");
segregatorServo.write(POS_WET);
} else {
Serial.println("Classification: DRY WASTE -> Tilting to Dry Bin");
segregatorServo.write(POS_DRY);
}
// Wait 3 seconds for trash to slide into the bin
delay(3000);
// Return flap back to center
segregatorServo.write(POS_NEUTRAL);
Serial.println("Returning to Neutral.");
delay(1000);
}
delay(100); // Polling delay
}
// Distance in centimeters from the HC-SR04 echo pulse width
long getDistance() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Timeout after 30ms (~5 meters max) to prevent blocking
long duration = pulseIn(echoPin, HIGH, 30000);
if (duration == 0) return 999; // No echo received
return duration * 0.034 / 2;
}Technical Challenges
Results
Lessons Learned
- Power first: most "flaky sensor" bugs on a breadboard are a shared-ground or current-sag problem, not a firmware bug.
- Let hardware threshold: the LM393 comparator makes the wet/dry decision deterministic and keeps the ADC out of the control loop.
- Explicit sequences win: naming each step (settle → classify → dwell → reset) made the mechanism easy to debug on the bench.
- Next iteration: replace the blocking
delay()chain with amillis()-based state machine so the loop can keep monitoring distance while the servo is dwelling.
Liked this project?
Check out more of my work or get in touch.