Relay turn on whenever I turn on Arduino

Hello every one

I designed a circuit that will turn the light (which is connected to a Realy) whenever the motion sensor detects any movement, and the light will stay ON till there IR sensor getting a signal from remote control then it will turn the light off.

Everything is working perfectly the only problem I have is: whenever I restart the Arduino or unplug Arduino and re-plug it again the relay automatically will turn on without detecting any movement by the motion detector.

I will attach the code and the circuit diagram.

please I need your help.

#include <IRremote.h>
int PIR_Sensor=3;
int Relay=10;
int IR_Sensor = 6;

IRrecv irrecv(MP_IR);
decode_results results;
#define BUTTON_0 0x9716BE3F

void setup() {
Serial.begin(9600);
pinMode(PIR_Sensor, INPUT);
pinMode(Relay, OUTPUT);
pinMode(IR_Sensor, INPUT);
digitalWrite(MP_Notification, HIGH);

irrecv.enableIRIn();
}

void loop() {
if(digitalRead(3) == HIGH)
{
digitalWrite(10, LOW);
}
if (irrecv.decode(&results))
{
if (results.value == BUTTON_0)
{
digitalWrite(10, HIGH);
}
irrecv.resume();
}
}

"Everything is working perfectly the only problem I have is: whenever I restart the Arduino or unplug Arduino and re-plug it again the relay automatically will turn on without detecting any movement by the motion detector."

Arduino pins may go thru undefined states on boot up. The first test you might do is just have dummy code in the arduino with the relay attached, then plug and unplug the arduino to see if the relay still actuates. Then attach the detector and plug and unplug again. If the relay does not actuate under these two test, then you probably can fix your issue in your code.

thank you very much for your kind replay.

1- I performed the first test (dummy code) it is not going to turn ON after unplug and re-plug.

2- But after attaching the (detector) it is not going to turn on automatically right after compiling the code, but after unplug it then re-plug Arduino it will turn the relay on automatically.

Your IR input pins may be initially floating after boot up. In the setup part of your code, you might try first set those pins low to clear any floating voltage, have a small delay, then set them for input.

 void setup() {
Serial.begin(9600);
pinMode(PIR_Sensor, INPUT);
digitalWrite(Relay,HIGH);
pinMode(Relay, OUTPUT);
pinMode(IR_Sensor, INPUT);
digitalWrite(MP_Notification, HIGH); 

irrecv.enableIRIn();

delay(500);
}

The following sequence will probably prevent this from happening

digitalWrite(Relay, HIGH);
pinMode(Relay, OUTPUT);

Hmm larryd beat me to it.

zoomkat:
Your IR input pins may be initially floating after boot up. In the setup part of your code, you might try first set those pins low to clear any floating voltage, have a small delay, then set them for input.

I did what you told me but the result is same after the delay period is finishing it turns the relay ON again.

larryd:

 void setup() {

Serial.begin(9600);
pinMode(PIR_Sensor, INPUT);
digitalWrite(Relay,HIGH);
pinMode(Relay, OUTPUT);
pinMode(IR_Sensor, INPUT);
digitalWrite(MP_Notification, HIGH);

irrecv.enableIRIn();

delay(500);
}




thanks for your kind replay.

I don't think you changed anything!! even tho I tried it but it is the same no change.

digitalWrite(Relay,HIGH);
And
delay(500);

The above lines were added.

I did that as well, but it was the same result, it is really weird. please if you have any ideas guys

Comment these lines out:


//if(digitalRead(3) == HIGH)
//{
// digitalWrite(10, LOW);
//}


Does the relay still pick at power up ?


Maybe try:

pinMode(PIR_Sensor, INPUT_PULLUP);

Without trying to reverse engineer the code, what type of output do the IR sensors have when motion is detected/