Only print "Led On" once in the serial monitor


I am using RCWL-0516 sensor to detect motion and turn on Led. The code is working and turns on the Led. My issue is that it prints “Led On” continuously rather than just once when it turns on and then print turn off.

  int Sensor = 2;  //RCWL-0516 Input Pin
  int led = 6; //LED Pin

    void setup()
      pinMode(Sensor,INPUT);// motion sensor connected to analog 0
      pinMode(led, OUTPUT);
    void loop()
      if (digitalRead(Sensor) == HIGH )

      Serial.println( "Led On" ); 
      else {
        digitalWrite(led, LOW);
         Serial.println( "Led Off" ); 

You need to print when the pin BECOMES high, not when the pin IS high.

Learn from the state change example in the Arduino IDE.

See the Edge Detection example.

Thank you @vaj4088 and @larryd it works the way I want it to now. :slight_smile:

Congratulations !