Smart dustbin with an ultrasonic sensor and servo on Arduino: design worksheet and code

A ₹990 design worksheet for a touch-free dustbin lid that opens when a hand comes close and closes after a few seconds, with the debounce logic and servo timing explained.

About the author · Editorial & verification standards

  • Parts₹990
  • Bench time3–4 h
  • LevelBeginner
  • ReviewedNov 2026

A ₹990 dustbin lid that opens on its own when a hand or a bag comes near. An HC-SR04 watches the front of the bin, an SG90 servo lifts the lid, and the sketch holds it open for three seconds after the last time something was seen, so it does not slam shut on a hand.

Scope: beginner. This is a design worksheet with illustrative costs. It has not been bench-tested by this site. The timing numbers below were computed by script. The lid weight and the hinge matter more than the code: a hobby servo only lifts a light lid. Keep the electronics away from the inside of the bin.

How it works

The sensor measures distance several times a second. To avoid a false trigger from one stray reading, the lid opens only after three readings in a row are under 20 cm. Once open, every reading under 25 cm restarts a three second timer, and the lid closes when the timer runs out. The two thresholds, 20 to open and 25 to stay open, are a small hysteresis band.

An SG90-type servo is commonly listed as turning about 60 degrees in 0.1 second at 4.8 V, so a 90 degree lift takes roughly 0.15 second with no load. Under load it is slower. Moving the lid in small steps gives a gentler, quieter motion than jumping straight to the target.

Bill of materials

PartQtyUnitTotal
Arduino Uno R3 (or compatible)1₹550₹550
HC-SR04 ultrasonic sensor1₹110₹110
SG90 micro servo1₹120₹120
Breadboard1₹60₹60
Jumper wires1₹50₹50
Hinge, tape and a light lid or cardboard1₹100₹100
Parts total₹990

Scroll the table sideways to see every column.

Prices checked Nov 2026. Shipping and tools are not included.

Prices are illustrative figures for Indian sellers in November 2026, before shipping, and have not been checked against current listings.

The numbers

StepValueMeaning
Open thresholdbelow 20 cmNeeds 3 readings in a row
Stay-open thresholdbelow 25 cmRestarts the timer
Reading interval60 msAbout 16 readings a second
Time to confirmabout 0.18 s3 readings × 60 ms
Hold time3,000 msAfter the last detection
Lift speed in the sketch5° every 15 ms90° in about 0.27 s
These are design choices, not measurements from a built bin.

Servo wiring matters. A servo that stalls against a heavy lid can draw several hundred milliamps and reset the Uno. If the board resets when the lid moves, power the servo from its own 5 V supply and join the grounds.

Power and wiring

PartConnects toNote
HC-SR04 VCC and GND5V and GND
HC-SR04 TRIG / ECHOD9 / D10
Servo signalD6Orange or yellow wire
Servo power and ground5V and GND, or a separate 5 V supplyJoin grounds if separate
Mount the sensor on the front rim, pointing forward and slightly up, so it sees hands and not the floor.

The sketch

// Smart dustbin: Uno + HC-SR04 + SG90 servo
// Design worksheet: angles depend on your hinge.

#include <Servo.h>

const int TRIG = 9, ECHO = 10, SERVO_PIN = 6;
const int CLOSED = 0, OPENED = 90;          // degrees
const float OPEN_CM = 20, KEEP_CM = 25;
const unsigned long HOLD_MS = 3000UL;

Servo lid;
int angle = CLOSED;
bool isOpen = false;
int seen = 0;
unsigned long lastSeen = 0;

float readCm() {
  digitalWrite(TRIG, LOW);  delayMicroseconds(2);
  digitalWrite(TRIG, HIGH); delayMicroseconds(10);
  digitalWrite(TRIG, LOW);
  unsigned long t = pulseIn(ECHO, HIGH, 25000UL);
  return t == 0 ? 999 : t / 58.3;
}

void moveTo(int target) {
  while (angle != target) {
    angle += (target > angle) ? 5 : -5;
    lid.write(angle);
    delay(15);
  }
}

void setup() {
  pinMode(TRIG, OUTPUT); pinMode(ECHO, INPUT);
  lid.attach(SERVO_PIN);
  lid.write(CLOSED);
}

void loop() {
  float d = readCm();
  unsigned long now = millis();
  if (!isOpen) {
    seen = (d < OPEN_CM) ? seen + 1 : 0;
    if (seen >= 3) { moveTo(OPENED); isOpen = true; lastSeen = now; seen = 0; }
  } else {
    if (d < KEEP_CM) lastSeen = now;
    if (now - lastSeen >= HOLD_MS) { moveTo(CLOSED); isOpen = false; }
  }
  delay(60);
}

The moveTo loop blocks while the lid moves, which is fine here because nothing else needs attention. If the lid does not reach full travel, change OPENED; do not push the servo against a mechanical stop for long. The sketch has been proofread but not compiled on this site.

Test plan

  1. Servo alone: run the servo with no lid and confirm it sweeps 0 to 90 degrees smoothly.
  2. Sensor alone: print readings and check a hand at 15 cm reads under 20.
  3. Lid weight: attach the lid and see whether the servo lifts it; if not, lighten the lid or move the linkage closer to the hinge.
  4. False trigger: walk past the bin at 1 m and make sure it stays shut.
  5. Hold timing: time how long it stays open after you step away.
  6. Hand safety: confirm the lid cannot trap fingers with this servo; a weak servo should only lift a light lid.

What usually goes wrong

SymptomLikely causeFix
Opens when nobody is nearSensor sees the floor or a wallAim the sensor higher and lower the open distance
Lid does not liftLid too heavy for the servoLighten the lid or move the horn closer to the hinge
Uno resets as the lid movesServo current dipUse a separate 5 V supply for the servo
Servo buzzes at restLid pushing against the stopReduce the OPENED angle
Closes while a hand is still nearKEEP_CM too smallRaise KEEP_CM or the hold time

Upgrades

  • Smooth motion: ease the lid with smaller steps near the ends.
  • Full-bin alert: add a second sensor inside to flag when the bin is nearly full.
  • Battery power: add a battery pack and a switch.
  • Foot pedal: add a pedal switch as a backup trigger.

Cost and parts notes

The ₹990 total is an illustration and does not include shipping. Use the cost calculator to compare sellers.

Common questions

A single reading can be wrong. Requiring three agreeing readings removes most false triggers.

Opening at 20 cm and staying open at 25 cm stops the lid fluttering at the edge of the range.

Probably not. It suits a card or very light plastic lid; a larger bin needs a stronger servo or a linear actuator.

It avoids touching the lid, which is the point, but it still needs regular cleaning.

Last reviewed: November 2026. This is a design worksheet with illustrative costs. It has not been bench-tested by this site; library names, pins and limits must be checked against your board and the current library versions, and measurements from your build are welcome.

More builds

Next builds to try

Free: the final-year project pack

A shortlist with BOMs, a report outline that matches the usual chapter scheme, a costing sheet and 40 viva questions. Built for the February–April submission season.

The Build Note

One short email when something is worth your bench time.

A new build sheet, a part price that moved, or a mistake worth avoiding. No daily drip, no recycled news.

One short email when a build or a price is worth your time. Unsubscribe in one click.