To make a smart dustbin using Arduino, an HC-SR04 ultrasonic sensor, and a servo motor, the project will automatically open the lid of the dustbin when an object is detected nearby and close it when the object is removed.
Here are the detailed steps and components needed to make this project:
Components Required:
- Arduino Uno (or any compatible board)
- HC-SR04 Ultrasonic Sensor
- Servo Motor (SG90 or similar)
- Jumper Wires
- Breadboard
- Power supply for Arduino (USB or external)
- Dustbin (small enough to mount the servo motor and ultrasonic sensor)
Circuit Diagram:
- HC-SR04 Ultrasonic Sensor:
- VCC → 5V on Arduino
- GND → GND on Arduino
- Trigger (Trig) → Pin 9 on Arduino
- Echo (Echo) → Pin 10 on Arduino
- Servo Motor:
- VCC → 5V on Arduino
- GND → GND on Arduino
- Control Pin (Signal) → Pin 6 on Arduino
Servo Motor Setup:
The servo motor will be attached to the lid of the dustbin. When the ultrasonic sensor detects an object within a set range, the servo will rotate to open the lid. After a specified time, the servo will return to its initial position, closing the lid.
Steps to Build the Smart Dustbin:
- Attach the Ultrasonic Sensor:
- Mount the ultrasonic sensor at the top of the dustbin, facing forward, where it can detect objects in front of it.
- Ensure the sensor is securely attached and angled for optimal detection.
- Attach the Servo Motor:
- Attach the servo motor to the dustbin lid. Make sure the servo is mounted in a way that it can rotate the lid smoothly.
- Connect the servo’s control wire to pin 6 on the Arduino, and make sure the power and ground wires are connected to the 5V and GND pins of the Arduino.
- Wire Everything Together:
- Connect the HC-SR04 to the Arduino as described above.
- Connect the servo motor to the Arduino using the appropriate pins.
- Arduino Code:
- The code will use the HC-SR04 sensor to measure the distance to any object. If the object is within a defined range (e.g., 10–15 cm), it will trigger the servo to open the dustbin lid. The servo will stay open for a few seconds, then close the lid.
Here is an example Arduino code:
#include <Servo.h>
const int trigPin = 9;
const int echoPin = 10;
const int servoPin = 6;
Servo myServo;
long duration;
int distance;
void setup() {
// Initialize serial communication
Serial.begin(9600);
// Set up the pins
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
// Attach the servo motor to the pin
myServo.attach(servoPin);
// Initially close the lid
myServo.write(0);
delay(1000);
}
void loop() {
// Clear the trigPin by setting it LOW
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Send a pulse to trigger the ultrasonic sensor
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Measure the duration of the echo pulse
duration = pulseIn(echoPin, HIGH);
// Calculate the distance in cm
distance = duration * 0.0344 / 2;
// Print the distance for debugging
Serial.print("Distance: ");
Serial.println(distance);
// If an object is detected within the range (e.g., 10-15 cm)
if (distance < 15 && distance > 5) {
// Open the lid
myServo.write(90);
delay(2000); // Keep the lid open for 2 seconds
// Close the lid
myServo.write(0);
delay(1000);
}
delay(100);
}
Explanation of the Code:
- Servo Library: The
Servo
library is used to control the servo motor. - HC-SR04 Setup:
- The trigger pin sends a pulse to initiate the sensor.
- The echo pin listens for the returning pulse and calculates the distance based on the time it takes to return.
- Distance Calculation: The formula used for distance calculation is:
Distance=duration×0.0344/2
This formula gives the distance in centimeters. - Condition to Open the Lid: If the distance detected by the ultrasonic sensor is between 5 cm and 15 cm, the servo will move to an angle of 90°, which opens the lid. After 5 seconds, the lid will close.
Calibration and Testing:
- Test the distance detection range and adjust the values in the code (e.g.,
distance < 15 && distance > 5
) according to your needs. - Test the servo’s movement to ensure it opens and closes the lid correctly.
- Adjust the servo delay times as needed for your dustbin.
Conclusion:
This is a simple project to build a smart dustbin that uses an ultrasonic sensor to detect when someone is near the bin and opens the lid automatically. The servo motor ensures the lid opens and closes smoothly, offering a hands-free experience. You can further improve this by adding more sensors for more accuracy, or integrating a more advanced power management system!