SR501 - Passive infrared sensor
Introduction
The SR501 is a passive infrared (PIR) sensor that can be used to detect movement of humans and animals through changes in the light or heat levels. The sensor has a simple digital output that is 0 ordinarily and changes to 1 when motion is detected. The simplicity of the SR602 in operation means that no external library is required to read data from the device.
On the SR501 are two potentiometers that adjust the sensitivity and the time delay (details can be found in the data sheet and a tutorial at Last Minute Engineers). Where the SR501 is deployed in devices the time delay potentiometer is turned as far anticlockwise as possible to minimise the time delay, and the sensitivity potentiometer is set to the middle of its range. There is also a jumper that can be set so that the device outputs a high signal while it continues to detect movement (default), or so that the high signal is output for the time set by the time delay potentiometer. The default, or retriggering mode is used.
Application: software and function
The SR501 PIR is used as a component in a ‘sentinel’ to detect motion near the display, and is a candidate for use in a later revision of the sensor board.
The following code can be used to testing the SR501 with the output connected to pin 2 on an Arduino:
// Copyright (C) 2021 Simon Butler
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject
// to the following conditions:
//
// The above copyright notice and this permission notice (including the
// next paragraph) shall be included in all copies or substantial
// portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
// SPDX-License-Identifier: MIT
int pir_status = 0;
int pir_output = 0;
int motion_detected = 0;
void setup() {
pinMode(2, INPUT);
Serial.begin(9600);
// may need to wait for 30-60 seconds for unit to acclimatise to
// ambient light conditions
Serial.print("Initialising ");
for ( int i = 0; i < 30; i++ ) {
delay(1000);
Serial.print(".");
}
Serial.println();
}
void loop() {
pir_output = digitalRead(2);
if (pir_output != 0) {
if (motion_detected == 0) {
Serial.println("PIR detected movement");
motion_detected = 1;
}
else {
// should occur with the H - retriggering - setting
Serial.println("PIR still detecting movement");
}
}
else {
if (motion_detected == 1) {
Serial.println("Motion over");
motion_detected = 0;
}
else {
Serial.println(".");
}
}
delay(250);
}