Arduino Forum

Using Arduino => Sensors => Topic started by: argohoo on Oct 17, 2020, 04:03 pm

Title: PIR sensor "activates" again when relay shuts off
Post by: argohoo on Oct 17, 2020, 04:03 pm
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

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:
    pinMode(lightSens, INPUT);
    pinMode(moveSens, INPUT);
    pinMode(moveSens2, INPUT);
    pinMode(relay, OUTPUT);
    digitalWrite(relay, LOW);
    timer = mints * timersec - 2;
  void loop() {
    // put your main code here, to run repeatedly:
    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);
Title: Re: PIR sensor "activates" again when relay shuts off
Post by: thehardwareman on Oct 17, 2020, 06:28 pm
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.

Title: Re: PIR sensor "activates" again when relay shuts off
Post by: argohoo on Oct 17, 2020, 06:50 pm
(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...
Title: Re: PIR sensor "activates" again when relay shuts off
Post by: thehardwareman on Oct 17, 2020, 09:46 pm
The link to the image is

Try add the filter to the PIR and check if it works better.
Title: Re: PIR sensor "activates" again when relay shuts off
Post by: 6v6gt on Oct 18, 2020, 11:25 am
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.

Title: Re: PIR sensor "activates" again when relay shuts off
Post by: argohoo on Oct 18, 2020, 03:05 pm
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.