# 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.

Canonical: https://roboticsengineering.in/projects/smart-dustbin-ultrasonic-servo-arduino/ | Published: 2026-11-04 | Updated: 2026-11-04

**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

| Part | Qty | Unit | Total |
| --- | --- | --- | --- |
| Arduino Uno R3 (or compatible) | 1 | ₹550 | ₹550 |
| HC-SR04 ultrasonic sensor | 1 | ₹110 | ₹110 |
| SG90 micro servo | 1 | ₹120 | ₹120 |
| Breadboard | 1 | ₹60 | ₹60 |
| Jumper wires | 1 | ₹50 | ₹50 |
| Hinge, tape and a light lid or cardboard | 1 | ₹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

| Step | Value | Meaning |
| --- | --- | --- |
| Open threshold | below 20 cm | Needs 3 readings in a row |
| Stay-open threshold | below 25 cm | Restarts the timer |
| Reading interval | 60 ms | About 16 readings a second |
| Time to confirm | about 0.18 s | 3 readings × 60 ms |
| Hold time | 3,000 ms | After the last detection |
| Lift speed in the sketch | 5° every 15 ms | 90° 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

| Part | Connects to | Note |
| --- | --- | --- |
| HC-SR04 VCC and GND | 5V and GND |  |
| HC-SR04 TRIG / ECHO | D9 / D10 |  |
| Servo signal | D6 | Orange or yellow wire |
| Servo power and ground | 5V and GND, or a separate 5 V supply | Join 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

| Symptom | Likely cause | Fix |
| --- | --- | --- |
| Opens when nobody is near | Sensor sees the floor or a wall | Aim the sensor higher and lower the open distance |
| Lid does not lift | Lid too heavy for the servo | Lighten the lid or move the horn closer to the hinge |
| Uno resets as the lid moves | Servo current dip | Use a separate 5 V supply for the servo |
| Servo buzzes at rest | Lid pushing against the stop | Reduce the OPENED angle |
| Closes while a hand is still near | KEEP_CM too small | Raise 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](https://roboticsengineering.in/robotics-project-cost-calculator/) to compare sellers.

## Common questions

**Why three readings in a row?**

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

**Why two distance thresholds?**

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

**Will an SG90 lift a normal kitchen bin lid?**

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

**Is it hygienic?**

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.
