Skip to content

Project 3.1.20: Occupancy Light

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


Overview

The Pico turns a light on when occupancy is detected and keeps it on briefly after the area becomes empty.

Lights are often left on unnecessarily or switched off too soon when occupancy is not tracked well.

A Pico 2 W prototype with a PIR sensor, output control, and status LED.

Occupancy detection, hold-time logic, and safe output testing.

Required Components

Raspberry Pi Pico 2 W
Raspberry Pi Pico 2 W
PIR motion sensor
PIR motion sensor
Breadboard
Breadboard
Jumper wires
Jumper wires
LED
LED
Resistor
Resistor

Circuit Connections

Component terminal Connect to Pico physical pin Notes
PIR OUT GPIO 14 GPIO 14 / physical pin 19 Occupancy input
Output control GPIO 15 GPIO 15 / physical pin 20 Relay or light output
Status LED anode 220 ohm resistor to GPIO 16 GPIO 16 / physical pin 21 Occupancy status LED

Wiring Diagram

Circuit Diagram

  PIR OUT            -> GPIO 14
  GPIO 15            -> relay IN or low-power light control
  GPIO 16            -> 220 ohm resistor -> status LED anode
  Common GND         -> PIR, output stage, and LED

Step-by-Step Assembly

  1. Connect the PIR output to GPIO 14 and allow the sensor to warm up before testing.
  2. Connect the relay input or low-power light control line to GPIO 15.
  3. Wire the status LED to GPIO 16 through a 220 ohm resistor.
  4. If a relay or external lamp supply is used, confirm the ground and power wiring safely before testing.

Testing Individual Components

Before running the full project, test each subsystem separately. This makes it easier to find wiring, library, or logic problems before full integration.

  1. Hardware setup: Assemble the Pico, sensor, indicator, and load wiring exactly as shown in the connection table before applying power.
  2. Test the input sensor: Test the PIR input so you know exactly when motion is being detected.
  3. Test the output device: Test the output device and status LED independently before running the occupancy logic.
  4. Test the decision logic: Walk in and out of the detection zone and confirm the hold time prevents rapid switching.
  5. Run the full system: Run the full controller and compare the printed state with the actual output timing.
  6. Validate the prototype: Adjust PIR placement and hold time to reduce false activations or annoying shutoff behavior.
  7. Save the project: Save the validated program on the Pico as main.py and keep a copy on the computer for future edits.

Full Project Code

After completing and checking the circuit connections, open Thonny IDE, copy and paste this code into a new file or upload the project file to the Raspberry Pi Pico 2 W, then run it from Thonny.

from machine import Pin
import time

PIR_PIN = 14
OUTPUT_PIN = 15
LED_PIN = 16
HOLD_SECONDS = 20

pir = Pin(PIR_PIN, Pin.IN, Pin.PULL_DOWN)
output = Pin(OUTPUT_PIN, Pin.OUT)
led = Pin(LED_PIN, Pin.OUT)
last_motion_time = 0

print('Occupancy light controller ready')

while True:
    motion = pir.value() == 1
    now = time.time()
    if motion:
        last_motion_time = now

    active = motion or (now - last_motion_time) < HOLD_SECONDS
    output.value(1 if active else 0)
    led.value(1 if active else 0)
    print('Motion:{} Active:{}'.format(motion, active))
    time.sleep(0.2)

How the Code Works

Code Section What It Does Why It Matters What to Modify During Testing
Motion timestamp Remembers the last time motion was seen The hold-time logic depends on accurate motion timing Watch the printed motion state while walking through the area
Hold-time rule Keeps the output active for a fixed time after movement stops This avoids lights turning off the moment a person stands still Shorten or lengthen the hold time during user testing
Mirrored status LED Shows when the controller considers the area active The LED helps validate timing even when the real load is disconnected Use the LED during safe dry-run testing before attaching a real load

Expected Result

The serial monitor reports the current reading or state clearly.

The hardware output responds when the decision logic changes state.

Subsystem behavior matches the thresholds, timing, or rules described in the document.

Validation Checks

  • Normal condition test: confirm the system stays in its safe or idle state under baseline conditions
  • Warning condition test: move the input close to the chosen threshold and confirm the transition is sensible
  • Critical condition test: trigger the strongest alert or control state and confirm the output response is correct
  • Calibration test: compare the sensor or timing against a known reference or repeated trial
  • Limitation test: deliberately create an awkward or noisy condition and note how the prototype behaves

Deployment and Limitations

  • This prototype is useful for local indicator boards and classroom control panels
  • Before deployment, labeling, enclosure design, and user training need improvement
  • Status tools only work well when users trust the meaning of each state and keep the system updated

Troubleshooting

Problem Possible Cause Solution
The light never turns off The PIR keeps retriggering or the hold time is too long Aim the sensor more narrowly and reduce the hold time
The light turns off too soon The hold time is too short or motion is not being detected well Increase HOLD_SECONDS and improve PIR placement
The relay or light never turns on The output wiring is wrong Test GPIO 15 with a simple on-off script before retrying the full logic
The PIR seems random The sensor is still warming up or covering too wide an area Wait for warm-up and re-aim the PIR toward the entry zone