print("Smart Dust Bin")
print("Date: 18/11/2024" )
print("Created by A.Zul")
# Import libraries
import OLED_library
from Ultrasonic_lib import HCSR04 # Import the Ultrasonic sensor library
from machine import Pin, SoftI2C, PWM
from utime import sleep
# Declaration for OLED
oled_pin = SoftI2C(scl=Pin(22), sda=Pin(21))
OMER = OLED_library.SSD1306_I2C(width=128, height=64, i2c=oled_pin)
# Initialize ultrasonic sensor
sensor = HCSR04(trigger_pin=5, echo_pin=4)
# Define pins for LEDs
green_led = Pin(13, Pin.OUT) # Green LED for "empty"
yellow_led = Pin(12, Pin.OUT) # Yellow LED for "half-full"
red_led = Pin(14, Pin.OUT) # Red LED for "full"
# Define buzzer with PWM
buzzer = PWM(Pin(27))
# Define servo motor (connected to GPIO 18)
servo = PWM(Pin(18))
servo.freq(50) # Standard servo frequency: 50Hz
# Function to control the servo
def set_servo_angle(angle):
# Convert angle to duty cycle (0-180 degrees maps to 0.5ms-2.5ms pulse width)
duty = int((angle/ 180) * 102 + 26) # Map 0°-180° to duty cycle
servo.duty(duty)
def sound_buzzer_full():
for _ in range(5):
"""
Play two distinct sounds when the bin is full.
"""
# First sound: High-pitched beep
buzzer.freq(800)
buzzer.duty(50)
sleep(0.3)
buzzer.duty(0)
sleep(0.1)
# Second sound: Low-pitched beep
buzzer.freq(400)
buzzer.duty(50)
sleep(0.4)
buzzer.duty(0)
sleep(0.2)
def display_oled(message, distance):
"""
Function to update the OLED display with status messages and distance.
"""
OMER.fill(0) # Clear the OLED
OMER.text("Garbage Bin Status", 0, 0, 1)
OMER.text(message, 0, 20, 1)
OMER.text("Distance: {:.1f}cm".format(distance), 0, 40, 1)
OMER.show()
def main():
max_height = 106 # Maximum height of the garbage bin in cm
full_threshold = 10 # Distance threshold for "full" in cm
half_threshold = 53 # Distance threshold for "half-full" in cm
while True:
try:
# Measure distance using the HC-SR04 sensor
distance = sensor.distance_cm()
print("Distance:", distance, "cm")
# Calculate the fill level percentage
fill_level = 100 - ((distance / max_height) * 100)
print("Fill Level:", round(fill_level), "%")
# Determine bin status
if distance <= full_threshold:
# Bin is full
green_led.value(0) # Turn off green
yellow_led.value(0) # Turn off yellow
red_led.value(1) # Turn on red
sound_buzzer_full() # Play the two distinct sounds
set_servo_angle(0) # Close the lid (angle 0°)
display_oled("Bin is FULL!", distance)
print("Bin is FULL!")
elif distance <= half_threshold:
# Bin is half-full
green_led.value(0) # Turn off green
yellow_led.value(1) # Turn on yellow
red_led.value(0) # Turn off red
buzzer.duty(0) # Turn off buzzer
set_servo_angle(90) # Keep lid partially open
display_oled("Bin is HALF-FULL!", distance)
print("Bin is HALF-FULL!")
else:
# Bin is empty
green_led.value(1) # Turn on green
yellow_led.value(0) # Turn off yellow
red_led.value(0) # Turn off red
buzzer.duty(0) # Turn off buzzer
set_servo_angle(90) # Keep lid fully open
display_oled("Bin is EMPTY!", distance)
print("Bin is EMPTY!")
except OSError as e:
print("Error reading from sensor:", e)
display_oled("Sensor Error!", 0)
sleep(0.1)
# Run the program
main()