Finding rise time and amplitude

what you are getting is the time between the rising and falling edges
running on an ESP32

#define PB0 16
volatile int pwm_value = 0;
volatile int prev_time = 0;

void setup() {
  Serial.begin(115200);
  analogReadResolution(12);

  // when pin D2 goes high, call the rising function
  attachInterrupt(PB0, rising, RISING);
}

void loop() {
  if (pwm_value > 0) {
    Serial.println(pwm_value);
    delay(1000);
  }
}

void rising() {
  attachInterrupt(PB0, falling, FALLING);
  prev_time = micros();
}

void falling() {
  attachInterrupt(PB0, rising, RISING);
  pwm_value = micros() - prev_time;
  //Serial.println(pwm_value);
}

input on GPIO16 is a square wave frequency 10Hz period 100mSec
image

serial monitor displays time between rising and falling edges as 50mSec

50002
50002
50001
50002
50002
50002
50002
50002

it is not recommended to call Serial.print() and similar functions in interrupt routines, e.g. they cause an exception on the ESP32
generally save data to volatile variables and print in loop() (as in the above code)

1 Like