Problem with Sensor PIR - SR505

Hi

I'm trying to get a motion sensor (SR-505) to work on the ESP32.

Searching the internet I found a way that supposedly would work. But with me it doesn't work. Nothing is detected. I've tried using two sensors of the same model, but nothing, I don't know what it could be.

The code is super simple:

#define pir_pin 14


void IRAM_ATTR detectsMovement() {
  Serial.println("MOTION DETECTED!!!");
}

void setup() {
  Serial.begin(9600);
  pinMode(pir_pin, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(pir_pin), detectsMovement, RISING);

}

void loop() {

}

And the sensor is wired in ESP32, with the (-) in the GND, the (+) in the 3v3 and using digital pin 14.

I'd ditch the interrupt, and just monitor the pin to see how it behaves.

Don't complicate thigs by using an ISR

Start by simply printing the state of the PIR pin. Does it change when expected ?

Is the PIR a 3V3 device ?

From https://static.rapidonline.com/pdf/78-4110_v1.pdf

Operating voltage range: DC4.5-20V

Hi,
The output of this module (SR-505) is LOW 0V and HIGH +3.3V,
but its power needs to be from +4.5V to a maximum of +20V.
If you are feeding it with +3.3V, it will not work.

RV mineirin

Thank you all!

I changed the 3.3V pin to the VIn pin and made a simpler and more conventional application:

#define pir_pin 14


/*void IRAM_ATTR detectsMovement() {
  Serial.println("MOTION DETECTED!!!");
}*/

void setup() {
  Serial.begin(9600);
  pinMode(pir_pin, INPUT);
  //attachInterrupt(digitalPinToInterrupt(pir_pin), detectsMovement, RISING);

}

void loop() {

  if(digitalRead(pir_pin)==HIGH){
    Serial.println("ON!");
  }

}

The problem is that when it detects the presence of movement and the sensor value goes to HIGH, it still remains for a few seconds, sometimes short but sometimes long.

Is there any way to make this work better?

The length of time that the input stays HIGH is a function of the PIR, not the sketch

If you want to detect when the input becomes HIGH rather than whe it is HIGH then look at the StateChangeDetection example in the IDE

Hi!

Thanks for the tip @UKHeliBob

I made this sketch:

#define pir_pin 15
bool lastStatePir = 0;
int statePir = 0;



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

void loop() {
  statePir = digitalRead(pir_pin);
  Serial.println("STATE PIR");
  Serial.println(statePir);
  Serial.println("LAST STATE PIR");
  Serial.println(lastStatePir);
  if (statePir != lastStatePir) {
    if (statePir == HIGH) {
      Serial.println("ON!");
    }
    else{
      Serial.println("OFF");
    }
  delay(50);
  }
  lastStatePir = statePir;
}

the problem is that the sensor already starts at HIGH. And then what I get on the serial monitor is the following:

11:33:05.305 -> 1
⸮⸮⸮E⸮ 4⸮Q⸮5 r+3H⸮STATE PIR
11:33:05.601 -> 1
11:33:05.601 -> LAST STATE PIR
11:33:05.652 -> 0
11:33:05.652 -> ON!
11:33:05.652 -> STATE PIR
11:33:05.700 -> 1
11:33:05.700 -> LAST STATE PIR
11:33:05.700 -> 1
11:33:05.700 -> STATE PIR
11:33:05.700 -> 1
11:33:05.745 -> LAST STATE PIR
11:33:05.745 -> 1
11:33:05.745 -> STATE PIR
11:33:05.745 -> 1
11:33:05.745 -> LAST STATE PIR
11:33:05.793 -> 1
11:33:05.793 -> STATE PIR
11:33:05.793 -> 1
11:33:05.793 -> LAST STATE PIR
11:33:05.793 -> 1
11:33:05.793 -> STATE PIR
11:33:05.840 -> 1
11:33:05.840 -> LAST STATE PIR
11:33:05.840 -> 1
11:33:05.840 -> STATE PIR
11:33:05.840 -> 1
11:33:05.840 -> LAST STATE PIR
11:33:05.887 -> 1
11:33:05.887 -> STATE PIR
11:33:05.887 -> 1
11:33:05.887 -> LAST STATE PIR
11:33:05.934 -> 1
11:33:05.934 -> STATE PIR
11:33:05.934 -> 1
11:33:05.934 -> LAST STATE PIR
11:33:05.934 -> 1
11:33:05.934 -> STATE PIR
11:33:05.979 -> 1
11:33:05.979 -> LAST STATE PIR
11:33:05.979 -> 1
11:33:05.979 -> STATE PIR
11:33:05.979 -> 1
11:33:05.979 -> LAST STATE PIR
11:33:06.028 -> 1
11:33:06.028 -> STATE PIR
11:33:06.028 -> 1
11:33:06.028 -> LAST STATE PIR
11:33:06.028 -> 1
11:33:06.074 -> STATE PIR
11:33:06.074 -> 1
11:33:06.074 -> LAST STATE PIR
11:33:06.074 -> 1
11:33:06.074 -> STATE PIR
11:33:06.121 -> 1
11:33:06.121 -> LAST STATE PIR
11:33:06.121 -> 1
11:33:06.121 -> STATE PIR
11:33:06.121 -> 1
11:33:06.121 -> LAST STATE PIR
11:33:06.167 -> 1
11:33:06.167 -> STATE PIR
11:33:06.167 -> 1
11:33:06.167 -> LAST STATE PIR
11:33:06.167 -> 1
11:33:06.215 -> STATE PIR
11:33:06.215 -> 1
11:33:06.215 -> LAST STATE PIR
11:33:06.215 -> 1
11:33:06.215 -> STATE PIR
11:33:06.215 -> 1
11:33:06.261 -> LAST STATE PIR
11:33:06.261 -> 1
11:33:06.261 -> STATE PIR
11:33:06.261 -> 1
11:33:06.261 -> LAST STATE PIR
11:33:06.308 -> 1
11:33:06.308 -> STATE PIR
11:33:06.308 -> 1
11:33:06.308 -> LAST STATE PIR
11:33:06.308 -> 1

It does not change from HIGH to LOW. It stays on HIGH for more than a minute and doesn't change.

The connection still remains the same. ESP32, the (+) connected to the VIN pin, the (-) to the GND and I'm using pin 15.

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