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

 

This comprehensive tutorial shows you how to create a complete IoT smart home monitoring system using Raspberry Pi Pico W with multiple sensors and actuators in the Wokwi simulator. This project includes ultrasonic sensor (HC-SR04), relay module, LEDs, and buzzer - perfect for learning IoT and home automation!

Project Overview

This smart home system can:

  • Measure distance with ultrasonic sensor (parking assist, security)
  • Control high-voltage devices via relay module
  • Provide visual feedback with LEDs
  • Sound alerts with buzzer
  • Connect to WiFi for IoT functionality (Pico W)

Components in This Project

  1. Raspberry Pi Pico W - Main microcontroller with WiFi
  2. HC-SR04 Ultrasonic Sensor - Distance measurement (2cm to 400cm)
  3. Relay Module - Control AC/DC devices (lights, fans, etc.)
  4. Green LED - Status indicator
  5. Red LED - Alert/warning indicator
  6. Buzzer - Audio alarm
  7. Breadboard and wires - Connections

Getting Started with Wokwi

Step 1: Create New Project

  • Go to https://wokwi.com
  • Click "New Project"
  • Select "Raspberry Pi Pico W" (important: use Pico W for WiFi)

Step 2: Add All Components



Click the "+" button and add each component:
  1. HC-SR04 Ultrasonic Sensor
    • Search: "HC-SR04" or "ultrasonic"
    • Add to workspace
  1. Relay Module
    • Search: "relay"
    • Select single-channel relay module
  1. LEDs (add 2)
    • Search: "LED"
    • Add one green and one red LED
  1. Buzzer
    • Search: "buzzer" or "piezo"
    • Add piezo buzzer
  1. Breadboard (optional for organization)
    • Helps organize connections

Understanding the Pin Connections

HC-SR04 Ultrasonic Sensor (4 pins):

  • VCC - Power (5V or 3.3V)
  • TRIG - Trigger pin (send ultrasonic pulse)
  • ECHO - Echo pin (receive reflected pulse)
  • GND - Ground

Relay Module (3-4 pins):

  • VCC - Power (5V recommended)
  • GND - Ground
  • IN - Control signal (LOW=ON, HIGH=OFF typically)
  • COM, NO, NC - Switch terminals (not used in simulation)

LEDs:

  • Anode (+) - Longer leg (connect to GPIO via resistor)
  • Cathode (-) - Shorter leg (connect to GND)

Buzzer:

  • Positive (+) - Signal pin
  • Negative (-) - Ground

Step 3: Wire the Circuit in Wokwi

Based on your diagram, make these connections:

Power Connections:

  1. HC-SR04 VCCVBUS (5V) on Pico W (Red wire)
  2. HC-SR04 GNDGND on Pico W (Black wire)
  3. Relay VCCVBUS (5V) on Pico W (Red wire)
  4. Relay GNDGND on Pico W (Black wire)
Diagram.json:
{
  "version": 1,
  "author": "Navya Sugamanchi",
  "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": {}
}

HC-SR04 Signal Connections:

  1. TRIGGP2 on Pico W (Green wire)
  2. ECHOGP3 on Pico W (Green wire)

Relay Control Connection:

  1. Relay INGP15 on Pico W (Green wire)

LED Connections:

  1. Green LED Anode (+)GP13 on Pico W (Green wire)
  2. Green LED Cathode (-)GND on Pico W
  3. Red LED Anode (+)GP14 on Pico W (Green wire)
  4. Red LED Cathode (-)GND on Pico W

Buzzer Connection:

  1. Buzzer (+)GP16 on Pico W (Green wire)
  2. Buzzer (-)GND on Pico W

Note: In real hardware, add 220Ω-330Ω resistors in series with LEDs. In Wokwi simulation, this is optional.

Step 4: Complete System Code

Here's the full code for the smart home system:

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 (ADC)
ldr = ADC(26)  # GP26 = ADC0

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

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

# -------------------------------
# Thresholds
# -------------------------------
INTRUDER_DISTANCE = 50      # cm
DARK_THRESHOLD = 30000      # LDR ADC value (tune if needed)

# -------------------------------
# Ultrasonic Distance Function
# -------------------------------
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)  # Turn buzzer ON

    # 🌗 PRIORITY 2: ENVIRONMENT STATUS
    else:
        red_led.value(0)
        buzzer.duty_u16(0)      # Turn buzzer OFF

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

Comments