I am trying to make the value of PIR sensor value HIGH for certain time using code. SR505 have a default time delay of 8 seconds. But I want to increase it to 20 seconds.

I am thinking of increasing it using TICKER library. I have tried and tested a basic code and seems to be working fine. But I want to implement it with Ticker which will keep the value HIGH for 20 seconds as soon as the timer goes above 20 seconds and PIR is not detecting the motion then it will go set again the timer to 0.

Here is how my code looks like:

#include <Ticker.h>

#define SIGNAL_PIN D2

Ticker flipper;

void setup()
  digitalWrite (SIGNAL_PIN, LOW);


void flip(){
  Serial.println("From callback");
  digitalWrite (SIGNAL_PIN, HIGH);

void loop() {
  if(digitalRead(SIGNAL_PIN)==HIGH) {
    Serial.println("Movement detected.");
    flipper.attach(1, flip);
  } else {
    Serial.println("Did not detect movement.");

Thank you!

  • Why do you feel the need to use the Ticker library? There is absolutely no need. All you need is a millis() timestamp.
    – Majenko
    May 1, 2019 at 9:35
  • Ok but I never used millis() before. How that work I will just check. If you have any example based on the same scenarios that will be amazing. May 1, 2019 at 9:37
  • I'm writing one for you as we speak.
    – Majenko
    May 1, 2019 at 9:37

1 Answer 1


You really don't need the Ticker library. Just maintain a timestamp of the last time the PIR was seen to be HIGH:

void loop() {
    static uint32_t lastSeenHigh = millis();
    static bool pirState = false;

    // If it's active then record that fact along with the time
    if (digitalRead(SIGNAL_PIN) == HIGH) {
        lastSeenHigh = millis();
        pirState = true;
    } else {
        // otherwise, if it's been 12 seconds since last seen
        // active, record that it's inactive.
        // 20 seconds, minus the 8 second active time, times 1000 is
        // 12000 milliseconds to extend the time by
        if ((millis() - lastSeenHigh) > 12000) {
            pirState = false;

    if (pirState == true) {
        // Do whatever you want in here when the PIR is active

With that (untested) code:

  • Any time the PIR is active the current time is recorded and the state is set true
  • When the PIR is LOW and at least 12 seconds have passed since the last activation, set the state to false.
  • Thank you so much, its amazing its working great! No library needed. May 1, 2019 at 9:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.