I am working on a period counter by the arduino uno and i try to use the pulseIn function to measure the how long is the rising time of a square wave with 50 HZ and display the value on a LCD displayer.
However when I input a 50.6 hz to pin5 the output value shown on the lcd displayer is 8117 .
Is there anyways, i can change it into decimal value and display 0.0198 on the displayer?
However when I input a 50.6 hz to pin5 the output value shown on the lcd displayer is 8117 .
Is there anyways, i can change it into decimal value and display 0.0198 on the displayer?
The output of the pulseIn() function is the time in microseconds that it took for the pin to go (or be) LOW, then go HIGH, then go LOW.
8117 is a decimal number, in that it isn't hexidecimal or octal or binary. So that part is already handled. If you want to display 0.0198 on the LCD, I'd suggest that you change:
lcd.print(duration);
to
lcd.print("0.0198");
Though why you would want to do that is a mystery.
Either the pulse is not what you think it is or the LCD is not displaying the number correctly.
As I suggested previously, what number is displayed if you print it to the serial monitor - if the value is the same then we know your assumption about the pulse is wrong, if its different we know that the LCD is not printing the number correctly
kk6682:
I want to print out the period of the signal .
which is equal to 1/50.6=0.197
Check your math as that should = 0.0197 seconds, so for one half the period (you are only measuring the positive half of the signal frequency) would be = to .009881 seconds or 9881 usec. So you are measuring about 18% fast which could be just not having an accurate signal generator or possibly noisy zero crossing detection causing an inaccurate 50% duty cycle, and you are only taking a single snap shot measurement rather then say taking a few and averaging them. I played with the pulse in command some a long while back and was not too pleased with it, as I never seemed to get real stable results on a constant frequency input, but never tracked down the cause of the variation even though I was using a good quality signal generator. Possibly millis and micros interrupts could be 'skewing' the results randomly? Lefty
I want to know that is the value of duration equal to the length of the rise time ?
which is equal to half of the period 0.197/2.
Now the displayer just shown as 8117
Is there anything i can convert the value from 8117 to the period
Measuring the duration of a wave (or half wave) does not strike me as a good way to measure the frequency of a signal, because it's very susceptible to minute changes in the timing of detecting the start and end conditions.
A far better way IMO is to measure the time taken to receive a number of pulses (and make the number high enough that the error due to timing resolution acceptably small) or count the number of pulses received in a period (and make the length of the period long enough that the error due to timing resolution and reception of incomplete waves is acceptably small).
I think either way can be valid, it depends largely on the response time you need. In the past I've done systems that "cross over" from counting to timing at a certain point to keep the display updating at a reasonable rate.
At 50Hz I think you are in a grey area unless you are happy with 10-second or worse display updates. With a 1-second update you would have a 2% jitter.
That said the best way (easiest to get an accurate result) would be to count for a long(ish) period.
New to the forum here, but since one of the projects I want to get into is going to be based on reading input frequencies, I wanted to participate.
Couldn't he add:
duration = duration + pulseIn(pin, LOW);
Right after reading the pulseIn(pin, HIGH)? The first thing I noticed was he was only reading half of the wave and likely assuming it was a perfect 50% duty cycle.
I get that, but I was more getting at the fact that reading only the high time isn't going to tell you the actual period, inaccuracies aside. Graynomad said what I was thinking... I just wanted to confirm that'd be the right approach in measuring it.