Arduino Smart Trash Can: Lid Opens When You Wave (Code)

Arduino ile Otomatik Kapaklı Akıllı Çöp Kutusu Yapımı projesi kapak görseli

🇹🇷 Bu yazıyı Türkçe okuyun

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

PartQty
Arduino Uno (or compatible)1
HC-SR04 ultrasonic distance sensor1
SG90 servo motor1
Small trash can with a lid, or a cardboard box1
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 clip1

Wiring

Arduino smart trash can with automatic lid wiring diagram
ComponentComponent pinArduino pin
HC-SR04VCC5V
HC-SR04TrigD9
HC-SR04EchoD10
HC-SR04GNDGND
ServoBrown (GND)GND
ServoRed (+)5V
ServoOrange (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

  1. 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.
  2. Upload the code and test it on the table first. When you bring your hand close to the sensor, the servo arm should turn.
  3. Adjust the angles. Attach the servo arm in the position that lifts the lid. If the lid doesn’t open far enough, try an openAngle value between 120 and 150.
  4. Mount the sensor on the bin. Fix the HC-SR04 to the front of the bin with hot glue, with its “eyes” facing outwards.
  5. 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.
  6. 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.
  7. 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 detectDistance value.

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.

Video

The narration is in Turkish. English subtitles are available: click CC on the video.

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

Bir Cevap Yazın