# Detecting Microsecond Pulse Duration

**URL:** <https://forum.arduino.cc/t/detecting-microsecond-pulse-duration/697522>\
**Category:** Sensors\
**Created:** [February 24, 2021, 11:17am UTC](https://forum.arduino.cc/t/detecting-microsecond-pulse-duration/697522 "2021-02-24T11:17:45Z")\
**Posts on this page:** 1\
**Showing post:** 10

<div class="post-metadata">

**Author:** ![srturner](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/srturner/32/549399_2.png) [@srturner](https://forum.arduino.cc/u/srturner)\
**Post date:** [February 25, 2021, 3:32pm UTC](https://forum.arduino.cc/t/detecting-microsecond-pulse-duration/697522/10 "2021-02-25T15:32:08Z")

</div>

Why did you switch from digital to analog reads? If your input signal reliably rises to the logic high state of the device you're using, I'd go back to digital read. I may not be understanding the whole purpose of your code, but I'd try something like:

```cpp
void setup() {
  unsigned long startTime = 0;
  unsigned long stopTime = 0;
}

void loop() {
  while (digitalRead(7)) {  //wait for low on pin 7
  }
  while (!digitalRead(7)) { //wait for high on pin 7
  }
  startTime = micros();     //high detected, set start time

  while (digitalRead(7)) {  //wait for low on pin 7
  }
  stopTime = micros();    //low detected, set stop time

  Serial.print(stopTime - startTime);
  delay(100); //or whatever
}

```

(Warning: untested and incomplete armchair code fragment. Worth what you paid for it...)  
S.

---

_[View the full topic](https://forum.arduino.cc/t/detecting-microsecond-pulse-duration/697522)._
