How to Build a Smart Dustbin with an Ultrasonic Sensor
Share
A touchless smart dustbin works by having an ultrasonic sensor ping continuously and measure how long the echo takes to bounce back, and when that measured distance drops below a set threshold — meaning a hand or object is close — the microcontroller swings a servo motor to lift the lid, then closes it again automatically once the object is gone for a few seconds. The whole loop runs on an Arduino Uno reading one sensor and driving one motor, which makes it one of the more approachable sensor-plus-actuator builds for anyone starting out with electronics.
How does an ultrasonic sensor detect a hand nearby?
An HC-SR04 ultrasonic sensor has two parts: a transmitter that sends out a short burst of sound above human hearing, and a receiver that listens for it to bounce back off whatever is in front of it. The microcontroller measures the time between sending the pulse and receiving the echo, and because sound travels through air at a known speed, that time converts directly into a distance. When your hand moves within the configured range — typically a short reach above the bin, not across the room — the reading crosses the trigger threshold and the microcontroller treats it as "object present." The sensor has no idea what it detected; it only reports distance, so the logic that turns "distance is small" into "open the lid" lives entirely in the firmware.
What parts does a touchless smart dustbin need?
Electronically, this build needs four things: a microcontroller to run the logic, an ultrasonic sensor to measure distance, a servo motor to move the lid, and a way to power the servo without starving the board. Compoden's build assistant priced all of these out for this exact project — see the parts table further down. One thing worth being upfront about: Compoden sells components, not dustbins. The bin itself, with a lid that can hinge open under servo power, is a physical container you will need to source separately — any household plastic bin with a hinged lid works fine as the body you mount the electronics onto.
How do you mount the servo to actually open a lid?
This is the part that firmware and a parts list can't fully solve for you. A servo motor only rotates a horn through an arc — it does not open a lid by itself. You need a simple mechanical link between the servo horn and the lid's hinge: a stiff wire or plastic arm bolted or hot-glued to the servo horn on one end and to the lid (or a lever arm off the hinge pin) on the other, so that when the servo turns, the arm pushes or pulls the lid open. Getting the geometry right — arm length, mounting angle, how far the servo needs to rotate to fully open the lid — is a hands-on fitting step you do once the servo and bin are both in front of you, and it's exactly why the generated firmware below exposes an open angle and a closed angle as tunable values rather than hard-coding them.
How do you avoid the lid opening for every passerby?
The trigger distance is the whole trick here. If it's set too wide, the sensor picks up anyone walking past the bin across the room and the lid flaps open constantly. The fix is to tune the threshold tight — close enough that only a hand deliberately held near the sensor triggers it, not general foot traffic a meter or two away. In practice that means testing with the serial monitor open, watching the live distance readings, and setting the trigger value just below where your hand naturally sits when you reach toward the bin. A short debounce window on top of that threshold — requiring the close reading to hold for a moment before acting — also filters out momentary reflections or a hand sweeping past rather than reaching in.
Watch it built live
We gave Compoden's storefront assistant this exact prompt:
"I want to build a touchless smart dustbin that opens its lid automatically when I bring my hand near it, using an ultrasonic sensor and a servo"
The assistant put together a 7-part tray and explained the reasoning in its reply:
"This build creates a smart dustbin that opens its lid without you touching it. When you bring your hand close to the bin, an HC-SR04 Ultrasonic Sensor measures the distance. This information tells the Arduino Uno R3 CH340G microcontroller to move a SG90 9g Micro Servo Motor, which then opens the dustbin lid."
It also flagged a real failure mode worth knowing about before you build:
"...the ultrasonic sensor might pick up reflections from the dustbin's own walls or objects near the bin, causing it to open unexpectedly. You might need to adjust the sensor's position or the code."
That's the same threshold-tuning problem covered above — the assistant is describing the identical failure mode you'd hit if the trigger distance is set too loosely, or the sensor is angled so it echoes off the bin's own rim.
Watch the firmware get generated
We took the same project to Compoden's VoltIQ PWA and asked for working firmware:
"I'm building a touchless smart dustbin with an Arduino Uno, an HC-SR04 ultrasonic sensor, and a servo motor. Write firmware that opens the lid with the servo when an object is detected within a short distance, and closes it again after a few seconds."
The generated sketch pins the HC-SR04's trigger to D9 and echo to D10, and the servo signal to D6, with a trigger distance constant set at 30 cm as a starting point. The build log summarized its own approach directly:
"Standard pinout: Trig → D9, Echo → D10, Servo signal → D6. The servo angle depends on your linkage — tune OPEN_ANGLE and CLOSED_ANGLE for your mechanism. The sketch uses a simple state machine with hysteresis to avoid lid flutter."
"Flash this to your Uno, open Serial Monitor at 115200 baud, and wave your hand over the sensor. Tune OPEN_ANGLE / CLOSED_ANGLE to match your linkage, and adjust TRIG_DIST_CM if 30 cm feels too sensitive or too sluggish. The hysteresis band and 200 ms debounce prevent the lid from flapping when you hover right at the threshold."
That hysteresis band and debounce are the firmware-side version of the same anti-false-trigger tuning covered above — the code holds its state across small distance fluctuations instead of reacting to every reading, so the lid doesn't flap when your hand hovers right at the trigger boundary.
Get everything in this build
| Part | Role | Price | Buy |
|---|---|---|---|
| Arduino Uno R3 CH340G | Microcontroller | Rs.280 | Add to cart |
| HC-SR04 Ultrasonic Sensor | Proximity sensor | Rs.85 | Add to cart |
| SG90 9g Micro Servo Motor | Lid actuator | Rs.120 | Add to cart |
| 5V 2A Micro-USB Power Adapter | Board power | Rs.140 | Add to cart |
| Male-to-Male Breadboard Jumper Wires (20 cm, 24 AWG) | Wiring | Rs.40 | Add to cart |
| SYB-170 Mini Breadboard | Prototyping board | Rs.20 | Add to cart |
| Bluesky Mini 5V 3A UBEC | Dedicated servo power | Rs.240 | Add to cart |
| Total (7 parts) | Rs.925 | Add all to cart | |
The bin itself is not sold by Compoden — bring your own household bin with a hinged lid, or a small enclosure you're comfortable drilling and mounting hardware into.
Built and Backed by Compoden. Every part above was matched, priced, and stock-checked by Compoden's own AI build assistant against its live catalog, and the firmware was generated by the same underlying system through the VoltIQ PWA. Compoden ships components across India, and the same tray that assembled this build is reproducible on the storefront by describing your own version of the project.
FAQ
Does the servo need its own power supply?
Yes. A servo pulling load — especially at the moment it starts moving — can draw current spikes the Arduino Uno's onboard regulator isn't built to supply. Powering the servo from a separate 5V source, like the UBEC used in this tray, and tying the grounds together keeps the board from resetting or the sensor readings from glitching.
Can I use a bigger dustbin or a heavier lid?
You can, but the SG90 micro servo used here is sized for a light lid. A heavier lid needs a servo with more torque, and possibly a different mechanical linkage to keep the lift smooth. Check the servo's torque rating against your lid's actual weight and the length of your linkage arm before committing to a size.
Why does the lid open when I'm nowhere near the bin?
Almost always this is the trigger distance set too wide, or the sensor picking up an echo off the bin's own rim or a nearby wall. Narrow the trigger distance, reposition or angle the sensor so it isn't facing a reflective surface close by, and add a short debounce so a single stray reading can't flip the lid.
Do I need to solder anything?
No. Every part in this tray connects with jumper wires on a breadboard — no soldering required to get the circuit working. Soldering only becomes relevant if you want a permanent, non-breadboard build later.