pulseIn() function returns zero periodically

Hi everyone,

I am using a simple sketch to measure the length of the pulse for both HIGH and LOW. Some times the HIGH or LOW length is zero. Did anyone had a similar issue?

This is the sketch

int pin = 0;

unsigned long duration_H;
unsigned long duration_L;
unsigned long counter;
//unsigned long timeout = 1000;



void setup()

{

  pinMode(pin, INPUT);

}



void loop()

{

  duration_H = pulseIn(pin, HIGH);
  duration_L = pulseIn(pin, LOW);
  
  if(duration_H>100){

    counter++;
  }
  Serial.print("Duration high: "); Serial.println(duration_H);
  Serial.print("Duration low: "); Serial.println(duration_L);
  
  Serial.print("Counter: "); Serial.println(counter);
  

}

Does this help?

Hi, thanks for you reply,

I have read this but actually don't help..

Did you read the part about it returning 0 when it times out? Only you know what pulse train it is that you're measuring but are you certain that the pulses are so regular that it can never time out? You could always add prints of millis() before and after the pulseIn(pin, HIGH) to get some idea.

Steve

Yes, I know about the time out. By default is one second and I am measuring a frequency of around 20Hz, so this is should not be the problem..

Maybe it is time to start looking at your signal with an oscilloscope.

this is should not be the problem

Let's not discount this just yet.

Nikosant03:
This is the sketch

Does Serial.print() work without Serial.begin()?

PaulRB:
Does Serial.print() work without Serial.begin()?

Good spot!

At least as interesting: using Serial prints, while reading pulseIn() on pin 0 (RX).