PIR to relay problem

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;
}
}
}

What are the voltages you are seeing at base of BC547 when its active and inactive?

I am having a similar problem. I'm using a pir sensor, checking for motion every 2 seconds, and when motion is detected I want to turn on some 12V LED lights and hold them on for a time (15 seconds). After the delay the sensor goes back to checking for motion. I have tried a relay to switch the 12V LEDs and when the relay is connected to Pin 13 the pir keeps coming back on immediately after the 15 second delay. (It goes off and then pops back on again) I have also tried using a MOSFET transistor to switch the LEDs. If I don't connect pin 13 to the relay or the MOSFET the sketch works perfectly, but as soon as I hook up the lights the pir keeps tripping whether there is motion or not.

Motion.txt (887 Bytes)

Are you driving the relay with the transistor? Please post your schematic.