Building a Smart Dustbin with Arduino and Ultrasonic Sensor

Building a Smart Dustbin with Arduino! In today’s innovative world, where contactless solutions are highly valued, building a smart dustbin can be a fantastic project. In this tutorial, we’ll guide you through creating a smart dustbin using Arduino Uno and an ultrasonic sensor. This project will not only help you keep your surroundings clean but also introduce you to the exciting world of Arduino programming.

Hardware Required:

  1. Arduino Uno
  2. Ultrasonic Sensor HC-SR04
  3. SG-90 Micro Servo Motor
  4. Dustbin
  5. Full Kit

Software Required:

  • Arduino IDE

Arduino Uno:

Arduino Uno is an open-source microcontroller board based on the ATmega328P processor. It offers various digital and analog pins, making it perfect for this project. Inexpensive and versatile, the Uno is an ideal choice for beginners and experienced hobbyists alike.

The Arduino Uno is a widely popular and beginner-friendly development board, offering 14 digital I/O pins, 6 analog input pins, USB connectivity, a 16 MHz quartz crystal, a built-in LED, and a reset button. It finds application in robotics, home automation, prototyping, and DIY electronics. Its simplicity, versatility, extensive community support, and online resources contribute to its immense popularity.
Arduino-Uno

Ultrasonic Sensor HC-SR04:

The HC-SR04 ultrasonic sensor is used to measure distances using sonar technology. It emits ultrasound waves and calculates the time taken for the waves to bounce back, allowing precise distance measurements.

Building a Smart Dustbin with Arduino: The sensor emits ultrasonic waves from the Trig pin. Subsequently, it measures the time for the waves to bounce back and receives them through the Echo pin. By calculating the time delay, it accurately and in real-time determines the distance to the object.

SG-90 Micro Servo Motor:

The SG-90 micro servo motor is lightweight, high-quality, and fast. It is commonly used in remote-controlled vehicles and robotics projects. In this project, it controls the lid of the dustbin, making it open and close automatically.

TowerPro SG-90 Servo Motor

Working Concept:

  1. Ultrasonic Detection: The ultrasonic sensor (HC-SR04) detects objects in front of the dustbin.
  2. Arduino Processing: The detected signals are sent to the Arduino Uno, which processes the information.
  3. Servo Motor Control: Based on the received signals, the Arduino triggers the SG-90 servo motor, opening the dustbin lid.
  4. Automatic Closure: The lid remains open for a predetermined duration (here, 3 seconds). After this time, the flap automatically closes.

Circuit Diagram:

Building a Smart Dustbin with Arduino and Ultrasonic Sensor
  • Ultrasonic Sensor Connections:
    • Echo Pin to Arduino Pin 5
    • Trig Pin to Arduino Pin 6
    • VCC to Arduino 5V
    • GND to Arduino GND
  • Servo Motor Connections:
    • Signal Pin to Arduino Pin 7
    • VCC to Arduino 3.3V
    • GND to Arduino GND
  • Power Supply:
    • Connect a 9V battery to the Vin pin on Arduino Uno.

Arduino Code:

#include <Servo.h>

Servo servo;     // Create a Servo object

int trigPin = 2;    // Pin for ultrasonic sensor trigger

int echoPin = 3;    // Pin for ultrasonic sensor echo

int servoPin = 9;   // Pin for servo motor

int led = 10;       // Pin for status LED

long duration, dist, average;   // Variables for ultrasonic sensor readings

long aver[3];   // Array for average distance readings

void setup() {

    Serial.begin(9600);  // Initialize serial communication for debugging

    servo.attach(servoPin);  // Attach the servo motor to its pin

    pinMode(trigPin, OUTPUT);  // Set trigPin as an output

    pinMode(echoPin, INPUT);   // Set echoPin as an input

    pinMode(led, OUTPUT);  // Set LED pin as an output

    servo.write(0);         // Close cap on power on

    delay(100);

    servo.detach();  // Detach the servo motor initially

}

void measure() {

    digitalWrite(led, HIGH);  // Turn on LED during measurement

    digitalWrite(trigPin, LOW);  // Prepare the ultrasonic sensor by sending a low pulse

    delayMicroseconds(5);

    digitalWrite(trigPin, HIGH);  // Send a high pulse to trigger the ultrasonic sensor

    delayMicroseconds(15);

    digitalWrite(trigPin, LOW);  // Stop the pulse

    pinMode(echoPin, INPUT);  // Set echoPin as an input to listen for the echo

    duration = pulseIn(echoPin, HIGH);  // Measure the time it takes for the echo to be received

    dist = (duration / 2) / 29.1;  // Calculate distance in centimeters using the speed of sound

    digitalWrite(led, LOW);  // Turn off LED after measurement

}

void loop() {

    for (int i = 0; i <= 2; i++) {   // Average distance readings

        measure();  // Call the function to measure distance

        aver[i] = dist;  // Store the distance in the array

        delay(10);  // Delay between measurements

    }

    dist = (aver[0] + aver[1] + aver[2]) / 3;  // Calculate the average distance

    if (dist < 50) {  // If an object is detected within 50 cm

        servo.attach(servoPin);  // Attach the servo motor

        delay(1);

        servo.write(0);  // Open the cap

        delay(3000);  // Wait for 3 seconds (adjustable)

        servo.write(150);  // Close the cap

        delay(1000);  // Wait for 1 second

        servo.detach();  // Detach the servo motor

    }

    Serial.print("Distance: ");

    Serial.print(dist);  // Print the distance to Serial Monitor

    Serial.println(" cm");  // Print unit (centimeters)

    delay(1000);  // Delay for a second between readings

}

Conclusion:

Building a Smart Dustbin with Arduino and an ultrasonic sensor not only contributes to a cleaner environment but also enhances your skills in programming and electronics. This project demonstrates the seamless integration of sensors and microcontrollers for practical applications. Feel free to experiment, modify, and extend this project to explore even more possibilities in the world of electronics and automation. Happy tinkering!