pulseIn Timeout = 69% of What You Specify

 
  digitalWrite(LED_BUILTIN, HIGH);
  ET = micros();
  t1 = pulseIn(inPulse, LOW,1000000);
  t2 = micros()-ET;
  Serial.print(t1);
  Serial.print("  ");
  Serial.println(t2);
  digitalWrite(LED_BUILTIN, LOW);
  delay(2000);

Monitor:
0 691612
0 691612
0 691604
etc.

Whatever I specify for the pulseIn timeout is set to 69% of that. Using Uno. Just me or do you see this too?

(Sorry - left this out:

unsigned long ET = micros();  //
unsigned long t1;         //
unsigned long t2;
const long outPulse = 8;
const long inPulse = 7;


void setup() {
  Serial.begin(115200);
  pinMode(inPulse, INPUT_PULLUP);
  pinMode(outPulse,OUTPUT);
  pinMode(LED_BUILTIN, OUTPUT);

}

void loop() {

Since it is only a snippet of code and not a complete sketch, nobody can see what you see since nobody can compile the code and test it.
Also,what is connected to inPulse?

Posted the rest of the code while you were typing. Nothing connected to inPulse, configured with pullup.

You do get that behavior in simulation as well: relays - Wokwi ESP32, STM32, Arduino Simulator

If you look at the documentation, it does say this: "The timing of this function has been determined empirically and will probably show errors in longer pulses." so it doesn't sound too exact.

Try using pulseInLong() instead of pulseIn().

That did the trick. Thanks John.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.