Sound sensor to meassure the length of the buyyer sound

I have following sound sensor

I wanted to write the code that would measure the length of the buzzer signal. Signal is triggered from different circuit.
But the sound sensor does not keep high signal while the signal is on.
Am I using this sensor wrong. the connection is simple 5vto 5v gnd to gnd and signal pin to arduino pin d2. I am using the interrupt to capture the timstamp of signal beginning and end.

boolean signal_received=false;
unsigned long impuls_begin;
unsigned long last_ISR_run;
unsigned long impuls_end;

void setup() {
  Serial.begin(9600);
  attachInterrupt(digitalPinToInterrupt(2), incoming_signal, RISING);
  pinMode(2,INPUT);
}

void incoming_signal(){
  if(millis()-last_ISR_run>10){   
    if (digitalRead(2)==HIGH){
      impuls_begin=millis();
      last_ISR_run=millis();
    } 
    if (digitalRead(2)==LOW){
      signal_received=true;
      impuls_end=millis();
      last_ISR_run=millis();
    }
  }
  
}


void loop() {
  if (signal_received) {
    Serial.println(impuls_end-impuls_begin);
    signal_received=false;
  }  
}

Please advice if you see anything wrong. Or maybe this sound sensor is just not good for this purpose. Let me know how would you do it.

Hi,
the "void incoming_signal()" function is called by
interrupt, so you shouldn't use millis() inside function called by interrupt.
When an interrupt occurs, new interrupts are blocked,
and since millis() uses interrupts, it will not work correctly inside functions called by interrupt.

The function called by an interrupt must be as simple as possible, as it blocks many processor resources.

I haven't tested the modification I made to your code, but test it there and then tell me the result.

boolean signal_received = false;
unsigned long impuls_begin;
unsigned long last_ISR_run;
unsigned long impuls_end;
bool flag = false;

void setup() {
  Serial.begin(9600);
  attachInterrupt(digitalPinToInterrupt(2), incoming_signal, RISING);
  pinMode(2, INPUT);
}

void incoming_signal() {
  flag = true;
}

void loop() {
  if (signal_received) {
    Serial.println(impuls_end - impuls_begin);
    signal_received = false;
  }
  if (flag == true)
  {
    if (millis() - last_ISR_run > 10) {
      if (digitalRead(2) == HIGH) {
        impuls_begin = millis();
        last_ISR_run = millis();
      }
      if (digitalRead(2) == LOW) {
        signal_received = true;
        impuls_end = millis();
        last_ISR_run = millis();
        flag = false;
      }
    }
  }
}

Also any variable used in the ISR and in the main code should be declared as volatile in addition to the variable type.

There is absolutely no need to use interrupts to do this, a simple pole is sufficient.

The interrupt is set to "RISING" so you would expect Pin 2 to always be HIGH in the ISR. You probably intended to set the interrupt to "CHANGE" instead.

That is not correct. There is no problem using millis() within an interrupt. It is delay() that should not be used inside an interrupt, maybe that is what you were thinking of.

Interrupt code should be kept short and no loops or functions should be used which might stop the code from finishing quickly, such as delay() or Serial.print(). But millis() is fine, it runs very quickly.

It is true that millis() will never change while interrupt code is running, but that is not a concern. If interrupt code waits for millis() to change, then that is as concern, not only because it will never change, but also because clearly the interrupt code would be running for far too long a time, even if it did change.

I did use the CHANGE mode and then trying something I changed it to RISING. With CHANGE mode it still does not work.
The signal at pin 2 while the buyyer is on is not constant at HIGH it fluctuates and this is why this code does not work. And the ISR is executed TOO many times.

Any suggestion how to tweak this to be able to capture the lenght of the buzzer signal?

It looks like you are suffering from decoupling problems.
See De-coupling
Try and add a large capacitor across 5V and Gnd both on the buzzer and the Sound sensor. As close to each component as you can manage.

Is this a microphone? If so is its output an AC signal? You would need to rectify that first to sense a continuous sound.