A small python script to control an SSR output device (e.g. an LED) in response to motion from a PIR motion sensor
It's best to install python dependencies in a python virtual environment:
# Create virtual env
python3 -m venv venv
# Activate virtual env
. venv/bin/activate
# Install dependencies
pip install -r requirements.txtTo deactivate the virtual env just run:
deactivateFlake8 is used for code linting and included in the requirements.txt
The script can be configured using the following environment variables:
- PIN_PIR
- GPIO pin for the PIR motion sensor input
- default: 18
- PIN_SSR
- GPIO pin for the SSR output
- default: 17
- MIN_ACTIVATION_SECS
- Minimum time in seconds to keep SSR active after motion is detected
- default: 10
- DRY_RUN
- Dry run mode for testing without actual GPIO hardware. Simulates random motion events.
- default: 0
- Install dependencies as described in the Setup section
- Configure environment variables if required e.g:
export DRY_RUN=1 - Run script e.g.
python uvbox.py
The script runs in a loop, checking for motion every second.
If motion is detected and the SSR is not currently active, the SSR is turned on and the script blocks for the minimum duration.
After the minmum duration has elapsed, the loop continues to the next iteration.
If motion is still detected on the next iteration of the loop, the SSR will remain on.
If no motion is detected, the SSR is turned off, assuming it's currently active.
This prevents the SSR device turning off and immediately turning back on while continuous motion is detected.
Dry run mode helps with testing when you don't have access to physical GPIO devices. Motion is simulated at random intervals (between 1 and 20 seconds) for random durations (between 1 and 10 seconds).
To run the script in dry run mode:
DRY_RUN=1 python uvbox.py