/*
* Project Name: Smart Dustbin with Ultrasonic Sensor and Servo
*
* Project Description:
* Build a smart dustbin that opens automatically using an ultrasonic sensor and an
Arduino UNO.
* The dustbin lid opens when an object is detected within 20 cm and closes after
3.5 seconds.
*
* License: GPL3+
* This project is licensed under the GNU General Public License v3.0 or later.
* You are free to use, modify, and distribute this software under the terms
* of the GPL, as long as you preserve the original license and credit the original
* author. For more details, see <[Link]
*
*
* Copyright (C) 2024 Ameya Angadi
*
*
* Code Created By: Ameya Angadi
* Last Modified On: December 5, 2024
* Version: 1.1
*
*/
#include <Servo.h> // Includes the Servo library
Servo servo;
int trig = 5; // Pin for triggering the ultrasonic sensor
int echo = 6; // Pin for receiving the echo signal
int servoPin = 9; // Pin to control the servo motor
long Duration, Distance, Average;
long aver[3]; // Array to store distance readings for averaging
void setup() {
[Link](9600);
[Link](servoPin);
pinMode(trig, OUTPUT);
pinMode(echo, INPUT);
[Link](0); // Initializes with the lid closed
delay(100);
[Link](); // Detaches servo to save power and avoid jitter
}
void measure() {
digitalWrite(trig, LOW);
delayMicroseconds(5);
digitalWrite(trig, HIGH);
delayMicroseconds(15);
digitalWrite(trig, LOW);
Duration = pulseIn(echo, HIGH); // Measures the time taken by the echo
Distance = (Duration / 2) / 29.1; // Converts time to distance in centimeters
}
void loop() {
for (int i = 0; i <= 2; i++) {
measure();
aver[i] = Distance; // Store each reading for averaging
delay(10);
}
Distance = (aver[0] + aver[1] + aver[2]) / 3; // Calculate average distance
if (Distance <= 20) { // Opens the lid if an object is detected within 20
cm
[Link](servoPin);
[Link](0); // Opens the lid
delay(3500); // Keeps the lid open for 3.5 seconds
[Link](180); // Closes the lid
delay(1500);
[Link](); // Detach to save power
}
}