11:18:52 AM NPT Arduino, Robotics & IoT tutorials for Nepal
Beginner

Smart Dustbin with Arduino, Ultrasonic Sensor and Servo

Build an automatic smart dustbin with Arduino, HC-SR04 and a servo. Wiring table, full code, fullness alert, testing and troubleshooting.

On this page
Smart Dustbin with Arduino, Ultrasonic Sensor and Servo

A smart dustbin opens its lid by itself when your hand or a piece of waste comes near, then closes again. It is a clean, hygienic idea, and it is one of the best science exhibition and school-level projects because the audience understands it in two seconds. With a second sensor inside the bin, it can also tell you when the bin is full.

This guide builds the version with a hand sensor on the front, a servo to lift the lid and an optional fullness indicator.

What you need

Scroll sideways to see all columns.

Item Qty Notes
Arduino Uno or classic 5 V Nano 1
HC-SR04 ultrasonic sensor (hand sensor) 1 Mounted on the front of the bin
HC-SR04 ultrasonic sensor (fullness sensor) 1 Mounted under the lid facing down; can be disabled in code
SG90 micro servo (or MG90S for a heavier lid) 1 Moves the lid
LEDs, red and green, with 220 Ω resistors 1 each Full and ready indicators
Electrolytic capacitor 470 uF 1 Across the servo supply
Small dustbin or plastic box 1 Light lid works best
5 V supply (USB power bank or 5 V adapter) 1 For the servo, rated for its peak current. Power the Arduino separately through USB
Cardboard, tape, hot glue, or a small bracket To mount the servo and arm
Jumper wires 1 set

Connection table

A servo can draw several hundred milliamps when it starts to move or when it stalls against a heavy lid. If it browns out the Arduino, power the servo from its own 5 V source and join the grounds.

Scroll sideways to see all columns.

Pin or terminal Connects to
Hand sensor VCC / GND 5 V / GND
Hand sensor Trig Arduino D9
Hand sensor Echo Arduino D10
Fullness sensor VCC / GND 5 V / GND
Fullness sensor Trig Arduino D7
Fullness sensor Echo Arduino D8
Servo signal Arduino D6
Servo VCC / GND 5 V supply / GND, with 470 uF capacitor across them
Green LED (through 220 Ω) Arduino D4, other end to GND
Red LED (through 220 Ω) Arduino D5, other end to GND
Arduino GND Common with servo supply GND

How it works

The 5 V HC-SR04 has a nominal range of 2–400 cm. A missing echo is treated as out of range. Keep hands clear of the lid’s closing path.

The hand sensor looks forward and measures distance many times each second. When the reading falls below the trigger distance (for example 20 cm), the Arduino rotates the servo to open the lid. It then holds the lid open for a few seconds, and closes it when nobody is near. To stop the lid from opening and closing repeatedly on noisy readings, the code needs three safeguards:

  1. Median of a few readings to remove spikes.
  2. Hold time (for example 3 seconds) after the hand leaves.
  3. A hysteresis margin: open at 20 cm, but keep it open until the reading is above 30 cm.

For fullness, the second sensor sits under the lid facing the waste. The distance from the lid to the waste is largest when the bin is empty and gets smaller as it fills. If it falls below a set value (say 10 cm) the red LED turns on. Measure the fullness only while the lid is closed, so your own hand is not mistaken for waste.

Step-by-step build

  1. Fix the servo near the lid hinge on the back of the bin. Attach a stiff cardboard or plastic arm to the servo horn and connect it to the lid with a small link or glue.
  2. Check the servo range by hand: 0 degrees should be closed and about 90 to 100 degrees fully open. Adjust the angles in the code for your lid.
  3. Mount the hand sensor on the front, facing outward at hand height.
  4. Mount the fullness sensor on the underside of the lid, facing down. If you omit it, set USE_FULL_SENSOR to false.
  5. Wire the circuit with the connection table, using a breadboard first.
  6. Upload the code and test with the lid disconnected, then attach the servo arm.
  7. Hide the wiring and electronics in the back with tape or a small box. Keep them away from moisture.

Complete code

sketch-1.inoDownload code
// Smart dustbin - not compiled yet
#include <Servo.h>

const int HAND_TRIG = 9, HAND_ECHO = 10;
const int FULL_TRIG = 7, FULL_ECHO = 8;
const int SERVO_PIN = 6, LED_READY = 4, LED_FULL = 5;

const int OPEN_CM   = 20;     // open when a hand is closer than this
const int CLOSE_CM  = 30;     // keep open until farther than this
const bool USE_FULL_SENSOR = true; // set false if the second sensor is omitted
const int FULL_CM   = 10;     // bin counted full if waste is closer than this
const unsigned long HOLD_MS = 3000;

const int ANGLE_CLOSED = 0;
const int ANGLE_OPEN   = 95;  // tune for your lid

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

long readCm(int trig, int echo) {
  digitalWrite(trig, LOW);  delayMicroseconds(2);
  digitalWrite(trig, HIGH); delayMicroseconds(10);
  digitalWrite(trig, LOW);
  unsigned long us = pulseIn(echo, HIGH, 25000UL);
  delay(60);                     // prevent old echoes reaching the next ping
  if (us == 0 || us > 23200UL) return 401; // no usable echo
  return us / 58;
}

// median of 3
long median3(int trig, int echo) {
  long a = readCm(trig, echo), b = readCm(trig, echo), c = readCm(trig, echo);
  if ((a >= b && a <= c) || (a <= b && a >= c)) return a;
  if ((b >= a && b <= c) || (b <= a && b >= c)) return b;
  return c;
}

void setup() {
  pinMode(HAND_TRIG, OUTPUT); pinMode(HAND_ECHO, INPUT);
  pinMode(FULL_TRIG, OUTPUT); pinMode(FULL_ECHO, INPUT);
  pinMode(LED_READY, OUTPUT); pinMode(LED_FULL, OUTPUT);
  lid.attach(SERVO_PIN);
  lid.write(ANGLE_CLOSED);
  digitalWrite(LED_READY, HIGH);
}

void loop() {
  long hand = median3(HAND_TRIG, HAND_ECHO);

  if (!isOpen && hand < OPEN_CM) {
    lid.write(ANGLE_OPEN);
    isOpen = true;
    lastSeen = millis();
  }
  if (isOpen) {
    if (hand < CLOSE_CM) lastSeen = millis();       // someone still near
    if (millis() - lastSeen > HOLD_MS) {
      lid.write(ANGLE_CLOSED);
      isOpen = false;
      delay(600);                                    // let the lid settle
    }
  }

  // Fullness check only when the lid is closed
  if (USE_FULL_SENSOR && !isOpen) {
    long waste = median3(FULL_TRIG, FULL_ECHO);
    digitalWrite(LED_FULL, waste < FULL_CM ? HIGH : LOW);
  }
  delay(100);
}

Testing

  • Hand sensor. Print hand to the Serial Monitor and check the value at 10, 20 and 30 cm.
  • Lid angles. Move the servo manually to find the real open and closed angles.
  • Hold time. Wave your hand and see that the lid stays open for 3 seconds after you leave.
  • Fullness. Drop paper into the bin and confirm the red LED turns on when it reaches the set level. Use a ruler to calibrate FULL_CM.
  • Trial run. Test ten consecutive openings and look for random opening when no one is there.

Troubleshooting

Scroll sideways to see all columns.

Symptom Likely cause Fix
Lid opens by itself Sensor sees a wall or a bin edge Aim the sensor outward; lower OPEN_CM
Servo twitches or Arduino resets Servo current spike Add the 470 uF capacitor; use a separate 5 V supply
Lid does not open fully Arm geometry or angle too small Increase ANGLE_OPEN; lengthen the arm
Lid slams or shakes Servo moving at full speed Step the angle in small increments with short delays
Red LED always on Fullness sensor sees the lid or the wall Aim it down at the waste; check the distance threshold
Lid closes on your hand Hold time too short Increase HOLD_MS
Sensor ignores a hand Fabric or hand at an angle Move the hand straight toward the sensor

Make it stand out

  • Count how many times the lid opens and show the total on an I2C LCD.
  • Add a buzzer that beeps when the bin is full.
  • Send a fullness alert using an ESP32 and a Telegram or MQTT message.
  • Make a two-bin version for dry and wet waste, with two hand sensors.

FAQ

Can I use an IR sensor instead of the ultrasonic one?

Yes, a reflective IR sensor works for a short range of a few centimetres, but it is more sensitive to sunlight and surface colour.

Which servo should I use for a heavy lid?

An SG90 is fine for light cardboard and plastic. For heavier lids, use a metal-gear servo such as MG90S or MG996R with a proper 5 V supply that can give at least 1 A.

Will it work outdoors?

Rain, dust and sunlight make life hard for sensors, so protect the electronics and keep them inside the bin.

Can I power it from a battery?

Yes. A power bank is easiest. Remember that continuous sensing drains the battery faster, so sleep or add a longer delay between readings if you need long life.

Related tutorials

See all Project Ideas →