Go Down

Topic: PIR sensor "activates" again when relay shuts off (Read 161 times) previous topic - next topic

argohoo

Oct 17, 2020, 04:03 pm Last Edit: Oct 17, 2020, 04:05 pm by argohoo
Ive been googling and searching help for this but just cant find. I bet solution is easy and simple but I just dont get it....
So, PIR sensor works fine on its own but as soon as I connect relay, the PIR sensor or relay "re-activates" as soon as the relay shuts off and of course the relay is on again, this goes as a "loop".
I have another exact same sensor as you can see from the code and it works fine, and everything works fine as long as I dont connect the relay but since I have 2 PIR sensors Im not sure is the problem the relay or the "first" sensor since the second one is not reporting 1 when the relay shuts off.
So just 2 movement sensors and 1 light sensor and relay.

heres a short video of the situation, at first I wave my hand on the sensor couple times and it reports "1" and doesnt trigger anymore until I wave my hand again. When I connect the relay it starts "looping".

Any help? thanks

https://youtu.be/zZp4Yg0QxOA


Code: [Select]

  int lightSens = A3;
  int lightsensVal;
 
  int moveSens = 8;
  int movesensVal;
 
  int moveSens2 = 4;
  int movesensVal2;
 
  int led=2;
 
  int relay = 3;
 
  unsigned long currentime;
  unsigned long sec;
  unsigned long prevsec;
 
  //timersec ajastuksen viive sekunneissa
  unsigned long timersec = 7;
 
  //mints = valolle ajastin minuutteina (ei käytössä!!)
  int mints = 1;
 
  int timer;
 
  void setup() {
    // put your setup code here, to run once:
    Serial.begin(9600);
    pinMode(lightSens, INPUT);
    pinMode(moveSens, INPUT);
    pinMode(moveSens2, INPUT);
    pinMode(relay, OUTPUT);
    pinMode(led,OUTPUT);
    digitalWrite(relay, LOW);
    digitalWrite(led,LOW);
    timer = mints * timersec - 2;
 
 
  }
 
  void loop() {
    // put your main code here, to run repeatedly:
    //Serial.println(lightsensVal);
    //Serial.println(movesensVal);
    Serial.println(movesensVal2);
    //Serial.println(sec);
    //Serial.println(timer);
    //Serial.println(sec);
    //Serial.println(prevsec);
 
    lightsensVal = map(analogRead(lightSens), 0, 1023, 0, 100);
 
    movesensVal = digitalRead(moveSens);
    movesensVal2 = digitalRead(moveSens2);
 
    currentime = millis();
    sec = currentime / 1000;
 
    if (sec - prevsec > timersec)
    {
      digitalWrite(relay, LOW);
  //    digitalWrite(led, LOW);
    }
 
 
    if (movesensVal2 == 1) {
      digitalWrite(relay, HIGH);
      prevsec = sec;
    }
 
  //  if (lightsensVal < 30 && (movesensVal == 1 || movesensVal2 == 1)) {
  //    digitalWrite(led, HIGH);
  //    prevsec = sec;
  //  }
  //  delay(500);
 
    //else digitalWrite(relay,LOW);
 
  }

thehardwareman

My experience with PIR sensors are that they are very sensitive for noise.

I had the same problem with a PIR sensor and a 7 segment LED display. I solved it by adding an LC filter like this.


argohoo

#2
Oct 17, 2020, 06:50 pm Last Edit: Oct 17, 2020, 06:52 pm by argohoo
(if your reply contains photo, I cant see it.... just a blank square.)

edit: nevermind, I see the photo on the phone, cant see it with computer....

but yeah, Ill assume its somekind of noise or such, just dont know how to get rid of it...

thehardwareman

The link to the image is http://www.dahl-stamnes.net/dahls/Ymse/pir.jpg

Try add the filter to the PIR and check if it works better.

6v6gt

This experience I have also had with some cheap PIR sensors, where they are retriggered by electrical activity on the circuit they are controlling. This effect is usually worse when the circuit has multiple PIR sensors, or where you are not using the sensor's built in timer to directly switch a load, but are delaying the switching of that load (a relay in this case). This is because these sensors at least attempt to suppress the direct results of their own switching activity.

The type AM312 seem to be good.

The type HC-SR505 seem to be poor in this respect.

You may be able to deal with this effect in software. That is, you ignore pulses from the sensor when these occur within, say 50 milliseconds of the switching of the relay. You have to set a timer to do this.

You can also play with adding small capacitors and inductors to the connections to the PIR sensor to see if the problem goes away, as you appear to have been doing  as @thehardwareman has done.



argohoo

#5
Oct 18, 2020, 03:05 pm Last Edit: Oct 18, 2020, 03:10 pm by argohoo
Ok, thanks both of you for your replies.
I dont have inductors with me right now, at least I think I dont... :-)

Any other component serve's the purpose? Capasitors I have.

I will definately try to make a short time-delay to the code to see if that helps!

Im trying to switch lights for the garage, actually the "broken" sensor is going to be above door outside and the other inside sniffing if there is movement inside so the first sensor needs to trigger "only once" when entering garage so delay might work well for that since there is movement outside only when entering garage.

Go Up