Smart Dustbin using Ultrasonic Sensor with Raspberry Pi
This project demonstrates how to create a smart dustbin using an ultrasonic sensor with Raspberry Pi. The system automatically detects the level of waste in the dustbin and sends an alert when the dustbin is full.
1. Introduction to Smart Dustbin
A smart dustbin is a system that can automatically detect the level of waste in a dustbin using an ultrasonic sensor. The ultrasonic sensor measures the distance between the sensor and the waste level, and based on the measurement, it sends an alert if the dustbin is full. This helps in managing waste and can be useful for both home and industrial applications.
2. What You Need: Components and Tools
Required Components:
- Raspberry Pi (any model with GPIO pins, preferably Pi 3 or Pi 4)
- HC-SR04 Ultrasonic Sensor
- Buzzer or LED (to indicate full bin)
- Breadboard
- Jumper Wires
- Power Supply for Raspberry Pi
3. Working Principle of the Smart Dustbin
The HC-SR04 ultrasonic sensor uses sound waves to detect the distance between the sensor and the waste level in the dustbin. The system continuously monitors this distance and, when the distance falls below a preset threshold, it triggers an alert, either in the form of a buzzer sound or a LED light, indicating that the dustbin is full.
4. Wiring Diagram: Raspberry Pi and HC-SR04 Ultrasonic Sensor
HC-SR04 to Raspberry Pi Connections:
- VCC → 5V (Raspberry Pi)
- GND → GND (Raspberry Pi)
- TRIG → GPIO23 (Pin 16)
- ECHO → GPIO24 (Pin 18)
5. Raspberry Pi Code for Smart Dustbin System
- Install necessary libraries: `sudo apt-get install python-rpi.gpio`
- Then upload the following Python code:
import RPi.GPIO as GPIO
import time
# Set up GPIO pins
GPIO.setmode(GPIO.BCM)
GPIO.setup(23, GPIO.OUT)
GPIO.setup(24, GPIO.IN)
GPIO.setup(18, GPIO.OUT)
# Function to measure distance
def measure_distance():
GPIO.output(23, True)
time.sleep(0.00001)
GPIO.output(23, False)
pulse_start = time.time()
while GPIO.input(24) == 0:
pulse_start = time.time()
while GPIO.input(24) == 1:
pulse_end = time.time()
pulse_duration = pulse_end - pulse_start
distance = pulse_duration * 17150
distance = round(distance, 2)
return distance
# Main program loop
try:
while True:
distance = measure_distance()
print(f'Distance: {distance} cm')
if distance < 10: # threshold for full bin
GPIO.output(18, True) # Turn on buzzer/LED
else:
GPIO.output(18, False) # Turn off buzzer/LED
time.sleep(1)
except KeyboardInterrupt:
GPIO.cleanup()
6. Code Explanation
- The program continuously measures the distance between the sensor and the waste level in the dustbin.
- The `measure_distance()` function sends a pulse from the TRIG pin and calculates the time it takes for the pulse to return after hitting an object. This time is then used to calculate the distance.
- If the distance falls below the preset threshold (e.g., 10 cm), it triggers the buzzer or LED to indicate that the dustbin is full.
7. Applications
- Smart waste management in public places
- Automated waste tracking in offices and homes
- Industrial waste monitoring
- IoT-based waste monitoring systems
8. FAQs: Smart Dustbin with Raspberry Pi
Q: Why is the ultrasonic sensor not detecting the correct distance?
A: Make sure the ultrasonic sensor is correctly connected, and the sensor is facing the correct direction. Also, ensure that there is no obstruction around the sensor.
Q: Can I use a different sensor for the project?
A: Yes, you can use other distance sensors such as the SRF05 or any other compatible sensor that works with the Raspberry Pi.
Q: How can I enhance this project?
A: You can add additional features like sending alerts via email or SMS when the dustbin is full, or integrate it with a cloud platform for monitoring.
9. Conclusion: What You’ve Learned
- How to interface the HC-SR04 ultrasonic sensor with Raspberry Pi
- How to measure distance using ultrasonic waves
- Creating a smart dustbin system that alerts when it's full