Devices & Components
Arduino Uno Rev3
Male/Male Jumper Wires
Ultrasonic Sensor - HC-SR04 (Generic)
Male/Female Jumper Wires
SG90 Micro-servo motor
Software & Tools
Arduino IDE
Project description
Code
Smart_Dustbin_Code
cpp
Upload this code to your Arduino
1/* 2 * Project Name: Smart Dustbin with Ultrasonic Sensor and Servo 3 * 4 * Project Description: 5 * Build a smart dustbin that opens automatically using an ultrasonic sensor and an Arduino UNO. 6 * The dustbin lid opens when an object is detected within 20 cm and closes after 3.5 seconds. 7 * 8 * License: GPL3+ 9 * This project is licensed under the GNU General Public License v3.0 or later. 10 * You are free to use, modify, and distribute this software under the terms 11 * of the GPL, as long as you preserve the original license and credit the original 12 * author. For more details, see <https://www.gnu.org/licenses/gpl-3.0.en.html>. 13 * 14 * 15 * Copyright (C) 2024 Ameya Angadi 16 * 17 * 18 * Code Created By: Ameya Angadi 19 * Last Modified On: December 5, 2024 20 * Version: 1.1 21 * 22 */ 23 24 25#include <Servo.h> // Includes the Servo library 26 27Servo servo; 28int trig = 5; // Pin for triggering the ultrasonic sensor 29int echo = 6; // Pin for receiving the echo signal 30int servoPin = 9; // Pin to control the servo motor 31long Duration, Distance, Average; 32long aver[3]; // Array to store distance readings for averaging 33 34void setup() { 35 Serial.begin(9600); 36 servo.attach(servoPin); 37 pinMode(trig, OUTPUT); 38 pinMode(echo, INPUT); 39 servo.write(0); // Initializes with the lid closed 40 delay(100); 41 servo.detach(); // Detaches servo to save power and avoid jitter 42} 43 44void measure() { 45 digitalWrite(trig, LOW); 46 delayMicroseconds(5); 47 digitalWrite(trig, HIGH); 48 delayMicroseconds(15); 49 digitalWrite(trig, LOW); 50 Duration = pulseIn(echo, HIGH); // Measures the time taken by the echo 51 Distance = (Duration / 2) / 29.1; // Converts time to distance in centimeters 52} 53 54void loop() { 55 for (int i = 0; i <= 2; i++) { 56 measure(); 57 aver[i] = Distance; // Store each reading for averaging 58 delay(10); 59 } 60 61 Distance = (aver[0] + aver[1] + aver[2]) / 3; // Calculate average distance 62 63 if (Distance <= 20) { // Opens the lid if an object is detected within 20 cm 64 servo.attach(servoPin); 65 servo.write(0); // Opens the lid 66 delay(3500); // Keeps the lid open for 3.5 seconds 67 servo.write(180); // Closes the lid 68 delay(1500); 69 servo.detach(); // Detach to save power 70 } 71}
Downloadable files
Smart Dustbin Connections
Follow this circuit diagram to make this project.
Smart_Dustbin_Connections.png

Comments
Only logged in users can leave comments