Smart Dustbin with Ultrasonic Sensor

Introduction

This experiment demonstrates how to build a smart dustbin using an ultrasonic sensor to detect when the bin is full and open the lid automatically.

Ultrasonic Sensor

Components Needed

Circuit Setup

  1. Connect the ultrasonic sensor to the Arduino to measure the distance to the waste.
  2. Connect the servo motor to the Arduino to open/close the dustbin lid.
Smart Dustbin Circuit Setup

Code for Smart Dustbin

Upload the following code to your Arduino to control the dustbin lid:


#include 

const int trigPin = 9;
const int echoPin = 10;
Servo lidServo;

void setup() {
  Serial.begin(9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  lidServo.attach(11);
}

void loop() {
  long duration, distance;
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration / 2) / 29.1;
  
  Serial.print("Distance: ");
  Serial.println(distance);
  
  if (distance < 10) {
    lidServo.write(90); // Open lid
  } else {
    lidServo.write(0); // Close lid
  }
  delay(500);
}
            

Explanation

The ultrasonic sensor measures the distance to the waste, and when the bin is full, it automatically opens the lid using the servo motor.

Troubleshooting