The Serial is being sent too many values

My code is meant to detect if there is any movement registered on my movement sensor, it is meant to just send 'S' to the serial once however it is sending 7 time, I was wondering how to make it so it just sends once.

const int sensor = A0;

void setup() {
  Serial.begin(9600);
  
}

void loop() {
  int sensorVal = analogRead(sensor);
  if(sensorVal > 2){
    Serial.write("S");
    delay(500);
  }
  
}

Any help would be appreciated.

Take a look at the state change detection example in the IDE.

You need to print when sensorVal becomes greater than 2 rather than when it is greater than 2

Look at the example suggested by AWOL