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 itemsNeed 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
| Sensor | Mounted | Measures | Decision |
|---|---|---|---|
| Hand sensor | Front of the bin, facing out | Distance to a hand | Under 20 cm: open the lid |
| Level sensor | Under the lid, facing down | Gap to the top of the rubbish | Converted 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
| Part | Part pin | Arduino Uno |
|---|---|---|
| Hand HC-SR04 | VCC / GND | 5V / GND |
| Hand HC-SR04 | TRIG / ECHO | D2 / D3 |
| Level HC-SR04 | VCC / GND | 5V / GND |
| Level HC-SR04 | TRIG / ECHO | D4 / D5 |
| SG90 servo | Signal / VCC / GND | D9 / 5V / GND |
| Green LED | Anode via 220 Ω | D6 |
| Red LED | Anode 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.
Key Parts of the Code Explained
| Code | What 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_MS | Keeps the lid open while the hand is present plus 3 s |
lidAngle += (lidTarget > lidAngle) ? 2 : -2 | Moves 2° every 12 ms for a smooth, quiet motion |
if (lidAngle == CLOSED_ANGLE) | Measures the fill level only with the lid shut |
(now / 400) % 2 | Flashes the red LED without delay() |
Upload and Test
| Test | Expected |
|---|---|
| Power on | Smart dustbin ready in Serial, green LED on, lid closed |
| Hand at 10 cm | Lid opening, lid rises smoothly in about 0.6 s |
| Hand kept there | Lid stays open |
| Hand removed | Lid closes about 3 s later |
| Fill the bin to within 4 cm of the sensor | Fill level: 86 %, red LED flashes |
Open the Serial Monitor at 9600 baud to see the fill level each time it changes.
Troubleshooting
| Problem | Likely cause | Fix |
|---|---|---|
| Lid opens by itself | Hand sensor sees a wall or the floor | Angle it slightly upward; lower HAND_CM |
| Lid opens and closes repeatedly | Lid swings into the hand sensor's view | Mount the hand sensor below the lid edge |
| Fill level jumps around | Uneven rubbish surface | Average several readings or mount the sensor centrally |
| Servo jitters or the Uno resets | Not enough current | Use a 5 V 2 A supply, not a laptop USB port |
| Servo strains at the end | Angle past the lid's travel | Reduce OPEN_ANGLE |
| Always 999 / no reading | Echo absorbed by soft rubbish, or wiring | Check TRIG/ECHO; soft materials reflect poorly |
Make It Your Own
- Buzzer: chirp once when the bin becomes full.
- LCD or OLED: show the fill percentage.
- Battery power: add a power switch and sleep between readings.
- IoT bin: move to an ESP32 and send a phone notification when it is full.
- 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.



