Home and IoTA Smart Bin
Bring your hand close and the lid lifts by itself. An ultrasonic sensor notices the hand, and a servo opens the lid and closes it three seconds after the hand has gone.
SensorsAn ultrasonic sensor measures the distance. Three LEDs show how close the obstacle is, and the buzzer beeps faster the closer you get.
Written by the RoboHub team. A question? Write to us.
Choose a variation and the parts list updates.
Connect the HC-SR04's VCC to 5V, GND to ground, TRIG to pin 9 and ECHO to pin 10.
Connect the green, yellow and red LEDs to pins 5, 6 and 7, each through a 330 Ω resistor. The buzzer's “+” goes to pin 8.
The code measures the distance on every pass and lights the LEDs and beeps accordingly.
const int TRIG = 9, ECHO = 10, BUZZER = 8;
const int GREEN = 5, YELLOW = 6, RED = 7;
void setup() {
pinMode(TRIG, OUTPUT);
pinMode(ECHO, INPUT);
pinMode(GREEN, OUTPUT);
pinMode(YELLOW, OUTPUT);
pinMode(RED, OUTPUT);
}
long distanceCm() {
digitalWrite(TRIG, LOW);
delayMicroseconds(2);
digitalWrite(TRIG, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG, LOW);
long us = pulseIn(ECHO, HIGH, 30000);
return us / 58; // 0 = nothing in range
}
void loop() {
long cm = distanceCm();
bool seen = cm > 0;
digitalWrite(GREEN, seen && cm > 50);
digitalWrite(YELLOW, seen && cm > 20 && cm <= 50);
digitalWrite(RED, seen && cm <= 20);
if (seen && cm <= 50) {
tone(BUZZER, 2000, 60);
delay(map(cm, 0, 50, 60, 500)); // closer = faster beeps
} else {
delay(100);
}
}Slowly bring your hand towards the sensor: at 50 cm the yellow LED lights and the beeping starts, at 20 cm the red one.

Illustration, made with AI
Illustration: on a garage floor a toy car reverses toward the ultrasonic sensor; the red LED on the breadboard is lit, with a buzzer beside it.
Step 4: Try it
Home and IoTBring your hand close and the lid lifts by itself. An ultrasonic sensor notices the hand, and a servo opens the lid and closes it three seconds after the hand has gone.
Home and IoTType the secret code and press #: with the right code the green light comes on and a servo slides the bolt open, with the wrong one the buzzer beeps three times.
GamesAn LED lights at a random moment. Whoever presses their button first wins. Built from Student Kit parts in 45 minutes.