Skip to content

4.1.6 Smart Sanitation AI Framework

Overview

Build and prototype a smart sanitation AI framework using the Raspberry Pi Pico 2 W and MicroPython.

This expert project is designed as an open-ended research and prototype challenge. Learners should use the concept, suggested components, and starter code as a guide, then conduct further research, improve the design, and build a working prototype.

The final result should demonstrate a working embedded system prototype with clear documentation, testing evidence, and a short presentation of design decisions.

Required Components

Pico 2 W
Raspberry Pi Pico 2 W
Ultrasonic Sensor
Usage sensor
Gas Sensor
Odor sensor
Ultrasonic Sensor
Waste-level sensor
Breadboard
Breadboard
Jumper Wires
Jumper wires

Component Image
Raspberry Pi Pico 2 W
Usage Sensor
Odor Sensor
Waste Level Sensor
Breadboard
Jumper Wires

Circuit Connections

Component Pin Connects To Pico GPIO / Physical Pin Number Notes
Usage Sensor GPIO14 See source design notes Verify pin compatibility and voltage level
Odor Sensor ADC0 (Pin 26) See source design notes Verify pin compatibility and voltage level
Waste Level Sensor ADC1 (Pin 27) See source design notes Verify pin compatibility and voltage level

Step-by-Step Assembly

Hardware Setup

Step 1: Connect the Usage Sensor

Connect the usage sensor output to GPIO14 on the Raspberry Pi Pico 2 W.


Step 2: Connect the Odor Sensor

Connect the odor sensor output to ADC0 (Pin 26).


Step 3: Connect the Waste Level Sensor

Connect the waste level sensor output to ADC1 (Pin 27).


Step 4: Verify the Circuit

Confirm that:

  • Usage sensor is connected to GPIO14
  • Odor sensor is connected to ADC0 (Pin 26)
  • Waste level sensor is connected to ADC1 (Pin 27)
  • All power and ground connections are secure

Testing Individual Components

Before running the full project, test each subsystem separately. This makes it easier to find wiring, code, or logic problems.

  • Test each sensor reading in the Thonny Shell.
  • Test each actuator using a safe low-power test.
  • Verify communication between system layers.
  • Record results and adjust the prototype before final integration.

Full Project Code

After completing and checking the circuit connections, open Thonny IDE, copy and paste this code into a new file or upload the project file to the Raspberry Pi Pico 2 W, then run it from Thonny.

Starter Code


# Smart Sanitation AI Framework - Project 357

# AI-Enhanced Multi-Parameter Monitoring

from machine import Pin, ADC
import time

usage = Pin(14, Pin.IN)
odor = ADC(Pin(26))
waste = ADC(Pin(27))

# Layer 1: Facility Edge Node
def read_sanitation_data():
    usage_count = usage.value()
    odor_raw = odor.read_u16()
    waste_raw = waste.read_u16()

    odor_level = int(((65535 - odor_raw) / 65535) * 100)
    waste_level = int(((65535 - waste_raw) / 65535) * 100)

    return {
        "usage": usage_count,
        "odor": odor_level,
        "waste": waste_level
    }

# Layer 2: AI Index with Weights
def calculate_ai_index(data):

    # AI-inspired weighted scoring
    odor_w = 0.5
    waste_w = 0.3
    usage_w = 0.2

    odor_score = data["odor"]
    waste_score = data["waste"]
    usage_score = 100 if data["usage"] else 20

    ai_score = (
        (odor_score * odor_w)
        + (waste_score * waste_w)
        + (usage_score * usage_w)
    )

    if ai_score >= 75:
        return "HEALTHY", ai_score
    elif ai_score >= 50:
        return "MONITOR", ai_score
    elif ai_score >= 25:
        return "WARNING", ai_score
    else:
        return "CRITICAL", ai_score

# Layer 3: Smart Nation Layer
def generate_national_status(status, score):

    actions = {
        "HEALTHY": [
            "Continue monitoring",
            "Routine maintenance"
        ],
        "MONITOR": [
            "Schedule inspection",
            "Increase frequency"
        ],
        "WARNING": [
            "Urgent cleaning required",
            "Alert supervisor"
        ],
        "CRITICAL": [
            "Immediate action",
            "Block facility"
        ]
    }

    return actions.get(status, [])

def run_demo():

    scenarios = [
        (1, 80, 85),
        (0, 50, 40),
        (1, 20, 15)
    ]

    for usage_val, odor_level, waste_level in scenarios:

        data = {
            "usage": usage_val,
            "odor": odor_level,
            "waste": waste_level
        }

        status, score = calculate_ai_index(data)
        actions = generate_national_status(status, score)

        print(
            f"\nUsage: {usage_val}, "
            f"Odor: {odor_level}%, "
            f"Waste: {waste_level}%"
        )

        print(f"AI Score: {score:.1f} - {status}")

        for action in actions:
            print(f"  - {action}")

run_demo()

How the Code Works

Code Section What It Does Why It Matters
Starter Code Implements one layer or subsystem of the prototype Separating the project into parts makes expert systems easier to test and improve

Expected Result

The system should collect data, process it, control the required outputs, and show clear evidence that the prototype solves the intended problem.


Troubleshooting

Problem Possible Cause Solution
No readings appear in Thonny Incorrect wiring, wrong pins, or missing library Recheck the circuit, confirm pin numbers, and test each module separately
Import error appears Library file is missing or saved in the wrong location Check the Pico file structure and save libraries exactly as instructed
Actuator does not respond Relay/driver wiring or external power issue Verify driver input, external power, and shared ground where required
Results are unstable Sensor noise, loose wiring, or poor calibration Secure the wiring and add calibration or averaging in code
Prototype works only sometimes System is too complex to test all at once Return to subsystem testing and integrate one part at a time