What does this project do?
We build a trash can whose lid opens by itself when you bring your hand close and closes again a few seconds later. An ultrasonic sensor detects the approaching hand and a servo motor lifts the lid. It’s hygienic, and it always gets a lot of attention in the classroom.
- Age group: 11 and up
- Level: Advanced
- Time: About 60–90 minutes (including preparing the bin)
- What you will learn: controlling a servo motor, measuring distance with an ultrasonic sensor, creating movement based on sensor data
Parts list
| Part | Qty |
|---|---|
| Arduino Uno (or compatible) | 1 |
| HC-SR04 ultrasonic distance sensor | 1 |
| SG90 servo motor | 1 |
| Small trash can with a lid, or a cardboard box | 1 |
| Breadboard (a small one is enough) | 1 |
| Male-to-male and male-to-female jumper wires | ~10 |
| Hot glue, double-sided tape, ice cream sticks | – |
| USB cable or 9V battery + battery clip | 1 |
Wiring

| Component | Component pin | Arduino pin |
|---|---|---|
| HC-SR04 | VCC | 5V |
| HC-SR04 | Trig | D9 |
| HC-SR04 | Echo | D10 |
| HC-SR04 | GND | GND |
| Servo | Brown (GND) | GND |
| Servo | Red (+) | 5V |
| Servo | Orange (signal) | D6 |
Code
// Arduino Smart Trash Can - ozandamci.com
#include <Servo.h>
const int trigPin = 9;
const int echoPin = 10;
const int servoPin = 6;
const int openAngle = 100; // lid open position
const int closedAngle = 0; // lid closed position
const int detectDistance = 20; // open if a hand is closer than this (cm)
Servo lid;
void setup() {
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
lid.attach(servoPin);
lid.write(closedAngle);
Serial.begin(9600);
}
int measureDistance() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
long duration = pulseIn(echoPin, HIGH, 30000);
if (duration == 0) return -1; // nothing in front of the sensor
return duration * 0.034 / 2; // centimetres
}
void loop() {
int distance = measureDistance();
Serial.print("Distance: ");
Serial.println(distance);
if (distance > 0 && distance < detectDistance) {
lid.write(openAngle);
delay(3000); // keep the lid open for 3 seconds
// Don't close the lid while the hand is still there
while (true) {
int newDistance = measureDistance();
if (newDistance <= 0 || newDistance >= detectDistance) break;
delay(500);
}
// Close the lid slowly
for (int angle = openAngle; angle >= closedAngle; angle--) {
lid.write(angle);
delay(10);
}
}
delay(100);
}
No Arduino? Try this circuit in your browser with the Wokwi simulation → (free, nothing to install). Click the HC-SR04 sensor to change the distance.
Step-by-step instructions
- Build the circuit. Connect the sensor and the servo to the Arduino as shown in the table. Share 5V and GND through the breadboard’s power rails.
- Upload the code and test it on the table first. When you bring your hand close to the sensor, the servo arm should turn.
- Adjust the angles. Attach the servo arm in the position that lifts the lid. If the lid doesn’t open far enough, try an
openAnglevalue between 120 and 150. - Mount the sensor on the bin. Fix the HC-SR04 to the front of the bin with hot glue, with its “eyes” facing outwards.
- Mount the servo. Glue the servo to the back edge of the bin, on the hinge side. Add an ice cream stick to the servo arm and position it so it touches the underside of the lid.
- Place the Arduino. Tape the Arduino and the breadboard to the back or side of the bin. You can also power it with a 9V battery instead of USB.
- Test it. Bring your hand closer than 20 cm to the front of the bin; the lid should open and close slowly 3 seconds after you take your hand away.
Troubleshooting
- The servo jitters or the Arduino restarts: The servo may be drawing too much current. Make the lid lighter or power the servo from a separate 5V supply (the GNDs must be connected together).
- The lid opens by itself: The sensor may be seeing the edge of the bin; move the sensor a little further forward or lower the
detectDistancevalue.
Try it yourself
- Put a second sensor inside the bin and add a “full” LED that lights up when the bin is full.
- Play a short thank-you tune when the lid opens.
For teachers: a two-page activity sheet for using this project in class: lesson plan, discussion questions, answer key and a student worksheet.





“Arduino Smart Trash Can: Lid Opens When You Wave (Code)” için 2 yorum