Smart Dustbin using Arduino
Smart Dustbin using Arduino
Overview
In this lesson, we will build a Smart Dustbin using an Arduino Uno microcontroller, an ultrasonic
sensor for detecting objects, and a servo motor to automate the opening and closing of the dustbin
flap. This project will introduce you to basic electronics, sensor integration, and microcontroller
programming using the Arduino IDE.
Stem connection
Materials Required
Arduino IDE: This is the development environment where we will write and upload the code to the
Arduino board.
Components Overview
1.Arduino Uno:
A microcontroller board based on the ATmega328P. It has 14 digital I/O pins, 6 analog inputs, a
USB connection, a power jack, and more. It’s user-friendly and widely used for DIY electronics
projects.
Measures the distance to objects using SONAR technology. It sends out sound waves and waits
for the echo to return. Based on the time it takes for the echo to return, the distance is calculated.
• Pins:
• VCC: Power Supply (5V)
• Trig: Sends out the ultrasonic signal
• Echo: Receives the signal when it bounces back
• GND: Ground
3.SG90 Micro Servo Motor:
A small motor used for precise movements. In this project, it will open and close the dustbin lid. It’s
lightweight, affordable, and perfect for small-scale robotics.
Working Concept
The Smart Dustbin automates the opening and closing of the dustbin lid based on proximity
detection:
Circuit Diagram
Connections:
1.Ultrasonic Sensor:
VCC → 5V (Arduino)
GND → GND (Arduino)
Trig → Pin 5 (Arduino)
Echo → Pin 6 (Arduino)
2.Servo Motor:
Signal → Pin 7 (Arduino)
VCC → 3.3V (Arduino)
GND → GND (Arduino)
3.Arduino Power:
Connect a 9V battery to the Vin and GND pins on the Arduino to power it.
void setup() {
Serial.begin(9600); // Begin serial communication for debugging
servo.attach(servoPin); // Attach the servo to the specified pin
pinMode(trigPin, OUTPUT); // Set the Trig pin as an output
pinMode(echoPin, INPUT); // Set the Echo pin as an input
servo.write(0); // Ensure the dustbin flap is initially closed
delay(100);
servo.detach(); // Detach servo to save power when not in use
}
void measure() {
digitalWrite(10, HIGH); // Optional: Turn on an LED when measuring distance
digitalWrite(trigPin, LOW); // Set the Trig pin low for a brief moment
delayMicroseconds(5);
digitalWrite(trigPin, HIGH); // Set the Trig pin high to send an ultrasonic pulse
delayMicroseconds(15);
digitalWrite(trigPin, LOW); // Set the Trig pin low again
duration = pulseIn(echoPin, HIGH); // Measure how long it takes for the echo to return
dist = (duration / 2) / 29.1; // Calculate the distance in cm
}
void loop() {
for (int i = 0; i <= 2; i++) { // Measure the distance multiple times for accuracy
measure(); // Call the measure function
aver[i] = dist; // Store each measurement in the array
delay(10); // Wait a short time between measurements
}
if (dist < 50) { // If an object is closer than 50 cm, open the dustbin
servo.attach(servoPin); // Attach the servo to control it
delay(1);
servo.write(0); // Open the dustbin flap
delay(3000); // Keep it open for 3 seconds
servo.write(150); // Close the dustbin flap
delay(1000);
servo.detach(); // Detach the servo to save power
}
Code Explanation
We first include the Servo.h library, which allows us to control the servo motor. Then, we create a
servo object called servo.
2.Pin Configuration:
Pins are declared for the Trig and Echo pins of the ultrasonic sensor, the Servo motor, and an
optional LED.
3.Setup Function:
Serial.begin(9600) initializes serial communication, which we can use for debugging. The servo
motor is attached to the specified pin. We also configure the Trig and Echo pins as output and
input, respectively.
4.Measure Function:
This function sends out an ultrasonic pulse using the Trig pin and measures how long it takes for
the pulse to bounce back. The time is converted into a distance value in centimeters using the
formula: dist = (duration / 2) / 29.1.
5.Loop Function:
The loop function continuously monitors the distance measured by the ultrasonic sensor. If the
distance is less than 50 cm (indicating an object in front of the dustbin), the servo motor opens the
lid for 3 seconds and then closes it.
6.Average Calculation:
• The distance is measured three times to improve accuracy, and the average is taken
to reduce noise in the measurements.
7.Servo Control:
The servo.attach() function is used to engage the servo motor when needed, and servo.detach() is
used to disengage it to save power.
• Change Opening Time: You can modify the time the dustbin lid stays open by
adjusting the delay(3000) in the code. Change the value 3000 to a different number (in
milliseconds).
• Adjust Detection Range: If you want the dustbin to open at a different distance,
change the if (dist < 50) condition. Adjust 50 to the desired range in centimeters.
Conclusion
************