Using PIR to activate relay

I want to activate a 6 relays in pairs when the PIR detects movement. I also wanted to have the PIR to be in an off state until the relays re-activate. I can't seem to get the PIR to work properly. Sidenote, I connected the PIR to an external 12v psu.

CODE

#define RELAY_ON 1
#define RELAY_OFF 0

#define Relay_1 2 // Arduino Digital I/O pin number
#define Relay_2 3
#define Relay_3 4
#define Relay_4 5
#define Relay_5 6
#define Relay_6 7

/-----( Declare Variables )-----/
int waittime; // Delay between changes

//the time we give the sensor to calibrate (10-60 secs according to the datasheet)
int calibrationTime = 30;

//the time when the sensor outputs a low impulse
long unsigned int lowIn;

//the amount of milliseconds the sensor has to be low
//before we assume all motion has stopped
long unsigned int pause = 15000;

int pirPin = 9; //the digital pin connected to the PIR sensor's output

void setup() /****** SETUP: RUNS ONCE ******/
{
waittime = 1000;
Serial.begin(9600);

//give the sensor some time to calibrate
Serial.print("calibrating sensor ");
for(int i = 0; i < calibrationTime; i++){
Serial.print(".");
delay(1000);
}
Serial.println(" done");
Serial.println("SENSOR ACTIVE");
delay(50);

//-------( Initialize Pins so relays are active at reset)----
digitalWrite(Relay_1, RELAY_ON);
digitalWrite(Relay_2, RELAY_ON);
digitalWrite(Relay_3, RELAY_ON);
digitalWrite(Relay_4, RELAY_ON);
digitalWrite(Relay_5, RELAY_ON);
digitalWrite(Relay_6, RELAY_ON);

//---( THEN set pins as outputs )----

pinMode(pirPin, INPUT);
digitalWrite(pirPin, LOW);

pinMode(Relay_1, OUTPUT);
pinMode(Relay_2, OUTPUT);
pinMode(Relay_3, OUTPUT);
pinMode(Relay_4, OUTPUT);
pinMode(Relay_5, OUTPUT);
pinMode(Relay_6, OUTPUT);
delay(4000); //Check that all relays are active at Reset

}//--(end setup )---

void loop() /****** LOOP: RUNS CONSTANTLY ******/
{

if(digitalRead(pirPin) == HIGH){
//---( Turn all 8 relays ON in pairs)---

digitalWrite(Relay_1, RELAY_OFF);// set the Relay OFF
digitalWrite(Relay_2, RELAY_OFF);// set the Relay OFF
delay(waittime); // wait for a second

digitalWrite(Relay_3, RELAY_OFF);// set the Relay OFF
digitalWrite(Relay_4, RELAY_OFF);// set the Relay OFF
delay(waittime);

digitalWrite(Relay_5, RELAY_OFF);// set the Relay OFF
digitalWrite(Relay_6, RELAY_OFF);// set the Relay OFF
delay(waittime * 15); // wait see all relays ON

if(digitalRead(pirPin) == LOW){
//---( Turn all 8 relays OFF in pairs)---

digitalWrite(Relay_1, RELAY_ON);// set the Relay ON
digitalWrite(Relay_2, RELAY_ON);// set the Relay ON
delay(waittime);

digitalWrite(Relay_3, RELAY_ON);// set the Relay ON
digitalWrite(Relay_4, RELAY_ON);// set the Relay ON
delay(waittime);

digitalWrite(Relay_5, RELAY_ON);// set the Relay ON
digitalWrite(Relay_6, RELAY_ON);// set the Relay ON
delay(waittime * 10); // wait see all relays OFF

}

}
}

If the PIR device output signal is a 12V signal, then you will probably kill the Arduino input pin in first shot.
Arduino runs off a 5v supply, and only a fragment higher than that could cause damage to input pins.

I donno if you have taken precautions to that, using series restors or a divider on the Arduino input, that will of course kill the drama.

Otherwise I'd suggest you monitor the PIR output using a voltmeter.
Your goal is to ensure you get a 100% valid digital (0V or 5V) signal from the PIR, before suspecting your code.

Anders53:
If the PIR device output signal is a 12V signal, then you will probably kill the Arduino input pin in first shot.
Arduino runs off a 5v supply, and only a fragment higher than that could cause damage to input pins.

I donno if you have taken precautions to that, using series restors or a divider on the Arduino input, that will of course kill the drama.

Otherwise I'd suggest you monitor the PIR output using a voltmeter.
Your goal is to ensure you get a 100% valid digital (0V or 5V) signal from the PIR, before suspecting your code.

I can run the PIR off the 3.3v pin on the arduino I guess. The 5V from the arduino is already occupied.

Dar_T:
I can run the PIR off the 3.3v pin on the arduino I guess. The 5V from the arduino is already occupied.

But you stated in first submission, that your are running the PIR off a 12V supply ?

I wonder if we are barking at different trees here ?
If the PIR output is an uncommitted relay contact output, is does not matter to the Arduino input pin, as then you can supply the relay contact with +5V on one side and the other relay contact side connects to the Arduino input pin no problems.

Anders53:
But you stated in first submission, that your are running the PIR off a 12V supply ?

I wonder if we are barking at different trees here ?
If the PIR output is an uncommitted relay contact output, is does not matter to the Arduino input pin, as then you can supply the relay contact with +5V on one side and the other relay contact side connects to the Arduino input pin no problems.

Ah, I see. Anyways I put in a soldered wire into the 3.3v of the PIR. Modifying Cheap PIR Motion Sensor to Work at 3.3V | Random Nerd Tutorials
So I'm running the PIR off the 3.3v pin of the arduino, the gnd pin of the arduino, and then into input 9. I adjusted my code to the website but it still doesn't work. I want the relays to be on when the PIR is low and then turn off with PIR is high. I also need the PIR to wait for at least 5 seconds before registering another input.

#define RELAY_ON 1
#define RELAY_OFF 0

#define Relay_1 2 // Arduino Digital I/O pin number
#define Relay_2 3
#define Relay_3 4
#define Relay_4 5
#define Relay_5 6
#define Relay_6 7

/-----( Declare Variables )-----/
int waittime; // Delay between changes

int state = LOW; // by default, no motion detected
int val = 0; // variable to store the sensor status (value)
int pirPin = 9; //the digital pin connected to the PIR sensor's output

void setup() /****** SETUP: RUNS ONCE ******/
{
waittime = 1000;
pinMode(pirPin, INPUT); // initialize sensor as an input
Serial.begin(9600); // initialize serial

//-------( Initialize Pins so relays are active at reset)----
digitalWrite(Relay_1, RELAY_ON);
digitalWrite(Relay_2, RELAY_ON);
digitalWrite(Relay_3, RELAY_ON);
digitalWrite(Relay_4, RELAY_ON);
digitalWrite(Relay_5, RELAY_ON);
digitalWrite(Relay_6, RELAY_ON);
//---( THEN set pins as outputs )----

digitalWrite(pirPin, LOW);
pinMode(Relay_1, OUTPUT);
pinMode(Relay_2, OUTPUT);
pinMode(Relay_3, OUTPUT);
pinMode(Relay_4, OUTPUT);
pinMode(Relay_5, OUTPUT);
pinMode(Relay_6, OUTPUT);
delay(4000); //Check that all relays are active at Reset

}//--(end setup )---

void loop() /****** LOOP: RUNS CONSTANTLY ******/
{

val = digitalRead(pirPin); // read sensor value
if (val == HIGH) { // check if the sensor is HIGH
//---( Turn all 8 relays ON in pairs)---

digitalWrite(Relay_1, RELAY_OFF);// set the Relay OFF
digitalWrite(Relay_2, RELAY_OFF);// set the Relay OFF
delay(waittime); // wait for a second

digitalWrite(Relay_3, RELAY_OFF);// set the Relay OFF
digitalWrite(Relay_4, RELAY_OFF);// set the Relay OFF
delay(waittime);

digitalWrite(Relay_5, RELAY_OFF);// set the Relay OFF
digitalWrite(Relay_6, RELAY_OFF);// set the Relay OFF
delay(5000); // wait see all relays ON

if (state == LOW) {
Serial.println("Motion detected!");
state = HIGH; // update variable state to HIGH
}
}

else {

//---( Turn all 8 relays OFF in pairs)---

digitalWrite(Relay_1, RELAY_ON);// set the Relay ON
digitalWrite(Relay_2, RELAY_ON);// set the Relay ON
delay(waittime);

digitalWrite(Relay_3, RELAY_ON);// set the Relay ON
digitalWrite(Relay_4, RELAY_ON);// set the Relay ON
delay(waittime);

digitalWrite(Relay_5, RELAY_ON);// set the Relay ON
digitalWrite(Relay_6, RELAY_ON);// set the Relay ON
delay(10000); // wait see all relays OFF

if (state == HIGH){
Serial.println("Motion stopped!");
state = LOW; // update variable state to LOW
}
}
}

Well - if the PIR supplies a signal of just under 3.3V for the Arduino input, it will likely not be detected, as a HIGH state voltage on the input pin should be somewhat higher, best a 3.5V as minimum HIGH signal voltage.

That could be your problem.

What do you mean by 5V on Arduino is "occupied" ?
The onboard 3.3V regulator is fed off the onboard 5V regulator, which again is fed off the external power input.
Meaning it does not matter if you supply the PIR from the 5V or the 3.3V Arduino power pin, as the load in both cases would end up on the 5V supply.

Anders53:
Well - if the PIR supplies a signal of just under 3.3V for the Arduino input, it will likely not be detected, as a HIGH state voltage on the input pin should be somewhat higher, best a 3.5V as minimum HIGH signal voltage.

That could be your problem.

What do you mean by 5V on Arduino is "occupied" ?
The onboard 3.3V regulator is fed off the onboard 5V regulator, which again is fed off the external power input.
Meaning it does not matter if you supply the PIR from the 5V or the 3.3V Arduino power pin, as the load in both cases would end up on the 5V supply.

Sorry I meant that the 5v pin of the arduino is connected to the relay VCC.