Hello,
I'm experiencing an issue with my Arduino Uno project, which includes an SR501 PIR sensor and two relays. After running for some time, the system freezes, and:
Sometimes, the relay LED pulses indefinitely.
Sometimes, the relay stays ON permanently, even when the SR501 no longer detects movement.
Here is the code I’m using:
#include <Arduino.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
const int pirPin = 2;
const int relayPin = 8;
const int relay2Pin = 9;
int i = 20; // Initially out of activation range
LiquidCrystal_I2C lcd(0x27, 16, 2);
void setup() {
Serial.begin(9600);
lcd.init();
lcd.backlight();
lcd.print("System Active");
Serial.println("System Active");
pinMode(pirPin, INPUT);
pinMode(relayPin, OUTPUT);
pinMode(relay2Pin, OUTPUT);
digitalWrite(relayPin, LOW);
digitalWrite(relay2Pin, LOW);
}
void loop() {
int pirState = digitalRead(pirPin);
if (pirState == HIGH) {
i = 1; // Restart activation cycle
}
if (i <= 20) {
digitalWrite(relayPin, HIGH);
digitalWrite(relay2Pin, HIGH);
delay(500); // Wait 500 ms
digitalWrite(relayPin, LOW);
digitalWrite(relay2Pin, LOW);
delay(500); // Wait 500 ms
i++; // Increment counter
}
else {
digitalWrite(relayPin, LOW);
digitalWrite(relay2Pin, LOW);
}
}
I have used a different code, but the same problem persists...
I have used 4 kΩ and 8.4 kΩ resistors as pulldown resistors because when I put my finger near the Arduino, it triggers and turns on the relays.
Can you please help me identify exactly where the problem is?
You can actually help yourself! Put Serial.print() messages in your code so you can trace the logic and when the Arduino freezes, your messages will, or should, show the path that led to the freeze.
Nice picture but it is missing a lot. For example there are no part numbers or links, you have a black and purple crossing over a ground pin. How is it powered. What is connected to the relays? An annotated schematic would explain all of this and more.l
For example, now it is behaving differently: when I connect the SR501, it stays on continuously, even if I use a 2kΩ, 4kΩ, or 10kΩ pull-down resistor from pin 2 to ground. I changed relays module and sr501 but again the same problem.
What is Vcc?
It sounds to me like the system is running on the "edge of power"
Replace the relays with a single LED, which draw less current than the relay.
Does the system freeze too?
Last resort to try is to put a delay between the relays switching ON to prevent peak currents. (This is a band-aid solution, rather than understanding the problem).
something like
if (i <= 20) {
digitalWrite(relayPin, HIGH);
delay(500);
digitalWrite(relay2Pin, HIGH);
delay(500); // Wait 500 ms
digitalWrite(relayPin, LOW);
delay(500);
digitalWrite(relay2Pin, LOW);
delay(500); // Wait 500 ms
i++; // Increment counter
}
I’m powering the relays using an external power supply instead of drawing current from the Arduino. Both the Arduino and the relay power supply share a common ground.
Despite this, the Arduino still freezes after a few hours of operation.
I’m using an HC-SR501 PIR sensor connected via a 4-meter ethernet cable, which generates frequent false triggers—even after placing a 1000µF capacitor between VCC and GND at the sensor’s end.
Interestingly, when I replaced the relays with an LED for testing, the Arduino ran without freezing, although the false triggers from the PIR sensor continued.
Looking for a cause of the PIR glitches I noticed.
you test for <= 20 in loop so the initial state is activated.
OK it is just a comment, however computers are sensitive for every bit.
If such minor differences appeared in code it could be major
Current code shows no cause for the glitches, so it might be in the cable or sensor itself.
What you could do is only reset the counter i when you have e.g. 2 consecutive PIR signals.
For this you need to track the previous state.
Reminds me of a PIR sensor in a store where I worked. The PIR was quite sensitive as an insect got into the sensor and every time it searched for its way out ==> ALARM!
A small insect within a few cm apparently looked similar enough to a human at several meters away.
long story short, the PIR might trigger on a spider or long grass or a moth.
do you have a 2nd sensor to compare
have you tried the sensor without the cable? (no short circuits in the cable?)
Yes, I’ve tested with four SR501 sensors and Arduino, but they often trigger false alarms.
Would adding 100nF capacitors between VCC and GND on the SR501 help reduce false alarms?
I'm really tired of trying to build a reliable alarm sensor — I just want to protect my chickens from predators.