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
- Raspberry Pi Pico W - Main microcontroller with WiFi
- HC-SR04 Ultrasonic Sensor - Distance measurement (2cm to 400cm)
- Relay Module - Control AC/DC devices (lights, fans, etc.)
- Green LED - Status indicator
- Red LED - Alert/warning indicator
- Buzzer - Audio alarm
- 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)
- HC-SR04 Ultrasonic Sensor
- Search: "HC-SR04" or "ultrasonic"
- Add to workspace
- Relay Module
- Search: "relay"
- Select single-channel relay module
- LEDs (add 2)
- Search: "LED"
- Add one green and one red LED
- Buzzer
- Search: "buzzer" or "piezo"
- Add piezo buzzer
- 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:
- HC-SR04 VCC → VBUS (5V) on Pico W (Red wire)
- HC-SR04 GND → GND on Pico W (Black wire)
- Relay VCC → VBUS (5V) on Pico W (Red wire)
- Relay GND → GND 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:
- TRIG → GP2 on Pico W (Green wire)
- ECHO → GP3 on Pico W (Green wire)
Relay Control Connection:
- Relay IN → GP15 on Pico W (Green wire)
LED Connections:
- Green LED Anode (+) → GP13 on Pico W (Green wire)
- Green LED Cathode (-) → GND on Pico W
- Red LED Anode (+) → GP14 on Pico W (Green wire)
- Red LED Cathode (-) → GND on Pico W
Buzzer Connection:
- Buzzer (+) → GP16 on Pico W (Green wire)
- 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
Post a Comment