Home › DIY Projects › Smart Dustbin

Smart Dustbin with Arduino, Ultrasonic Sensor and Servo (Touch-Free Lid)

Wave your hand and the lid opens by itself, then closes 3 seconds later. A fun, useful first robotics project with just three parts.

  • Level: Beginner
  • Time: 45 minutes
  • Board: Arduino Uno
  • No soldering
▶ Copy code + open simulatorGet the code

Watch it in action

Subscribe to ZEENAIX on YouTube for more projects

What you will build

An HC-SR04 ultrasonic sensor on the front of a small dustbin watches for a hand. When something comes closer than 20 cm, the Arduino tells an SG90 servo to swing the lid open to 90°. The lid stays open while your hand is there and for 3 seconds after it leaves, then the servo closes it again. No touching, so it is cleaner for kitchens, classrooms and clinics.

You will learn how to read distance with pulseIn(), how to move a servo with the Arduino Servo library, and how to use millis() to keep something open for a set time without stopping the program. Try it in the simulator first, then fix it on a real bin.

Components required

Arduino Uno R3 compatible boardArduino Uno R3 × 1The brain. Any Uno or Nano works.
View in store →
HC-SR04 ultrasonic distance sensorHC-SR04 ultrasonic sensor × 1Measures about 2 cm to 4 m.
View in store →
SG90 micro servo motorSG90 micro servo × 1Turns to an exact angle, 0 to 180°.
View in store →
Breadboard × 1Half or full size.
View in store →
Jumper wires (male to male) × 10About 10 wires.
View in store →
Small dustbin with a flip lid × 1Any light plastic bin with a hinged lid. Not sold by us.

Circuit diagram and connections

Tap any part in the diagram to see what it does and where it connects.

Arduino Uno9 1065V GND HC-SR04 SG90 Red = 5V · Black/Brown = GND · Orange = signal / Trig · Blue = Echo

Tap a part to learn more.

Part pinArduino pinWire colour (suggested)
HC-SR04 VCC5VRed
HC-SR04 TrigD9Orange
HC-SR04 EchoD10Blue
HC-SR04 GNDGNDBlack
SG90 signal (orange)D6Orange
SG90 power (red)5VRed
SG90 ground (brown)GNDBrown or black

Arduino code

Copy this sketch into the simulator or the Arduino IDE. We wrote and tested it ourselves; you are free to use and change it.

/*
  ZEENAIX DIY Project: Smart Dustbin with Arduino (touch-free lid)
  https://zeenaix.com/diy-projects/smart-dustbin-arduino/

  Parts: Arduino Uno, HC-SR04 ultrasonic sensor, SG90 micro servo,
         jumper wires (and a small dustbin with a hinged lid).

  Wiring:
    HC-SR04  VCC -> 5V   GND -> GND   TRIG -> pin 9   ECHO -> pin 10
    SG90     red -> 5V   brown -> GND orange (signal) -> pin 6

  What it does:
    Measures the distance every 100 ms. When a hand comes closer than
    OPEN_CM, the servo swings the lid open. The lid stays open while the
    hand is there and for HOLD_MS after it leaves, then closes again.
*/

#include <Servo.h>

const int TRIG_PIN  = 9;
const int ECHO_PIN  = 10;
const int SERVO_PIN = 6;

const int  OPEN_CM      = 20;    // open when a hand is closer than this
const int  LID_CLOSED   = 0;     // servo angle with the lid closed
const int  LID_OPEN     = 90;    // servo angle with the lid open
const unsigned long HOLD_MS = 3000;  // keep open this long after the hand leaves

Servo lid;
bool isOpen = false;
unsigned long lastSeen = 0;

long readCm() {
  digitalWrite(TRIG_PIN, LOW);
  delayMicroseconds(2);
  digitalWrite(TRIG_PIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG_PIN, LOW);
  unsigned long us = pulseIn(ECHO_PIN, HIGH, 30000UL);  // 30 ms = about 5 m
  if (us == 0) return -1;                                // nothing in range
  return (long)(us / 58);                                // microseconds to cm
}

void setup() {
  Serial.begin(9600);
  pinMode(TRIG_PIN, OUTPUT);
  pinMode(ECHO_PIN, INPUT);
  lid.attach(SERVO_PIN);
  lid.write(LID_CLOSED);
  Serial.println("ZEENAIX Smart Dustbin ready");
}

void loop() {
  long cm = readCm();
  unsigned long now = millis();
  bool handNear = (cm > 0 && cm < OPEN_CM);

  if (handNear) {
    lastSeen = now;
    if (!isOpen) {
      lid.write(LID_OPEN);
      isOpen = true;
      Serial.println("Hand detected: lid OPEN");
    }
  } else if (isOpen && now - lastSeen > HOLD_MS) {
    lid.write(LID_CLOSED);
    isOpen = false;
    Serial.println("Lid CLOSED");
  }

  static unsigned long lastPrint = 0;
  if (now - lastPrint >= 500) {
    lastPrint = now;
    Serial.print("Distance: ");
    if (cm < 0) Serial.print("--"); else Serial.print(cm);
    Serial.print(" cm | Lid: ");
    Serial.println(isOpen ? "OPEN" : "CLOSED");
  }
  delay(100);
}

Step-by-step instructions

A. Try it in the free simulator (5 minutes)

  1. Open the ZEENAIX Arduino Simulator and choose Blank editor with the Arduino Uno board.
  2. Delete the starter code, then paste the sketch from the code section.
  3. Click Libraries, search Servo and click + Add to project (the one by Michael Margolis). Without it you get Servo.h: No such file.
  4. Tap + Add and place an HC-SR04 ultrasonic sensor and a servo.
  5. Draw the wires exactly as in the connection table above.
  6. Press the green ▶ run button and open Serial. You should see ZEENAIX Smart Dustbin ready, then a distance reading twice a second.
  7. Tap the sensor and drag its distance below 20 cm. The servo arm swings to 90° and the Serial Monitor shows Hand detected: lid OPEN. Move it back above 20 cm and the lid closes 3 seconds later.

B. Build it for real (40 minutes)

  1. Test the circuit on the breadboard first: connect the HC-SR04 and the servo to the Uno as in the table, upload the code and check the servo moves when your hand is close.
  2. Plug the Uno into your computer, open the Arduino IDE, select Tools › Board › Arduino Uno and the right COM port, paste the code and click Upload. The Servo library is already built into the Arduino IDE.
  3. Fix the HC-SR04 on the front of the bin, just below the lid, with the two round "eyes" pointing outwards. Double-sided foam tape works well.
  4. Fix the servo at the back of the bin next to the hinge. Join the servo horn to the lid with a stiff wire, an ice-cream stick or a strip of plastic, so that turning the horn lifts the lid.
  5. Check the angles: if the lid does not open fully, change LID_OPEN (try 70 to 120). If it does not close fully, change LID_CLOSED. Upload again after each change.
  6. Power it from a USB power bank or a 5V phone charger so it works away from the computer.

Try it here: what the lid will do

Distance: 40 cm | Lid: CLOSED

Same rules as the sketch: closer than 20 cm opens the lid. It closes 3 seconds after your hand moves away.

How it works

Every 100 ms the Arduino sends a 10 microsecond pulse on Trig. The HC-SR04 answers with an Echo pulse that lasts as long as the sound takes to reach your hand and come back. Sound needs about 58 microseconds per centimetre for the round trip, so distance = echo time ÷ 58.

A servo turns to the angle set by the width of a pulse it receives 50 times a second: about 0.5 ms is 0° and 2.4 ms is 180°. The Servo library makes these pulses for you, so lid.write(90) is all you need. The code remembers the last time it saw a hand with millis(). When more than 3,000 ms have passed without a hand, it closes the lid. Because it never uses a long delay(), the sensor keeps checking all the time.

Tested: we compiled this sketch for the Arduino Uno (Servo library, zero warnings) and ran it in an AVR simulation with a hand model. With the hand at 80 cm it printed 79 cm and the servo stayed at 0°. When the hand moved to 10 cm it printed Hand detected: lid OPEN and the servo pulse changed to 1.47 ms (90°). After the hand left, the lid closed 3 seconds later, as designed.

Real-life uses of a touch-free lid

Kitchen bin

No more touching a dirty lid with food on your hands. Great for families and hostel kitchens.

Hospital and clinic waste

Touch-free bins cut the spread of germs. The same idea is used in medical waste bins.

Classroom science fair

A working smart dustbin is one of the most popular school and college exhibition projects. Add a cardboard bin and it looks finished.

Automatic pet feeder lid

Swap the bin for a food box: the lid opens when your pet comes close and closes after it eats.

Smart door or gate model

Use the same code to open a small gate for a toy car or a model parking barrier when something arrives.

Troubleshooting

  • Servo shakes or the Uno restarts: the servo can draw too much current from USB. Power the board from a good 5V 1A charger or add a 100 µF capacitor across the servo power wires.
  • Lid opens on its own: something else is closer than 20 cm, like a wall. Point the sensor away from walls or reduce OPEN_CM.
  • Lid never opens: check Trig is on pin 9 and Echo on pin 10, and that the sensor VCC goes to 5V. Open the Serial Monitor at 9600 baud to see the distance.
  • Lid does not open far enough: raise LID_OPEN or move the link closer to the servo centre.

Frequently asked questions

Which servo should I use?

An SG90 9 g micro servo is enough for a small, light lid. For a bigger bin use a stronger metal-gear servo like the MG996R and a separate 5V supply.

Can I change how long the lid stays open?

Yes. Change HOLD_MS at the top of the code. 3000 means 3 seconds; 5000 means 5 seconds.

Can it run on batteries?

Yes. A USB power bank is the easiest option. You can also use 4 AA cells (about 6V) on the Vin pin, but a 9V battery will run out quickly with a servo.

Do I need to buy parts to try this?

No. You can run the whole project in the free ZEENAIX simulator first, then buy only the parts you need when you are ready to build it.

Build it for real

Get the Arduino Uno, HC-SR04 sensor, SG90 servo, breadboard and jumper wires from the ZEENAIX store, with delivery across India.

▶ Copy code + open simulatorMore DIY projects