Arduino PIR-relay setup turning on at random times.

All,

I am new at programming an arduino, and I thought I had all of my ducks in a row on the sketch I did. I am wiring halloween props using a PIR sensor connected through telephone cable to the Arduino nano in a box with the single relay module board. The PIR is a standard 5-12v pir that is receiving 4.68V (tolerance is 4.5V) of input power. I checked all of the soldering connections for shorts, as well as the wires. I have the sensor connected to the A0 Pin, and the relay to the A2 Pin. The problem I am having is that the sensor goes off at different times, and now not at all. In addition, it sometimes goes off without the sensor attached. Am I not defining the pins correctly? I tried to make this work last year, but gave up and now I am trying to get it to work this year, as I have had more time to troubleshoot. Any help would be appreciated.

Below is my sketch:

int relay = A2; // the pin that the LED is attached to
int sensor = A0; // the pin that the sensor is attached to
int val = 0; // variable to store the sensor status (value)

void setup() {
Serial.begin(9600);
pinMode(relay, OUTPUT); // initialize LED as an output
pinMode(sensor, INPUT); // initialize sensor as an input
Serial.println("Initializing Sensor");
delay(60000);
// Serial communication for debugging purposes

}
void loop(){
val = digitalRead(sensor); // read sensor value
Serial.println(val);
if (val == HIGH) { // check if the sensor is HIGH
digitalWrite(relay, LOW); // turn Relay ON --------This may be inverted for some controller units already made (High)
Serial.println("Relay On");
delay(1000); // delay 1 second
digitalWrite(relay, HIGH); // turn Relay OFF --------This may be inverted for some controller units already made (Low)
Serial.println("Relay Off");
delay(10000); // delay 10 seconds
}
}

I put in the serial lines for troubleshooting. This seems like a fairly simple sketch. I didn't think that the Analog pin would matter since it can be defined as a digital pin.

Welcome to the Arduino forum. Looks like you have been using the Serial.println() properly to debug your code. So, that only leaves the physical connections and you did not include a schematic showing ALL the hardware and their connections.

Paul

All,

Here is my diagram and the mess of wires for the whole system. Sorry the Diagram is done in paint.
Interesting enough, if I don’t have the sensor connected, the relay will sometimes go off as well. I checked all of the connections, and they show continuity.

I see you are using 6 volts to Vin. Check the NANO specs. I think it needs at least 7.5 volts. If the wires between the sensor and the Nano are very long, they are operating an an antenna, picking up noise impulses from somewhere. When you disconnected the sensor, did you disconnect at the sensor or at the Nano?

Paul

Paul_KD7HB:
I see you are using 6 volts to Vin. Check the NANO specs. I think it needs at least 7.5 volts.

Pin 30 VIN takes 6-20v.

I would look at not powering the sensor and the relay from the Nano. Power them directly and only receive and send signals from the microcontroller.

Just a note. It is much easier for the forum members to read your code if you enter it with code tags.

i.e. use the </> icon when entering code.