Build it step by step
0 of 5 doneFix the sensor
Mount the HC-SR04 on the front of the lid, facing up and out, so a hand above the bin is seen.
Fix the servo
Mount the servo at the hinge so its horn pushes the lid up. Tie the horn to the lid with stiff wire or a 3D-printed arm.
Wire the sensor
VCC, GND, TRIG to D9, ECHO to D10.
Wire the servo
Signal to D6. Power it from the 4 × AA pack with the 100 µF capacitor across it, and join the pack's minus to Uno GND.
Upload and tune
Upload, open the serial monitor and wave your hand. Change OPEN until the lid opens fully without straining.
How do you build a touchless smart dustbin?
An ultrasonic sensor sees a hand over the bin and a servo lifts the lid, then closes it after three seconds. You need Arduino Uno R3, HC-SR04 Ultrasonic Sensor, MG90S metal gear micro servo, Electrolytic Capacitor 100 µF, Battery holder 4 × AA with switch, AA alkaline battery × 4 and Jumper wires male to female (40 pcs). It takes 5 steps and about 60 minutes.
- Level
- Beginner
- Time
- About 60 min
- Parts
- 7
Wiring
| From | To | Why |
|---|---|---|
| HC-SR04 VCC / GND | Uno 5V / GND | Sensor power |
| HC-SR04 TRIG | Uno D9 | Start a measurement |
| HC-SR04 ECHO | Uno D10 | Echo pulse width = distance |
| Servo signal (orange) | Uno D6 | Lid angle |
| Servo red / brown | 4 × AA pack + / − | Servo power, separate from the Uno |
| 4 × AA − | Uno GND | Shared ground |
| 100 µF capacitor | Across servo + and − | Smooths current spikes |
Code
Arduino IDE, board "Arduino Uno". Servo library is built in.
Runs on a virtual Arduino Uno in your browser, with sliders for the sensors. Happy with it? Verify and upload to your real board from the same page (Chrome or Edge on a computer).
#include <Servo.h>
const int TRIG = 9, ECHO = 10;
const int OPEN = 100, CLOSED = 0; // adjust for your lid hinge
Servo lid;
unsigned long openedAt = 0;
bool isOpen = false;
long distanceCm() {
digitalWrite(TRIG, LOW); delayMicroseconds(2);
digitalWrite(TRIG, HIGH); delayMicroseconds(10);
digitalWrite(TRIG, LOW);
long us = pulseIn(ECHO, HIGH, 30000); // time out after 30 ms (about 5 m)
return us ? us / 58 : 999;
}
void setup() {
pinMode(TRIG, OUTPUT); pinMode(ECHO, INPUT);
lid.attach(6); lid.write(CLOSED);
Serial.begin(9600);
}
void loop() {
long d = distanceCm();
Serial.println(d);
if (d < 20) { lid.write(OPEN); isOpen = true; openedAt = millis(); }
if (isOpen && millis() - openedAt > 3000) { lid.write(CLOSED); isOpen = false; }
delay(100);
}What the serial monitor should show
Open the serial monitor at the baud rate in the code. This is what our simulator prints for a virtual Arduino Uno with its sensors at their default values. Your numbers will follow your real readings, but the format should match.
80
Test checklist
What you should see at each check. If a reading is far off, stop and use the troubleshooting list.
| Check | Expected |
|---|---|
| No hand | Serial shows the distance to the ceiling or 999; lid stays closed |
| Hand at 10 cm | Lid opens within about 0.2 s |
| Hand removed | Lid closes about 3 s later |
| Servo current while moving | 150 to 400 mA from the AA pack |
If it does not work
The lid opens by itself
The sensor sees the bin rim or a wall. Tilt it outwards or lower the 20 cm limit.
The servo buzzes when closed
CLOSED pushes past the stop. Raise it from 0 to 5 or 10.
The Uno resets when the lid moves
The servo is powered from the Uno. Use the separate AA pack with a shared ground.
Distance always 999
TRIG and ECHO swapped, or ECHO not on D10.
Still stuck? Find the fault step by step with your multimeter →
Common questions
What parts do I need for the touchless smart dustbin?
Arduino Uno R3, HC-SR04 Ultrasonic Sensor, MG90S metal gear micro servo, Electrolytic Capacitor 100 µF, Battery holder 4 × AA with switch, AA alkaline battery × 4 and Jumper wires male to female (40 pcs). MG90S metal gear micro servo: Metal gears last longer on a lid.
How long does the touchless smart dustbin take to build?
About 60 minutes for a beginner maker, in 5 steps.
How do I know the touchless smart dustbin works?
No hand: Serial shows the distance to the ceiling or 999; lid stays closed. Hand at 10 cm: Lid opens within about 0.2 s. Hand removed: Lid closes about 3 s later.
The lid opens by itself: what should I check?
The sensor sees the bin rim or a wall. Tilt it outwards or lower the 20 cm limit.
The servo buzzes when closed: what should I check?
CLOSED pushes past the stop. Raise it from 0 to 5 or 10.
The Uno resets when the lid moves: what should I check?
The servo is powered from the Uno. Use the separate AA pack with a shared ground.
Distance always 999: what should I check?
TRIG and ECHO swapped, or ECHO not on D10.
