PIR Motion Sensor - With Relays

Hello, I am working on a project where a PIR motion sensor triggers an 8 channel relay. The relays turn on at different times. I could not find a code online for this so I took it upon myself to attempt to write it myself. However, when I set it up, the relays go off by themselves even with no motion. it seems that they are in a loop. Could someone please review the code and see if there is anything wrong with it.
Thank You!!!

Code-
int irmotionPin = 4;
int relayPin1 = 5;
int relayPin2 = 6;
int relayPin3 = 7;
int relayPin4 = 8;
int D = 2000;

void setup(){
Serial.begin(9600);
pinMode(relayPin1, OUTPUT);
pinMode (relayPin2, OUTPUT);
pinMode (relayPin3, OUTPUT);
pinMode (relayPin4, OUTPUT);
digitalWrite(relayPin1, LOW);
digitalWrite(relayPin2, LOW);
digitalWrite(relayPin3, LOW);
digitalWrite(relayPin4, LOW);
}

void loop(){

while (digitalRead(irmotionPin) == HIGH) {
digitalWrite(relayPin1, HIGH);
delay(D);
digitalWrite(relayPin2, HIGH);
digitalWrite(relayPin3, HIGH);
digitalWrite(relayPin4, HIGH);
Serial.println("Triggered");
delay(5000);
}

digitalWrite(relayPin2, LOW);
digitalWrite(relayPin3, LOW);
delay(4000);
digitalWrite(relayPin1, LOW);
digitalWrite(relayPin4, LOW);
Serial.println("Waiting");
delay(1);
}

Do a simple test to determine whether the relays are switched on by a HIGH or a LOW signal by making a direct connection to the 5V or GND pin on your Arduino. The common relay modules are often active LOW.

Beginners often have the misconception that HIGH == on and LOW == off. This is not necessarily true. HIGH and LOW are simply two different states.

AxelT:
The relays turn on at different times.

Of course. That's simply because you tell your Arduino to set one or two, then wait a bit, and then set the others.

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html . Then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks.. Tom..... :slight_smile: