Skip to main content

How to Build IoT Smart Home System with Raspberry Pi Pico W in Wokwi: Complete Tutorial

IoT Smart Home System with Raspberry Pi Pico W | MakeMindz
Beginner–Intermediate · MicroPython · Wokwi

Build an IoT Smart Home System with Raspberry Pi Pico W

Distance sensing, light detection, buzzer alerts & LED indicators — fully simulated in Wokwi with complete MicroPython code.

Open Free Simulation
🏠

Project Overview

This tutorial builds a complete IoT Smart Home Monitoring System using the Raspberry Pi Pico W — all simulated in Wokwi, so no physical hardware is needed. The system integrates multiple sensors and output devices to mimic real-world home automation and security.

Measure distance (security / parking)
Detect intruders via ultrasonic sensing
Monitor light intensity (LDR)
Buzzer alarm activation
LED visual status indicators
WiFi-ready IoT architecture
Night watch & day safe modes
Priority-based security logic
💡 What You Will Learn How ultrasonic echo timing works · ADC on Pico W · Priority-based security logic · PWM buzzer control · Multi-sensor embedded systems · Wokwi IoT simulation · MicroPython code structure
🔧

Components Used

# Component Role Type
1 Raspberry Pi Pico W Main controller with built-in WiFi Controller
2 HC-SR04 Ultrasonic Sensor Distance measurement (2 cm – 400 cm) Sensor
3 LDR (Photoresistor) Ambient light detection via ADC Sensor
4 Green LED Safe / night watch status indicator Output
5 Red LED Intruder alert indicator Output
6 Buzzer PWM-driven audio alarm Output
7 Relay Module Control high-voltage devices (optional) Passive
8 Breadboard & Jumper Wires Circuit connections Passive
📌

Wiring & Pin Map

All connections at a glance. Wire each component to the corresponding GPIO pin on the Pico W.

Ultrasonic TRIGGP16
Ultrasonic ECHOGP17
Ultrasonic VCC3V3
Ultrasonic GNDGND.7
Red LED (Anode)GP14
Green LED (Anode)GP15
LED CathodesGND.4
Buzzer (+)GP13
Buzzer (–)GND.4
LDR AOGP26 (ADC0)
LDR VCC3V3
LDR GNDGND.6
📐

Circuit Diagram (diagram.json)

Paste this diagram.json into your Wokwi project to set up all parts and connections instantly.

MAIN CONTROLLER
Raspberry Pi Pico W
MicroPython v1.24.1 · Built-in WiFi
📡
HC-SR04
Ultrasonic Sensor
TRIG→GP16 | ECHO→GP17
💡
LDR
Photoresistor
AO→GP26 (ADC0)
🟢
Green LED
Safe Indicator
A→GP15 | C→GND
🔴
Red LED
Alert Indicator
A→GP14 | C→GND
🔔
Buzzer
PWM Audio Alarm
+→GP13 | –→GND
Power Rails
3V3 & GND
All sensors & outputs
All wires shown in Wokwi simulation · Open simulation to view interactive circuit
diagram.json
{
  "version": 1,
  "author": "MakeMindz",
  "editor": "wokwi",
  "parts": [
    {
      "type": "board-pi-pico-w",
      "id": "pico",
      "top": 0,
      "left": 0,
      "attrs": { "env": "micropython-20241129-v1.24.1" }
    },
    {
      "type": "wokwi-led",
      "id": "led1",
      "top": 92.4,
      "left": -178.6,
      "attrs": { "color": "green" }
    },
    {
      "type": "wokwi-led",
      "id": "led2",
      "top": 92.4,
      "left": -140.2,
      "attrs": { "color": "red" }
    },
    {
      "type": "wokwi-hc-sr04",
      "id": "ultrasonic1",
      "top": -56.1,
      "left": 82.3,
      "attrs": {}
    },
    {
      "type": "wokwi-buzzer",
      "id": "bz1",
      "top": 79.2,
      "left": -94.2,
      "attrs": { "volume": "0.1" }
    },
    {
      "type": "wokwi-photoresistor-sensor",
      "id": "ldr1",
      "top": 127.8,
      "left": 122.8,
      "rotate": 180,
      "attrs": {}
    }
  ],
  "connections": [
    [ "pico:GND.4",     "led1:C",          "black", [ "h0" ] ],
    [ "led2:C",          "pico:GND.4",     "green", [ "v0" ] ],
    [ "bz1:1",           "pico:GND.4",     "green", [ "v0" ] ],
    [ "bz1:2",           "pico:GP13",      "green", [ "v0" ] ],
    [ "led2:A",          "pico:GP14",      "green", [ "v0" ] ],
    [ "led1:A",          "pico:GP15",      "green", [ "v0" ] ],
    [ "pico:GP26",       "ldr1:AO",        "green", [ "h0" ] ],
    [ "pico:GND.6",      "ldr1:GND",       "black", [ "h32.66", "v35.15" ] ],
    [ "pico:3V3",        "ldr1:VCC",       "green", [ "h23.06", "v121.55" ] ],
    [ "ultrasonic1:VCC", "pico:3V3",       "red",   [ "v19.2" ] ],
    [ "ultrasonic1:TRIG","pico:GP16",      "green", [ "v163.2", "h-0.4" ] ],
    [ "ultrasonic1:ECHO","pico:GP17",      "green", [ "v0" ] ],
    [ "pico:GND.7",      "ultrasonic1:GND","black", [ "h0" ] ]
  ],
  "dependencies": {}
}
⚙️

System Logic Explained

The system uses a priority-based control structure — the same pattern found in professional security automation. Security always takes precedence over environment mode.

🚨 Priority 1 — Intruder Alert

Triggered when distance < 50 cm

  • Red LED turns ON
  • Green LED turns OFF
  • Buzzer activates (PWM @ 1 kHz)
  • Serial monitor prints alert
🌙 Priority 2 — Night Watch

Triggered when LDR value > 30000 (dark)

  • Red LED OFF
  • Buzzer OFF
  • Green LED blinks (0.5s)
  • System enters monitoring mode
☀️ Priority 3 — Day Safe Mode

Triggered when light intensity is high

  • Red LED OFF
  • Buzzer OFF
  • Green LED stays ON
  • Normal operation
💡 How Ultrasonic Distance Works The HC-SR04 fires a 10 µs pulse on TRIG. The sensor sends ultrasonic bursts and the ECHO pin goes HIGH for the duration the sound takes to return. Distance = (echo duration × 0.0343) ÷ 2 (in cm).
🐍

Complete MicroPython Code

Copy and paste this into main.py in your Wokwi project or on your physical Pico W.

main.py — MicroPython
from machine import Pin, ADC, PWM
import time

# ──────────────────────────────────
# Hardware Configuration
# ──────────────────────────────────

# Ultrasonic Sensor (HC-SR04)
trigger = Pin(16, Pin.OUT)
echo    = Pin(17, Pin.IN)

# LDR connected to ADC pin GP26
ldr = ADC(26)  # GP26 = ADC0

# LEDs
red_led   = Pin(14, Pin.OUT)
green_led = Pin(15, Pin.OUT)

# Buzzer — PWM for audible tone
buzzer = PWM(Pin(13))
buzzer.freq(1000)  # 1 kHz

# ──────────────────────────────────
# Thresholds (tune as needed)
# ──────────────────────────────────
INTRUDER_DISTANCE = 50     # cm — trigger alert below this
DARK_THRESHOLD    = 30000  # LDR ADC value — higher = darker

# ──────────────────────────────────
# Ultrasonic Distance Measurement
# ──────────────────────────────────
def measure_distance():
    trigger.low()
    time.sleep_us(2)
    trigger.high()
    time.sleep_us(10)
    trigger.low()

    while echo.value() == 0:
        start = time.ticks_us()

    while echo.value() == 1:
        end = time.ticks_us()

    duration = time.ticks_diff(end, start)
    distance = (duration * 0.0343) / 2
    return distance

print("Security System Initialized")
print("──────────────────────────")

# ──────────────────────────────────
# Main Loop
# ──────────────────────────────────
while True:
    distance    = measure_distance()
    light_value = ldr.read_u16()

    print(f"Distance: {distance:.1f} cm | LDR: {light_value}")

    # 🚨 PRIORITY 1: INTRUDER ALERT
    if distance < INTRUDER_DISTANCE:
        print("🚨 INTRUDER DETECTED!")
        red_led.value(1)
        green_led.value(0)
        buzzer.duty_u16(30000)  # Buzzer ON

    # 🌗 PRIORITY 2 & 3: ENVIRONMENT MODE
    else:
        red_led.value(0)
        buzzer.duty_u16(0)      # Buzzer OFF

        if light_value > DARK_THRESHOLD:
            print("🌙 Night Watch Mode")
            green_led.toggle()   # Blink
            time.sleep(0.5)
        else:
            print("☀️  Day Safe Mode")
            green_led.value(1)  # Steady ON
            time.sleep(0.5)
📝

Step-by-Step Build Guide

1
Open Wokwi and Create a New Project
Go to wokwi.com and create a new project. Select Raspberry Pi Pico W as your board and choose MicroPython as the language.
2
Add diagram.json
Click the diagram.json tab in Wokwi and replace the contents with the diagram.json code above. This adds all components (Pico W, HC-SR04, LDR, LEDs, buzzer) and their wired connections automatically.
3
Add the MicroPython Code
Click the main.py tab and paste the complete code from Step 4 above. The code sets up GPIO pins, defines the measure_distance() function, and runs the main monitoring loop.
4
Run the Simulation
Click the green ▶ Play button. The serial monitor will print distance and LDR readings. You should see "Day Safe Mode" or "Night Watch Mode" messages depending on the simulated light level.
5
Test the Intruder Alert
In the Wokwi simulation, click on the HC-SR04 sensor and adjust the distance slider to below 50 cm. The red LED should light up, the buzzer should activate, and the serial monitor prints "🚨 INTRUDER DETECTED!"
6
Test Night Watch Mode
Click on the LDR sensor in Wokwi and drag the light level slider to a low value (simulating darkness). The green LED will begin blinking and the serial monitor will print "🌙 Night Watch Mode".
7
Tune Thresholds (Optional)
Adjust INTRUDER_DISTANCE and DARK_THRESHOLD in the code to suit your real-world environment. Higher DARK_THRESHOLD values make the system more sensitive to low-light conditions.
8
Deploy to Real Hardware (Optional)
Flash MicroPython firmware to your physical Pico W using Thonny IDE. Wire the components as per the pin map, copy main.py to the board, and run. The system works identically on real hardware.

© 2026 MakeMindz · Raspberry Pi Pico W IoT Tutorials · Built for makers and learners

Comments

try for free