Skip to content

Project 4.1.2: Smart Sanitation For Smart Cities

Overview

Build and prototype a smart sanitation system for smart cities 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

Jumper Wires
Jumper wires
Breadboard
Breadboard
Gas Sensor
Odor / air-quality sensor
Ultrasonic Sensor
Usage sensor
Pico 2 W
Raspberry Pi Pico 2 W
USB cable

Components

Circuit Connections

Component Pin Connects To Pico GPIO / Physical Pin Number Notes
VCC 3.3V See source design notes Verify pin compatibility and voltage level
GND GND See source design notes Verify pin compatibility and voltage level
OUT GPIO14 (Pin 19) See source design notes Verify pin compatibility and voltage level
VCC 3.3V See source design notes Verify pin compatibility and voltage level
GND GND See source design notes Verify pin compatibility and voltage level
AOUT ADC0 (Pin 26) See source design notes Verify pin compatibility and voltage level

Step-by-Step Assembly

Hardware Setup

Step 1: Connect Usage Sensor (Digital)

  1. VCC → 3.3V
  2. GND → GND
  3. OUT → GPIO14 (Pin 19)

Step 2: Connect Odor Sensor (Analog)

  1. VCC → 3.3V
  2. GND → GND
  3. AOUT → ADC0 (Pin 26)

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


# Smart Sanitation for Smart Cities - Project 352

# Three-layer IoT Architecture

from machine import Pin, ADC
import time

# Layer 1: Facility Node Sensors
usage_sensor = Pin(14, Pin.IN)
odor_sensor = ADC(Pin(26))

def read_facility_data():
    """Layer 1: Read usage and odor sensors"""

    usage_count = usage_sensor.value()

    # Read odor level
    odor_raw = odor_sensor.read_u16()
    odor_level = int(((65535 - odor_raw) / 65535) * 100)

    return {
        "usage_detected": usage_count,
        "odor_level": odor_level,
        "timestamp": time.time()
    }

def assess_sanitation(data):
    """Layer 2: Sanitation condition logic"""

    odor = data["odor_level"]

    if odor < 30:
        return "SERVICE_REQUIRED", "CRITICAL"
    elif odor < 40:
        return "SERVICE_REQUIRED", "MODERATE"
    elif odor < 60:
        return "MONITOR", "OK"
    else:
        return "GOOD", "OK"

def generate_facility_status(facility_id, data, condition):
    """Layer 3: Dashboard status"""

    status, alert = condition

    return {
        "facility_id": facility_id,
        "status": status,
        "alert_level": alert,
        "last_service": time.time() - 3600,
        "next_action":
            "SCHEDULE_SERVICE"
            if status == "SERVICE_REQUIRED"
            else "MONITOR"
    }

# Main loop
def start_sanitation_system():

    facility_id = "FACILITY_001"

    while True:

        # Layer 1: Collect
        data = read_facility_data()

        # Layer 2: Process
        condition = assess_sanitation(data)

        # Layer 3: Output
        status = generate_facility_status(
            facility_id,
            data,
            condition
        )

        print(f"Facility: {facility_id}")
        print(f" Usage: {data['usage_detected']}")
        print(f" Odor: {data['odor_level']}%")
        print(f" Status: {status['status']}")
        print(f" Alert: {status['alert_level']}")
        print("-" * 40)

        time.sleep(10)

start_sanitation_system()

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