RCWL-0516 triggering relay

Hi,

Pretty new to Arduino but I managed to construct an RCWL-0516 and 5v relay circuit to switch on a light but am having some problems.

The sensor activates the relay and about 50% of the time it switches itself off again after 3 seconds of not seeing any movement. The other 50% of the time, it switches off then immediately switches back on again. It'll do this a number of times then switch off and stay off, until motion is sensed again. It's almost as if the RCW is storing movement info and sending it to the arduino as soon as the 3 second delay is over.

Is there anything I can do to my sketch to make the relay behave like I want?

int Sensor = 2;     // RCWL-0516 Input Pin
int LED = 13;       // LED Output Pin
int RELAY = 12;       // RELAY Output Pin

int sensorval = 0;  // RCWL-0516 Sensor Value


void setup() {
 Serial.begin(9600);
 pinMode (Sensor, INPUT);  // RCWL-0516 as input
 pinMode (LED, OUTPUT);    // LED as OUTPUT
 digitalWrite(LED, LOW);   // Turn LED Off
 pinMode (RELAY, OUTPUT);    // RELAY as OUTPUT
 digitalWrite(RELAY, LOW);   // Turn RELAY Off
}

void loop(){

 sensorval = digitalRead(Sensor);  // Read Sensor value
 
 if (sensorval == HIGH) {        
   Serial.print(sensorval);
   digitalWrite(LED, HIGH);  // Turn LED On
   digitalWrite(RELAY, HIGH);  // Turn RELAY On
   delay(3000);
   digitalWrite(LED, LOW);
   digitalWrite(RELAY, LOW);
   }
 
}

I used:

RCWL-0516
Arduino Nano
5v relay

Thanks in advance for any insight.

Please edit your post and insert code tags!

The RCWL-0516 detects tiny changes in it's radar wave field. Switching on and off a relay inside it's detection range probably causes such changes. You either have to shield the two parts very effectively or add some time after switching the relay during that no sensor input is used. The easiest way to achieve that (but quite bad code) is to add a delay(3000) after switching the relay off.

pylon:
Please edit your post and insert code tags!

The RCWL-0516 detects tiny changes in it's radar wave field. Switching on and off a relay inside it's detection range probably causes such changes. You either have to shield the two parts very effectively or add some time after switching the relay during that no sensor input is used. The easiest way to achieve that (but quite bad code) is to add a delay(3000) after switching the relay off.

Thanks for the reply. Code tags inserted and delay(3000) added to sketch. Fingers crossed.