I have borrowed three different PIR sketches with the same results; the sensor initially is active and fires the 12v relay once, or even several times, but then the sensor becomes inactive and the relay remains on, that is the motor runs continuously. The Arduino is connected to a BC547 transistor/1K resistor. These sketches originally are for simple demo, using LED on D13. When I try this as a test, all works well but then, when the output is changed to the relay, all goes bananas. Any idea what I am doing wrong?
/* PIR sensor tester*/
int PIRpin = 8; // choose the pin for the relay
int inputPin = 3; // choose the input pin (for PIR sensor)
int pirState = LOW; // we start, assuming no motion detected
int val = 0; // variable for reading the pin status
void setup() {
pinMode(PIRpin, OUTPUT); // declare LED as output
pinMode(inputPin, INPUT); // declare sensor as input
Serial.begin(9600);
}
void loop(){
val = digitalRead(inputPin); // read input value
if (val == HIGH) { // check if the input is HIGH
digitalWrite(PIRpin, HIGH); // turn LED ON
if (pirState == LOW) {
// we have just turned on
Serial.println("Motion detected!");
// We only want to print on the output change, not state
pirState = HIGH;
}
} else {
digitalWrite(PIRpin, LOW); // turn LED OFF
if (pirState == HIGH){
// we have just turned of
Serial.println("Motion ended!");
// We only want to print on the output change, not state
pirState = LOW;
}
}
}