Relay Turned on When Arduino Connected to Power Source

Greetings,

Dears I Hope you are in Good Health,

I’m Using Arduino UNO, This is My Project:

Motion Detector(PIR)
Relay
IR Remote

this is how it work

if Motion Detected Turn on the Relay, when IR Remote Received the Signal it Will Turn off the Relay

THIS IS MY PROBLEM

when i Upload the code it will work Normally, but when i unplug the the power and plugged in again, the Relay will Turn on :frowning:

can any one Help me Please to make relay off when plugged to power source.

Thanks in Advance…

#include <IRremote.h>
int MP_Sensor=3;    //Define PIR
int MP_Notification=4;  //Define Relay
int MP_IR = 6;  //Define IR Remote


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


void setup() {
Serial.begin(9600);
pinMode(MP_Sensor, INPUT);
pinMode(MP_Notification, OUTPUT);
pinMode(MP_IR, INPUT);
digitalWrite(MP_Notification, HIGH);   //Make Relay off at Start

irrecv.enableIRIn();
}

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

Since your relay is active LOW use a pullup resistor. The pin defaults to an input (high impedance) at powerup.

Dear ToddL1962 Many thanks for your Reply,

I'm Little bit new at Arduino :slight_smile:
Can you explain it to me because I Don't Understand.

This line controls the relay state at startup:

digitalWrite(MP_Notification, HIGH);   //Make Relay off at Start

try

digitalWrite(MP_Notification, LOW);   //Make Relay off at Start

Dear aarg,
I Tried this solution but Didn't Work :frowning:

aarg:
This line controls the relay state at startup:

digitalWrite(MP_Notification, HIGH);   //Make Relay off at Start

try

digitalWrite(MP_Notification, LOW);   //Make Relay off at Start

Probably not as most relay modules are active low.

What does the sensor sense at startup? What kind of sensor is it? Is the input floating by any chance?
Why do you name the I/O pins, but then not use the names in loop()?

my motion detector (PIR) is work without any problem.

and there is no different if I use the definition or the Arduino pin