# Waste Segregation and Monitoring System
# Developed for Raspberry Pi Pico using MicroPython
from machine import Pin, ADC, PWM
from time import sleep, ticks_us, ticks_diff
import network
import urequests
# Blynk Setup
BLYNK_AUTH_TOKEN = "ZV9qy5FJL76KYSgp43k9nj2H7ebPr7Ne"
BLYNK_URL = f"http://blynk-cloud.com/{BLYNK_AUTH_TOKEN}/update"
# Component Pins
trig = Pin(2, Pin.OUT) # Ultrasonic Trigger
echo = Pin(3, Pin.IN) # Ultrasonic Echo
ldr = ADC(26) # LDR connected to ADC
pot = ADC(27) # Potentiometer (FSR substitute) connected to ADC
buzzer = PWM(Pin(15)) # Buzzer connected to GPIO15
rgb_red = Pin(16, Pin.OUT)
rgb_green = Pin(17, Pin.OUT)
rgb_blue = Pin(18, Pin.OUT)
button = Pin(14, Pin.IN, Pin.PULL_DOWN) # Push Button
mq135 = ADC(28) # MQ135 Gas Sensor connected to ADC
force_sensor = ADC(29) # Force Sensor connected to ADC
# Wi-Fi Connection
SSID = "Wokwi-GUEST"
PASSWORD = ""
def connect_wifi():
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(SSID, PASSWORD)
while not wlan.isconnected():
print("Connecting to Wi-Fi...")
sleep(1)
print("Connected to Wi-Fi!")
print(wlan.ifconfig())
# Distance Measurement (Ultrasonic Sensor)
def measure_distance():
trig.low()
sleep(0.02)
trig.high()
sleep(0.00001)
trig.low()
pulse_start = pulse_end = 0
while echo.value() == 0:
pulse_start = ticks_us()
if ticks_diff(ticks_us(), pulse_start) > 1000000:
return -1 # Timeout, no echo
while echo.value() == 1:
pulse_end = ticks_us()
if ticks_diff(ticks_us(), pulse_end) > 1000000:
return -1 # Timeout, no echo
pulse_duration = ticks_diff(pulse_end, pulse_start)
distance = (pulse_duration * 0.0343) / 2
return distance
# Buzzer Alert
def buzzer_alert(duration=1):
buzzer.freq(1000)
buzzer.duty_u16(32768) # 50% Duty Cycle
sleep(duration)
buzzer.duty_u16(0)
# RGB LED Control
def rgb_control(red, green, blue):
rgb_red.value(red)
rgb_green.value(green)
rgb_blue.value(blue)
# MQ135 Gas Sensor Reading
def read_gas():
gas_value = mq135.read_u16() # Read raw ADC value
ppm = (gas_value / 65535) * 1000 # Example conversion
return ppm
# Force Sensor Reading
def read_force():
force_value = force_sensor.read_u16() # Read raw ADC value
force = (force_value / 65535) * 100 # Example conversion
return force
# Send Data to Blynk
def send_data_to_blynk(vpin, value):
try:
url = f"{BLYNK_URL}/{vpin}?value={value}"
response = urequests.get(url)
print(f"Blynk Response for {vpin}: {response.text}")
response.close()
except Exception as e:
print(f"Error sending data to Blynk: {e}")
# Main Program
def main():
connect_wifi()
previous_data = {"distance": -1, "pressure": -1, "gas": -1, "light": -1, "force": -1}
while True:
distance = measure_distance()
light_intensity = ldr.read_u16() // 655
pressure = pot.read_u16() // 655
gas = read_gas()
force = read_force()
# RGB LED Indication for Distance
if distance != -1:
if distance < 10: # Less than 10 cm
rgb_control(1, 0, 0) # Red
buzzer_alert()
elif 10 <= distance < 30:
rgb_control(1, 1, 0) # Yellow
else:
rgb_control(0, 1, 0) # Green
# Push Button Logic for Manual Alert
if button.value():
print("Manual Alert Activated!")
buzzer_alert()
# Display and Send Data to Blynk if Changed
if distance != previous_data["distance"]:
send_data_to_blynk("V1", distance)
previous_data["distance"] = distance
print(f"Distance: {distance:.2f} cm")
if pressure != previous_data["pressure"]:
send_data_to_blynk("V2", pressure)
previous_data["pressure"] = pressure
print(f"Pressure: {pressure}")
if gas != previous_data["gas"]:
send_data_to_blynk("V3", gas)
previous_data["gas"] = gas
print(f"Gas: {gas:.2f} ppm")
if light_intensity != previous_data["light"]:
send_data_to_blynk("V4", light_intensity)
previous_data["light"] = light_intensity
print(f"Light Intensity: {light_intensity}")
if force != previous_data["force"]:
send_data_to_blynk("V5", force)
previous_data["force"] = force
print(f"Force: {force:.2f}")
sleep(5) # Delay between readings
# Run the Program
try:
main()
except KeyboardInterrupt:
print("Program Stopped!")