Sporadic Values from pulseIn

While using pulseIn to calculate the frequency of an AC audio wave which is converted into a square wave with an LM339 I get values that are much different while the wave stays constant.

int duration;

int setup()
{
  pinMode(2, INPUT);
  Serial.begin(9600);
}

int loop()
{
  duration = pulseIn(2, LOW);
  Serial.println(1000000 / (duration * 2));
  delay(100);
}

Here's what I get in the Serial Monitor:

919
934
250000
924
931
920
932
932
919
934
931
919
932
934
919
927
920
250000
927
919
919
500000
919
932
932

This is while feeding a 900 Hz Sine wave into the LM339 from the sound card.

Thanks

Did you look at the documentation for the pulseIn() function? Hint. The function does not return an int.

The function does not return an int.

Good Call

I simplified the code so that it does not do anything with what pulseIn returns, in didn't seem to help much.

unsigned long duration;

int setup()
{
  pinMode(2, INPUT);
  Serial.begin(9600);
}

int loop()
{
  duration = pulseIn(2, LOW);
  Serial.println(duration);
  delay(100);
}

In the serial monitor I got:

318
319
327
327
326
319
327
1
327
326
321
327
326
329

It would probably take a scope to find out what you're really getting.

How about decoupling on the LM339?
What value of pull up resistor are you using on the open collector outputs of that chip?

GoForSmoke:
It would probably take a scope to find out what you're really getting.

On my scope the LM339 follows the audio very closely and neither are "jumpy".

Grumpy_Mike:
How about decoupling on the LM339?
What value of pull up resistor are you using on the open collector outputs of that chip?

I am using a 10K Ohm resistor to positive and a 4.7K Ohm resistor in series to the 10K to ground.

These sporadic values seem to appear more often at lower frequencies.

unsigned long duration;

int setup()
{
  pinMode(2, INPUT);
  Serial.begin(9600);
}

int loop()
{
  duration = pulseIn(2, LOW);
  Serial.println(1000000 / (duration * 2));
  delay(100);
}

While running this code at 800 Hz I get:
771
776
781
500000
771
500000
781
781
500000
771
771
776
780
781
775

At 1500 Hz I get:
1445
1440
1470
1470
1440
1436
1440
1445
1440
1470
1470
1470
1440
1440
1440
1445

I should also mention I am using Arduino IDE version 1.0.1 with a BoArduino.

Does anyone know if 1.0.1 fixed the timeout bug in pulseIn()?

  • Modifying pulseIn() to wait for a transition to start timing (i.e. ignoring
    any pulse that had already started when the function was called).

Is this the bug you were referring to? I found this in the revisions.txt file for 1.0.1 under Arduino 0013.

PetaVolt:
Is this the bug you were referring to? I found this in the revisions.txt file for 1.0.1 under Arduino 0013.

A couple of months ago Jantje noticed a weird behavior with the timeout. Timeouts would occur after 200ms regardless of what pulseIn() was given.

http://arduino.cc/forum/index.php/topic,102504.0.html
Skip to: pulseIn timeout is puzzling me (SOLVED) - #7 by system - Programming Questions - Arduino Forum

I am using a 10K Ohm resistor to positive and a 4.7K Ohm resistor in series to the 10K to ground.

That sounds wrong.

Output of LM339 direct to the digital input and one end of the 10K, the other end of the 10K to +5V.
Those two extra resistors sound like they will limit the high voltage, it might just be on the limit of triggering the logic input.
And a 0.1uF across the LM339's power pins?

I am using a 10K Ohm resistor to positive and a 4.7K Ohm resistor in series to the 10K to ground.

I was wrong about the 4.7K Ohm resistor, it was 100K.

I took out the 100K out so now all I have is a 10K pull up resistor on the output of the LM339 and I still have the problem. However, I am still getting the full 5 volt output as well as a ground low from the LM339.

Ok it is better left off.

And a 0.1uF across the LM339's power pins?

I say this because the output signal could be ringing which would explain the odd value of 1 you get.

And a 0.1uF across the LM339's power pins?

I put one in and it didn't seem to help much:

1048
1039
1030
500000
1039
1048
1041
1046
1028
1041
1030
1050
1033
1033
500000

Can you post what the scope shows going into the pin?

This is at 1000 Hz.

I put one in and it didn't seem to help much:

Yes it did it got rid of the spurious reading of 1.
Your main problem seems to be the spurious readings of 500000.
The other readings are not going to be rock steady you know.

Grumpy_Mike:

I put one in and it didn't seem to help much:

Yes it did it got rid of the spurious reading of 1.
Your main problem seems to be the spurious readings of 500000.

In the last code I posted I added some math to output the frequency rather than what pulseIn() returned so the readings of 1 turned into 500000.