🗑️ Project 6 – Smart Bin (HC-SR504)

This smart bin with Arduino HC-SR04 improves hygiene and recycling by automatically opening when waste is detected. Furthermore, the ultrasonic sensor (HC-SR04) accurately detects waste at a minimum distance of 15 cm, which triggers the bin to open using a servo motor. that opens the lid at a right angle (90°) for 5 seconds (enough time to dispose of the waste) before closing immediately.

I decided to realize this project in two phases:

  1. First, I will test the code on a paper box to calculate the angle and figure out the materials needed for the final build.
  2. Once finished (not done yet), I will implement the project on a real bin.

Requirements:

  • Arduino Uno or ESP32
  • 5V Battery
  • HC-SR504 Ultrasonic Sensor
  • Servo motor
  • 7 Wires
  • 1 Paper box ( for prototype)
  • Glue (to stick the paper)

1. Box in Paper

1. Creation of Box

To create the box I’ll use a patreon that I’ve created

2. Connection

We need to connect the Servo motor : Red -> 5V (on Arduino),Yellow -> Pin DATA (any), Brown -> GND (on Arduino).

And the HC-SR04: TRIG -> Pin 5, ECHO -> Pin 6 (on Arduino), 5V and GND connected accordingly.

3. Code

To write the code, we’ll use the Ultrasonic library to help significantly to use the HC SR504 to give information and control the range of detection. and Servo to just control the servo by giving the angle with myservo.write(90). To finish we need to create a loop for the countdown of the bin (the 5 seconds between each opening). And I decided to do a phase after a cycle we need to wait 10 seconds because It’s a bit silly to open and reopen the trash can in such a short interval.

#include <Ultrasonic.h>
#include <Servo.h>

Ultrasonic ultrasonic(5, 6);  // TRIG = 5, ECHO = 6
Servo myservo;

const int openAngle = 90;
const int closeAngle = 0;
const int triggerDistance = 15;            //  Trigger by 15 cm
const unsigned long openDuration = 3000;  // 5 seconds
const unsigned long cooldownTime = 500;    // Wait before new open

bool isOpen = false;
bool ready = true;
unsigned long eventTime = 0;

void setup() {
  Serial.begin(9600);
  myservo.attach(9);
  myservo.write(closeAngle);
}

void loop() {
  int distance = getFilteredDistance();
  Serial.print("Distance (cm): ");
  Serial.println(distance);

  unsigned long now = millis();

  if (ready && !isOpen && distance <= triggerDistance) {
    myservo.write(openAngle);
    Serial.println("🔓 Open !");
    isOpen = true;
    ready = false;
    eventTime = now;
  }

  // Fermeture après 10 secondes
  if (isOpen && now - eventTime >= openDuration) {
    myservo.write(closeAngle);
    Serial.println("🔒 Lock.");
    isOpen = false;
    eventTime = now;  // Start cooldown
  }

  // Réarmement après cooldown
  if (!ready && !isOpen && now - eventTime >= cooldownTime) {
    ready = true;
    Serial.println("✅ Ready to open if a hand come.");
  }

  delay(100);
}

int getFilteredDistance() {
  int total = 0;
  int count = 0;
  for (int i = 0; i < 3; i++) {
    int d = ultrasonic.read();
    if (d > 0 && d < 100) {
      total += d;
      count++;
    }
    delay(20);
  }
  return (count > 0) ? total / count : 999;
}

4. Conclusion

The bin works very well in this prototype phase. The next step is to implement the same system on a real, full-sized bin to test its performance in practical conditions.

You can find a project that combine led, servo and joystick here !

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *