Skip to content

Project 2.3.11: Bluetooth_motion_light

Intermediate Embedded Systems Project Using Raspberry Pi Pico 2 W and MicroPython


Overview

This project builds a motion-activated light that can be armed or disarmed from a phone over BLE. A PIR sensor detects movement, the Pico applies decision logic, and an LED provides local lighting.

The system uses edge detection and a timed hold so the light reacts to new motion events and avoids constant retriggering.


Required Components

PIR motion sensor module (HC-SR501 or similar)
PIR motion sensor module (HC-SR501 or similar)
Jumper wires
Jumper wires
Raspberry Pi Pico 2 W
Raspberry Pi Pico 2 W
LED
LED
220Ω resistor
220Ω resistor
Breadboard
Breadboard

Phone with BLUETOOTH app

Circuit Connections

Component terminal Connect to Pico physical pin Notes
PIR VCC VBUS / 5V Physical pin 40 Use 5V only if the PIR requires it and the OUT pin is 3.3V safe
PIR GND GND Physical pin 38 Common ground
PIR OUT GPIO 4 GPIO 4 / physical pin 6 Digital motion signal (edge-detected)
LED anode (+) 220Ω resistor then GPIO 0 GPIO 0 / physical pin 1 Local light output
LED cathode (-) GND Physical pin 38 Common ground

Wiring Diagram

Circuit Diagram

Raspberry Pi Pico 2 W
┌─────────────────────┐
│                     │
│  GPIO 4  ───────────┤──── PIR OUT (motion)
│  VBUS/5V ───────────┤──── PIR VCC (if required)
│  GND     ───────────┤──── PIR GND
│  GPIO 0  ───────────┤──── 220Ω → LED anode
│  GND     ───────────┤──── LED cathode
│                     │
└─────────────────────┘

PIR dome points to detection area; allow warm-up time before testing.

Step-by-Step Assembly

  1. Place the Raspberry Pi Pico 2W on the breadboard across the center gap.
  2. Position the PIR module so the dome faces the detection area and identify VCC/GND/OUT.
  3. Connect PIR VCC to VBUS/5V only if the module requires it and its OUT is 3.3V safe.
  4. Connect PIR GND to Pico GND.
  5. Connect PIR OUT to GPIO 4.
  6. Place the LED, connect its anode through a 220Ω resistor to GPIO 0 and its cathode to GND.
  7. Verify wiring and give the PIR time to warm up before testing.

Wiring Check

  • [x] Pico 2W placed correctly across the breadboard gap
  • [x] PIR VCC connected only if module requires 5V and OUT is 3.3V safe
  • [x] PIR GND connected to Pico GND
  • [x] PIR OUT connected to GPIO 4
  • [x] LED anode connected through 220Ω to GPIO 0 and cathode to GND
  • [x] No loose jumper wires

Testing Individual Components

  • Build the PIR and LED circuit and power the Pico.
  • Wait ~30 seconds for the PIR warm-up before evaluating motion readings.
  • Run a quick script to print pir.value() and confirm the sensor output changes when you move.
  • Run an LED blink test to verify resistor and polarity.

Full Project Code

from machine import Pin
import bluetooth
import time
from ble_uart import BLEUART

DEVICE_NAME = "AIDER_MotionLight"
LED_ON_TIME_MS = 5000
PIR_WARMUP_MS = 30000

led = Pin(0, Pin.OUT)
pir = Pin(4, Pin.IN)

ble = bluetooth.BLE()
ble.active(True)
uart = BLEUART(ble, name=DEVICE_NAME)

system_armed = True
led_until = 0
last_motion = 0
command_queue = []

def send_line(message):
    print(message)
    uart.write((message + "\n").encode())

def on_rx(data):
    command = data.decode("utf-8").strip().lower()
    if command:
        command_queue.append(command)

def report_status():
    armed_text = "ARMED" if system_armed else "DISARMED"
    led_text = "ON" if led.value() else "OFF"
    send_line("System: {} | LED: {}".format(armed_text, led_text))

uart.on_rx(on_rx)
send_line("Bluetooth motion light ready")
send_line("Wait {} seconds for PIR warm-up.".format(PIR_WARMUP_MS // 1000))
time.sleep_ms(PIR_WARMUP_MS)

while True:
    now = time.ticks_ms()
    motion = pir.value()

    # Edge detection: trigger only on rising edge
    if system_armed and motion == 1 and last_motion == 0:
        led_until = time.ticks_add(now, LED_ON_TIME_MS)
        send_line("Motion detected. Light timer restarted.")

    # LED on while timer active
    if system_armed and time.ticks_diff(led_until, now) > 0:
        led.on()
    else:
        led.off()

    # Handle incoming BLE commands
    if command_queue:
        command = command_queue.pop(0)
        if command in ("arm", "on"):
            system_armed = True
            send_line("System armed.")
        elif command in ("disarm", "off"):
            system_armed = False
            led.off()
            send_line("System disarmed.")
        elif command == "status":
            report_status()
        elif command == "help":
            send_line("Commands: arm, disarm, on, off, status, help")
        else:
            send_line("Unknown command. Send help.")

    last_motion = motion
    time.sleep_ms(100)

How the Code Works

Code Section What It Does Why It Matters
BLEUART setup Starts BLE UART service and sets device name Lets students connect from a BLE UART app
PIR warm-up Waits ~30s for sensor stabilization Prevents false triggers during startup
Edge detection Triggers on PIR rising edge only Avoids repeated retriggers while PIR is high
led_until timer Keeps LED on for a fixed hold time after motion Creates expected motion-light behaviour
Command queue Queues BLE text commands for main loop handling Keeps wireless input non-blocking

Expected Result

After warm-up, the LED turns on for the configured hold time when new motion is detected while the system is armed. A phone connected via the BLE app can arm, disarm, and request status.


Troubleshooting

Problem Possible cause Solution
LED never turns on LED polarity reversed or resistor path broken Reconnect LED anode to GPIO 0 via 220Ω and cathode to GND
Motion is detected constantly PIR still warming up or facing heat source Wait longer, move PIR away from warm airflow, retest
Motion does not trigger PIR OUT on wrong pin or module power incorrect Check PIR OUT wiring to GPIO 4 and module supply voltage
Phone cannot find BLE device Pico not advertising or helper files missing Confirm ble_uart.py and ble_advertising.py on Pico and restart
Commands ignored Phone connected to wrong service or text differs Reconnect to BLE UART service and send exact commands