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.
Components Needed
- Ultrasonic Sensor (HC-SR04)
- Servo Motor
- Arduino
- Jumper Wires
Circuit Setup
- Connect the ultrasonic sensor to the Arduino to measure the distance to the waste.
- Connect the servo motor to the Arduino to open/close the dustbin lid.
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
- If the servo motor does not move, check the wiring and the servo pin assignment in the code.
- If the distance readings are inaccurate, ensure the ultrasonic sensor is correctly placed.