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 SimulationProject 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.
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.
Circuit Diagram (diagram.json)
Paste this diagram.json into your Wokwi project to set up all parts and connections instantly.
{
"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.
Triggered when distance < 50 cm
- Red LED turns ON
- Green LED turns OFF
- Buzzer activates (PWM @ 1 kHz)
- Serial monitor prints alert
Triggered when LDR value > 30000 (dark)
- Red LED OFF
- Buzzer OFF
- Green LED blinks (0.5s)
- System enters monitoring mode
Triggered when light intensity is high
- Red LED OFF
- Buzzer OFF
- Green LED stays ON
- Normal operation
Complete MicroPython Code
Copy and paste this into main.py in your Wokwi project or on your physical Pico W.
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
diagram.json code above. This adds all components (Pico W, HC-SR04, LDR, LEDs, buzzer) and their wired connections automatically.measure_distance() function, and runs the main monitoring loop.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.main.py to the board, and run. The system works identically on real hardware.
Comments
Post a Comment