PIR motion sensor delay

I'm having some trouble with setting up a PIR motion sensor. Basically, I want every time a trigger happens to add to a count that is saved on a text file. This is to track a small animal entering and exiting a pe The problem I'm having is the delay between triggers. I want a delay of about 2 seconds between triggers, however the lowest I can get is about 5 seconds. The first set of sensors I bought turned out to have an internal limit of 5 seconds, so I bought a second one that can go down to .5 seconds. Again, I'm having the same issue where the delay is too long. This time it can range from about 4-6 seconds, even with the potentiometers at a minimum. It is set on a single trigger mode. There is no delay in my code either. Anyone have any tips? Am I just testing it wrong (waving my hand by it) or should I try to get a different sensor? If so, any recommendations? Here is my code if anything helps:

#include <SD.h>
#include <SPI.h>

File myFile;
const int chipSelect = 4; // Pin for SD module

// pin for the PIR sensor
const int sensorPin = 3;

// Counter for trigger
unsigned long count = 0;

void printFormattedTime(unsigned long currentTime) {
    unsigned long seconds = currentTime / 1000;
    unsigned long minutes = seconds / 60;
    unsigned long hours = minutes / 60;
    seconds %= 60;
    minutes %= 60;
    
    myFile.print(hours);
    myFile.print(":");
    if (minutes < 10) myFile.print("0");
    myFile.print(minutes);
    myFile.print(":");
    if (seconds < 10) myFile.print("0");
    myFile.println(seconds);
}

void setup() {
    Serial.begin(9600);
    pinMode(sensorPin, INPUT);

    if (!SD.begin(chipSelect)) {
        Serial.println("SD Card initialization failed!");
        return;
    }
    Serial.println("SD Card initialized.");
}

void loop() {
    if (digitalRead(sensorPin) == HIGH) {
        count++;
        Serial.println(count);
        logEvent(count);
        delay(1500);
        
        //while (digitalRead(sensorPin) == HIGH) {
            //delay(100); 
        //}
        

    }

}

void logEvent(unsigned long count) {
    myFile = SD.open("log.txt", FILE_WRITE);
    if (myFile) {
        myFile.print("PIR Sensor Triggered - Count: ");
        myFile.print(count);
        myFile.print(" - Time: ");
        printFormattedTime(millis());
        myFile.close();
    } else {
        Serial.println("Error opening log file");
    }
}

Hello @bangoo welcome to the forum.

You might want to look at this How to get the best out of this forum before you proceed any further.

It tells you how to ask a question. Congratulations for posting the code correctly, but you are missing some vital stuff, like telling us what sort of Arduino you are using.

But in the loop function you have

Also opening and closing a file takes time as well.

A beginners mistake is to try and do too much at once, the trick to build up from simple functionality one feature at a time. So try commenting out the LogEvent call in the loop function.

What does the serial print produce? Select what you see in the serial port window and post it like it were code.

What is a "pe"?
There may be easier ways to do this

Sorry about that, I’m using an Arduino Uno Rev3. I meant to say that I tried it without a delay, and it didn’t change anything. The serial print is there for me to see what the count is and when it is triggering. Every time there is a trigger, it posts the count. I want the time between triggers to be about 2 seconds, but I can’t get it below 5. I don’t want to comment out the logevent part because I want it to write to the text file every time the sensor is triggered. Is there a better way to do this?

Meant pen sorry about that

Then it may be much easier to use an IR beam break sensor at the pen entry/exit point.

Yes of course it is.

That might not be possible you will have to see.

You will never know until you can discover where the problem is. You can only do this by temporally removing aspects of your code.

You can reject this advice if you like, but if you do then sounds to me like you are not really interested in solving your problem.

Hi, @bangoo
Welcome to the forum.

Can I suggest a couple of different approaches to updating your log.

1.) Only update the log after a number of triggers has been counted, say 5.
That will mean your project is spending most of its time ready for the next trigger and could possibly miss 1 in 5 if the animal traffic gets high.

2.) After say 15 minutes, check the count if it has changed then update the log, if not keep monitoring. This will also maximise the detecting time of your project.

Thanks.. Tom.... :smiley: :+1: :coffee: :australia:

I’ve tried removing the log event all together and just printing to the serial monitor. It doesn’t reduce the delay at all. I tried with and without delays but nothing improves the delay time.

The issue with that is I need every trigger along with a timestamp logged. It would be much better to only do it every so often, but I need every trigger.

Other than using a different sensor, there is nothing you can do about the 5 second delay.

Yea I’m thinking of switching to 2 IR Beam sensors. Do you have any tips for protecting the ends of them so that the animal doesn’t damage the sensor?

I don't think the industrial versions need protection. For the hobby type you can place a short length of tube/pipe over the emitter and receiver. I don't know how well they will work with a clear plastic cover.

Buy the beam break type not the reflector type.

Yup, last time I did this I used the reflective ones and they didn’t work out so well. Do you know if there are any mounting brackets available for Ir beam sensors, or good spots to look for that kind of thing? The outside of the pen is a flat wall, and with how the mounting holes are on the sensors, I’m not going to be able to screw them in to the pen and also have them face each other. Worst case I can just hot glue them on, but I’m not sure how long that will last. Any tips? And thanks for the help so far.

I've use some double sticky tapes that once they go on they are on forever.
Well I live in the US and we have a store called Home Depot, that carries just about anything you need for a home project.

You have in your code:-

That is a pitifully slow serial speed to use these days. Make it faster, but for a start try and remove the call to it in the loop function to see if that is causing the problem.

Why are you reluctant to do some faulty finding? If you continue with not cooperating people will stop trying to help you. Your attitude is appalling.

1 Like

Hi, @bangoo

Does it go down to 0.5s ?

Tom.... :smiley: :+1: :coffee: :australia:

The sensitivity set to minimum means a delayed trigger time.
The time pot is the length of the "on" trigger.
So it seems the time pot should be set to minimum, and the sens pot to maximum.
Leo..

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.