Skip to content

Uno · Build guide

Arduino Smart Dustbin with Automatic Lid and Fill-Level Alert

A touchless dustbin that opens its lid when you wave a hand and closes it gently a few seconds later, built with an Arduino Uno, an SG90 servo and two HC-SR04 sensors. The second sensor measures how full the bin is and flashes a red LED when it needs emptying.

Arduino Smart Dustbin with Automatic Lid and Fill-Level Alert
Difficulty
Beginner
Read time
14 min
Board
Uno
Author
Shantanu Kumar Mohanty
Published
Nov 20, 2026
Views
0

What You Will Build

Quick answer: mount one HC-SR04 on the front of the bin (TRIG D2, ECHO D3), a second under the lid pointing down (TRIG D4, ECHO D5), and an SG90 on D9 that lifts the lid. When a hand comes within 20 cm the lid opens smoothly, stays open while you are there plus 3 s, then closes. A green LED means there is space; a flashing red LED means the bin is over 85 % full.

It is one of the most popular first projects, and this version fixes the things that make simple versions annoying:

  • Smooth motion: the servo moves 2° at a time instead of slamming the lid
  • Hold timer: the lid stays open as long as the hand is there, then waits 3 s
  • Two sensors, no crosstalk: they are pinged one after the other, never together
  • Fill level measured only when the lid is closed, so the sensor sees the rubbish, not the ceiling

Components

8 items
NameQtyLink
Arduino Uno R3×1—
HC-SR04 ultrasonic sensor×2—
SG90 or MG90S servo (MG90S for a heavier lid)×1—
Green LED + 220 Ω resistor×1—
Red LED + 220 Ω resistor×1—
Dustbin with a hinged lid (8 to 15 L)×1—
5 V 2 A USB power supply×1—
Jumper wires, hot glue or screws, a thin rod or string for the lid linkage×1—

Need these parts? Get this build sheet by email

Parts list with buy links, plus new builds as they're published. By subscribing you agree to our Privacy Policy. Unsubscribe anytime.

⚠️ Warning

Keep electronics away from wet rubbish: mount the Uno and wiring outside the bin or in a small box, and use a lid-mounted sensor guard. Do not let the servo push against a stuck lid for long; a stalled SG90 overheats. Use a lid light enough for the servo to lift easily.

How It Works

Measuring Distance with Echoes

Each HC-SR04 sends a 40 kHz ultrasonic burst when its TRIG pin gets a 10 µs pulse, then holds ECHO HIGH until the echo returns. The pulse length in microseconds divided by 58 gives the distance in centimetres:

distance_cm = echo_us / 58

The 58 comes from the speed of sound (about 343 m/s at 20 °C) and the fact that the sound travels there and back.

Two Jobs for Two Sensors

SensorMountedMeasuresDecision
Hand sensorFront of the bin, facing outDistance to a handUnder 20 cm: open the lid
Level sensorUnder the lid, facing downGap to the top of the rubbishConverted to fill percentage

The fill percentage uses the bin depth: an empty 30 cm bin gives a 30 cm gap (0 %), a gap of 4.5 cm means 85 % full.

fill % = (BIN_DEPTH_CM - gap) / BIN_DEPTH_CM × 100

Why Ping One at a Time

If both sensors fire together, one can hear the other's echo and report a false distance. The sketch reads the hand sensor, waits 10 ms, then reads the level sensor, every 100 ms. It only trusts the level reading when the lid is fully closed, because an open lid points the sensor at the ceiling.

Try It in the Simulator First

Open the circuit below. Click the front HC-SR04 and set its distance to 10 cm: the lid servo swings open smoothly. Set it back to 60 cm and it closes after 3 s. Set the level sensor to 3 cm and the red LED starts flashing to say the bin is full.

Try it in the simulator

Wiring

PartPart pinArduino Uno
Hand HC-SR04VCC / GND5V / GND
Hand HC-SR04TRIG / ECHOD2 / D3
Level HC-SR04VCC / GND5V / GND
Level HC-SR04TRIG / ECHOD4 / D5
SG90 servoSignal / VCC / GNDD9 / 5V / GND
Green LEDAnode via 220 ΩD6
Red LEDAnode via 220 ΩD7

Fitting the Servo to the Lid

Glue or screw the servo to the back of the bin beside the hinge, with the horn pointing forward under the lid. At 0° the horn lies flat and the lid is closed; at 100° the horn has pushed the lid up. Find the right angles before gluing anything: upload the sketch, then adjust OPEN_ANGLE until the lid opens fully without the servo straining.

The Code

Only the built-in Servo library is needed. Set BIN_DEPTH_CM to the distance from the level sensor to the bottom of your empty bin.

C++

Key Parts of the Code Explained

CodeWhat it does
readCm()Sends the 10 µs trigger and converts the echo time to cm; returns 999 when nothing echoes
pulseIn(echo, HIGH, 25000UL)Waits at most 25 ms so a missing echo cannot freeze the sketch
lastHandSeen and HOLD_MSKeeps the lid open while the hand is present plus 3 s
lidAngle += (lidTarget > lidAngle) ? 2 : -2Moves 2° every 12 ms for a smooth, quiet motion
if (lidAngle == CLOSED_ANGLE)Measures the fill level only with the lid shut
(now / 400) % 2Flashes the red LED without delay()

Upload and Test

TestExpected
Power onSmart dustbin ready in Serial, green LED on, lid closed
Hand at 10 cmLid opening, lid rises smoothly in about 0.6 s
Hand kept thereLid stays open
Hand removedLid closes about 3 s later
Fill the bin to within 4 cm of the sensorFill level: 86 %, red LED flashes

Open the Serial Monitor at 9600 baud to see the fill level each time it changes.

Troubleshooting

ProblemLikely causeFix
Lid opens by itselfHand sensor sees a wall or the floorAngle it slightly upward; lower HAND_CM
Lid opens and closes repeatedlyLid swings into the hand sensor's viewMount the hand sensor below the lid edge
Fill level jumps aroundUneven rubbish surfaceAverage several readings or mount the sensor centrally
Servo jitters or the Uno resetsNot enough currentUse a 5 V 2 A supply, not a laptop USB port
Servo strains at the endAngle past the lid's travelReduce OPEN_ANGLE
Always 999 / no readingEcho absorbed by soft rubbish, or wiringCheck TRIG/ECHO; soft materials reflect poorly

Make It Your Own

  1. Buzzer: chirp once when the bin becomes full.
  2. LCD or OLED: show the fill percentage.
  3. Battery power: add a power switch and sleep between readings.
  4. IoT bin: move to an ESP32 and send a phone notification when it is full.
  5. Foot sensor: move the hand sensor low to open with a foot instead.

FAQ

How does a smart dustbin know a hand is there?

An HC-SR04 on the front measures the distance to whatever is in front of it. When that distance drops below 20 cm the sketch treats it as a hand and opens the lid.

Why use two ultrasonic sensors?

One detects your hand; the other, under the lid, measures how full the bin is. A single sensor cannot do both jobs.

Is an SG90 strong enough?

For a light plastic lid, yes. For a heavier or larger lid use an MG90S or MG996R with its own 5 V supply and a common ground.

Why does the lid open on its own?

The hand sensor is seeing something within 20 cm, often the floor, a wall or the open lid. Re-aim it or lower HAND_CM.

Can I run it on batteries?

Yes, with four AA cells or a USB power bank, but the HC-SR04s and servo draw power constantly, so expect days rather than months.

What You Learned

You used ultrasonic ranging for two different jobs, moved a servo smoothly without delay(), and avoided sensor crosstalk. For a deeper look at the sensor see the HC-SR04 guide, and for the same sensor driving a buzzer see the HC-SR04 parking alert.

Topicsarduino-unohc-sr04ultrasonic-sensorservosg90smart-dustbinbeginnermillis

Questions & Answers