Ever had your hands full of messy kitchen scraps and struggled to open the trash can? We’re building a fully automated, hands-free smart trash can using an Arduino, an ultrasonic sensor, and a servo motor. When your hand approaches, the lid pops open. A few seconds later, it slams shut. It brings a touch of sci-fi automation right into your kitchen, until the batteries die and you’re locked out of your own garbage.

The brain of the operation is the Arduino Uno. An HC-SR04 Ultrasonic Sensor sits on or near the lid, continuously sending out high-frequency sound waves. If those waves bounce off your hand and return within a certain timeframe (indicating your hand is close), the Arduino triggers the SG90 Micro Servo, which is physically attached to the hinge of the trash can lid.
We’re going to keep this simple. You can retrofit almost any lightweight desktop or small kitchen trash can with a swinging or hinged lid.
(Note: You will also need a small trash can with a hinged lid, some hot glue, and a 9V battery or power bank to power the Arduino).
This code constantly measures the distance using the ultrasonic sensor. If an object is detected within 15 cm (about 6 inches), it moves the servo to open the lid, waits 3 seconds, and then closes it.
#include <Servo.h>
Servo lidServo;
const int trigPin = 5;
const int echoPin = 6;
const int servoPin = 9;
long duration;
int distance;
void setup() {
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
lidServo.attach(servoPin);
// Make sure the lid starts closed (adjust angle as needed for your specific build)
lidServo.write(0);
Serial.begin(9600);
}
void loop() {
// Trigger the sensor
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Read the echo
duration = pulseIn(echoPin, HIGH);
// Calculate distance in cm
distance = duration * 0.034 / 2;
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
// If hand is within 15 cm, open the lid
if (distance > 0 && distance <= 15) {
lidServo.write(90); // Open position
delay(3000); // Keep open for 3 seconds
lidServo.write(0); // Close position
}
delay(100); // Short delay before next reading
}
lidServo.write(0) and lidServo.write(90) values in the code so the lid closes flush and opens wide enough.