Illustration: a bin with an ultrasonic sensor on the front; a servo has lifted the lid.Home and IoT
Illustration (made with AI): roughly what the finished project looks like.

A 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.

Difficulty
Beginner
Time
1 h
Age
10+
Parts
5, 5 on the site

Written by the RoboHub team. A question? Write to us.

Variations

Choose a variation and the parts list updates.

Instructions

  1. Sensor

    Connect the HC-SR04's VCC to 5V, GND to ground, TRIG to pin 6 and ECHO to pin 7. Mount the sensor on the front of the bin, facing up or forward.

    D3 is the board's digital pin 3, GND is ground. Red wires are power, black ground, the other colours signals (on a real servo the ground wire is often brown). In the steps, what a step adds is in colour and what is already connected in grey.

    • HC-SR04 sensor VCC → Arduino 5V
    • HC-SR04 sensor TRIG → Arduino D6
    • HC-SR04 sensor ECHO → Arduino D7
    • HC-SR04 sensor GND → Arduino GND
  2. Servo

    The servo has three wires: brown to ground, red to 5V, orange to pin 3. The sensor and the servo both need 5V, so feed them from one breadboard rail.

    D3 is the board's digital pin 3, GND is ground. Red wires are power, black ground, the other colours signals (on a real servo the ground wire is often brown). In the steps, what a step adds is in colour and what is already connected in grey.

    • SG90 servo SIG → Arduino D3
    • SG90 servo + → Arduino 5V
    • SG90 servo − → Arduino GND
  3. The lid mechanism

    Fix the servo at the back of the bin next to the lid's hinge, and link its arm to the lid with a piece of wire or a wooden stick. Choose a light lid: an SG90 can't lift a heavy one.

  4. Upload the code

    CLOSED and OPEN are the lid angles. If the lid doesn't open fully or goes too far, change these numbers.

    Arduino C++
    #include <Servo.h>
    
    const int TRIG = 6, ECHO = 7, SERVO = 3;
    const int CLOSED = 0, OPEN = 100;      // lid angles: adjust them for your bin
    const int NEAR_CM = 20;                // a hand closer than this opens the lid
    Servo lid;
    unsigned long lastSeen = 0;
    
    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 setup() {
      pinMode(TRIG, OUTPUT);
      pinMode(ECHO, INPUT);
      lid.attach(SERVO);
      lid.write(CLOSED);
    }
    
    void loop() {
      long cm = distanceCm();
      if (cm > 0 && cm < NEAR_CM) {
        lid.write(OPEN);
        lastSeen = millis();
      } else if (millis() - lastSeen > 3000) {
        lid.write(CLOSED);                 // 3 seconds after the hand has gone
      }
      delay(100);
    }
  • Sensors
    Beginner1 h10+

    Parking Sensor with Sound and Lights

    An ultrasonic sensor measures the distance. Three LEDs show how close the obstacle is, and the buzzer beeps faster the closer you get.

    6/6 parts on the site
  • Home and IoT
    Intermediate1 h12+

    A Code Lock with a Keypad

    Type 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.

    7/7 parts on the site
  • Home and IoT
    Beginner30 min10+

    A Motion Alarm

    A PIR sensor notices a warm body moving. As soon as someone walks into the room, a siren sounds and a red light flashes.

    6/6 parts on the site