Hi everyone, I’ve been working on this small project for about 3 weeks, I’m stuck at this point and I could lose my job if I don’t get it to work properly :o so I will really appreciate your help.
The idea is simple, pir senses motion, a timer starts decreasing, each time motion is detected the timer resets, when the time ends the relay acts as switch and the lights are turned off. Green led turns on only when movement is detected, and the yellow led starts blinking when the time is near to end.
Everything is working properly, the only problem is that when the lights are shut off (no movement has been detected, and the time ends), the PIR “”"“senses”""" movement again (even when there wasn’t any), and the lights are turned on again
I think the PCB includes the protection needed for the Relay: diode, optocoupler, resistor, transistor. Please correct me if I’m wrong.
I think the problem is within my code because the relay respond properly when trying another code that turns on the light when motion is detected.
I attach a schematic of the circuit, the code, and a picture of the relay I’m using. I couldn’t find the datsheet of the relay module, but it reads: HL-52 v1.0
Here is the link of the PIR datasheet: https://www.mpja.com/download/31227sc.pdf
I’m using Arduino Uno powered by USB.
I really need your help guys, I might lose my job if I don’t get this to work properly
I’m I having parasite signals?
Please help me…Thank you.
int Pir = 2; // connect the Pir to pin 2
int Relay = 7; // connect the relay to pin 7
int pirState = LOW; // we start, assuming no motion detected
int val = 0;
const int LEDyellowPin= 13;
const int LEDgreenPin=12;
int t=0;
int calibrationTime = 20;
void setup(){
Serial.begin(9600);
pinMode(Pir, INPUT); // set the pir sensor as an input
pinMode(Relay, OUTPUT); // set the relay as output
pinMode(LEDyellowPin, OUTPUT);
pinMode(LEDgreenPin, OUTPUT);
digitalWrite(Relay, HIGH); // turn off the relay
Serial.print("calibrating sensor ");
for(int i = 0; i < calibrationTime; i++){
Serial.print(".");
delay(1000);
}
Serial.println(" done");
Serial.println("SENSOR ACTIVE");
delay(50);
}
void loop() {
if (digitalRead(Pir) == HIGH) { // PIR starts the counter
digitalWrite(LEDgreenPin, HIGH);
t = 20; // in seconds
if (pirState == LOW) { // pir state was low
// motion was just detected
Serial.println("motion detected Relay ON");
// We only want to print on the output change, not state
t= 20;
pirState = HIGH;
}
}
if (t > 0) { // if countdown is still running
digitalWrite(Relay, LOW);
Serial.print(t);
Serial.println(" Relay ON");
t --; // -1
if (t < 5 && t > 0){
digitalWrite(LEDyellowPin, LOW);
delay(200);
digitalWrite(LEDyellowPin, HIGH);
delay(200);
digitalWrite(LEDyellowPin, LOW);
delay(200);
digitalWrite(LEDyellowPin, HIGH);
delay(200);
digitalWrite(LEDyellowPin, LOW);
delay(200);
digitalWrite(LEDyellowPin, HIGH);
delay(200);
}
}
else {
digitalWrite(LEDgreenPin, LOW);
digitalWrite(LEDyellowPin, LOW);
if (pirState == HIGH){ // pir was on
// motion has stopped
Serial.println("motion ended ");
digitalWrite(Relay, HIGH);
Serial.println("Relay OFF");
pirState = LOW;
delay(1000);
}
}
delay(1000); // one sec loop delay
}