trying to make a dual stage PIR sensor

Hey guys, So I have been working on a variety of raspberry pi projects over the last few years and have learned a lot with Python, but for this specific sensor the pi is a little overkill on size and power consumption, after a little research I bought a nodemcu esp8266 board to cut down on power consumption using the sleep function.

So a little explanation about the sensor, I am using a PIR sensor to trigger my car alarm multiplex input, this input will beep a warn away on any signal less than .8 seconds, and a full alarm for anything over .8 seconds. I accomplished this with the pi (and some programming help) by starting a 5 second timer when motion is first detected, a half second pulse is immediately sent to the alarm (this is the first stage), and then when the time is up if the PIR was triggered 2 more times ( 3 triggers in 5 seconds) it sends out the 1 second pulse (this is the second stage), otherwise it does nothing after the initial pulse.
This seems to be working great. The issue was cats walking under the vehicle would trigger the alarm, making it into a 2 stage output scares them off before the alarm is fully triggered.

I followed a few tutorials and have everything wired up and everything to this point is working as I would expect, but only the first stage without a timer. I used a capacitor and transistor to allow the PIR to wake the board up, and that works great (draws about 5mA from the vehicle battery when idle). I was told I needed to use a time while loop in my python code for the second stage, but not sure how to do that with the Arduino code as it seems to be much different. can anyone point me to a source of info on how to do this? or a forum post where an example code is provided that I can adjust to my needs?

I chose a wifi board because eventually I want to have it send an email on the second stage output, but for now I just want to get this working.

here is my code so far for the nodemcu:

code:
int Balarm = 14; //Buzzer alarm connected to GPIO-14 or D5 of nodemcu
int PIRsensor = 5; //PIR sensor output connected to GPIO-5 or D1 of nodemcu

void setup() {
pinMode(PIRsensor, INPUT); // PIR sensor as input
pinMode(Balarm, OUTPUT); // Buzzer alaram as output
digitalWrite (Balarm, LOW);// Initially buzzer off

}

void loop(){
int state = digitalRead(PIRsensor); //Continuously check the state of PIR sensor
delay(500); //Check state of PIR after every half second

if(state == HIGH){
digitalWrite (Balarm, HIGH); //If intrusion detected ring the buzzer
delay(500); //Ring buzzer for .5 seconds
//Serial.println("Motion detected!");
}
else {
digitalWrite (Balarm, LOW); //No intrusion Buzzer off
//Serial.println("Motion absent!");
delay(50000);
}

Serial.println("I'm awake, but I'm going into deep sleep mode until RESET pin is connected to a LOW signal");
ESP.deepSleep(0);
}

If it helps, here is the working Python code. The pin numbers are different and it lists the PIR as a button in the comments, but this currently does what I need it to do on the pi, although I am not opposed to a different/better way if someone knows of one.

import RPi.GPIO as GPIO
import time

GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(6, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(5, GPIO.OUT)



while True:
    print ("Waiting for first button press")
   
    while (GPIO.input(6)) == 1:
        time.sleep(0.1)
        
    while (GPIO.input(6)) == 0:
        time.sleep(0.05)
            
    bc = 1
    print("button pressed")
    end = time.time() + 5
    GPIO.output(5, GPIO.HIGH)
    time.sleep(.5)
    GPIO.output(5, GPIO.LOW)

    while end > time.time():
    
        if (GPIO.input(6)) == 0:
            bc = bc + 1
            print("button pressed")
            print(bc)
            while (GPIO.input(6)) == 0:
                time.sleep(0.05)
            print ("button released")
        
        if bc == 3:
            GPIO.output(5, GPIO.HIGH)
            time.sleep(1)
            GPIO.output(5, GPIO.LOW)
            bc = bc + 1
            
    time.sleep(0.1)

Why delay (50000) ? That’s 50 seconds during which time your program is “stopped”, so it takes 50+ seconds to “loop”

that was left over from when I was trying to test the current draw, but the device appears to go back to sleep 3 seconds after motion is detected so it didn't seem to do anything and I forgot to take it out.

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