Smart Dustbin With Ultrasonic Sensor and Servo

by Ameya Angadi in Circuits > Arduino

37 Views, 0 Favorites, 0 Comments

Smart Dustbin With Ultrasonic Sensor and Servo

Smart Dustbin Cover Image k.png

Build a smart dustbin that opens automatically using an ultrasonic sensor and an Arduino UNO.

Supplies

  1. Arduino Uno
  2. SG90 Servo Motor
  3. Male-Male Jumper Wires
  4. Male-Female Jumper Wires
  5. HCSR04 Ultrasound Sensor

Connecting the Ultrasonic Sensor (HC-SR04)

  1. Connect the VCC pin of the HC-SR04 sensor to the 5V pin on the Arduino.
  2. Connect the GND pin to the Arduino's ground.
  3. Connect the Trig pin to digital pin 5 on the Arduino.
  4. Connect the Echo pin to digital pin 6 on the Arduino.

Setting Up the Servo Motor for Lid Control:

  1. Connect the servo motor’s VCC (red wire) to the Arduino’s 5V pin.
  2. Connect the GND (black wire) to the Arduino's ground.
  3. Connect the servo signal wire (usually orange or yellow) to digital pin 9 on the Arduino.

Writing the Code:

In the code, the ultrasonic sensor measures distance, and if the distance is within a specified range (e.g., 20 cm or less), the servo motor rotates to open the dustbin lid. The lid will close after a short delay of 3.5 seconds.

Testing the Setup:

Place the dustbin setup in front of you, ensuring the ultrasonic sensor points outward.

Move your hand or an object close to the sensor (within 20 cm) to see the lid open.

After a delay, the lid should automatically close.

Troubleshooting Tips:

Ensure the servo motor and ultrasonic sensor are correctly wired to avoid inaccurate readings.

If the lid does not open smoothly, adjust the servo angle in the code for an optimal opening range.

Want to see this project in action?

Watch my YouTube video where I demonstrate how the smart dustbin works step-by-step!

Watch the Video for Step-By-step Tutorial

Smart Dustbin with Ultrasonic Sensor and Servo

Conclusion:

By completing this project, you've built a simple yet effective smart dustbin that opens its lid automatically when needed. You can adjust the range or add features like LED indicators to make it more interactive. Check out my other projects for ideas to expand your smart dustbin system!

If you want to explore more, check out my profile for related projects, and don’t forget to follow me for updates on new tutorials and advanced projects!

Code

/*
* 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 <https://www.gnu.org/licenses/gpl-3.0.en.html>.
*
*
* 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() {
Serial.begin(9600);
servo.attach(servoPin);
pinMode(trig, OUTPUT);
pinMode(echo, INPUT);
servo.write(0); // Initializes with the lid closed
delay(100);
servo.detach(); // 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
servo.attach(servoPin);
servo.write(0); // Opens the lid
delay(3500); // Keeps the lid open for 3.5 seconds
servo.write(180); // Closes the lid
delay(1500);
servo.detach(); // Detach to save power
}
}

Downloads

Smart Dustbin Connections

ed2a8137-1295-435b-b89e-c4b1c19fcd74.png

Follow this circuit diagram to make this project.