Go Down

### Topic: pulseIn() function problem for measure frequency (Read 2993 times)previous topic - next topic

#### kk6682

##### Nov 05, 2012, 06:50 am
hi all,

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?

Here are my Code

#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 6, 4, 9,

int pin = 5;
unsigned long duration;

void setup()
{
pinMode(pin, INPUT);

lcd.begin(8, 2);
}

void loop()
{
duration = pulseIn(pin, HIGH);
lcd.clear();
lcd.setCursor(0,0);

lcd.print(duration);
delay(1000);
}

THANKS A LOTS

#### DuaneB

#1
##### Nov 05, 2012, 07:09 am
what happens if you print it to serial instead ?

Duane B

rcarduino.blogspot.com
Read this
http://rcarduino.blogspot.com/2012/04/servo-problems-with-arduino-part-1.html
then watch this
http://rcarduino.blogspot.com/2012/04/servo-problems-part-2-demonstration.html

Rcarduino.blogspot.com

#### PaulS

#2
##### Nov 05, 2012, 11:54 am
Quote
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:
Code: [Select]
`    lcd.print(duration);`
to
Code: [Select]
`    lcd.print("0.0198");`

Though why you would want to do that is a mystery.

#### kk6682

#3
##### Nov 05, 2012, 02:58 pmLast Edit: Nov 05, 2012, 03:00 pm by kk6682 Reason: 1
I want to print out the period of the signal .

which is equal to 1/50.6=0.197

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

#### DuaneB

#4
##### Nov 05, 2012, 03:02 pm
Your doing something wrong somewhere.

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

Duane B
Read this
http://rcarduino.blogspot.com/2012/04/servo-problems-with-arduino-part-1.html
then watch this
http://rcarduino.blogspot.com/2012/04/servo-problems-part-2-demonstration.html

Rcarduino.blogspot.com

#### Graynomad

#5
##### Nov 05, 2012, 03:09 pm
You period is ~10mS that's 10,000 so there's a problem with the 8117 number somewhere.

Quote
how long is the rising time of a square wave with 50 HZ

At this level "rise times" do not enter into it. How are you generating this square wave? I suspect it has something to do with that.

What happens if you measure the LOW half of the waveform, do the two numbers add up to 20,000?

____
Rob
Rob Gray aka the GRAYnomad www.robgray.com

#### retrolefty

#6
##### Nov 05, 2012, 04:23 pmLast Edit: Nov 05, 2012, 04:25 pm by retrolefty Reason: 1

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

#### PeterH

#7
##### Nov 05, 2012, 05:34 pm
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 only provide help via the forum - please do not contact me for private consultancy.

#### Graynomad

#8
##### Nov 05, 2012, 05:50 pm
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.

_____
Rob
Rob Gray aka the GRAYnomad www.robgray.com

#### dsteck

#9
##### Nov 05, 2012, 07:17 pm
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.

#### DuaneB

#10
##### Nov 05, 2012, 07:26 pm
If you read through some of the previous responses you will see that that approach will double any errors.

Duane B

rcarduino.blogspot.com
Read this
http://rcarduino.blogspot.com/2012/04/servo-problems-with-arduino-part-1.html
then watch this
http://rcarduino.blogspot.com/2012/04/servo-problems-part-2-demonstration.html

Rcarduino.blogspot.com

#### dsteck

#11
##### Nov 05, 2012, 07:30 pm

If you read through some of the previous responses you will see that that approach will double any errors.

Duane B

rcarduino.blogspot.com

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.

#### DuaneB

#12
##### Nov 05, 2012, 07:35 pm
Realistically if the OP is after frequency he only needs to measure the time between rising edges using an interrupt.

Duane B

Read this
http://rcarduino.blogspot.com/2012/04/servo-problems-with-arduino-part-1.html
then watch this
http://rcarduino.blogspot.com/2012/04/servo-problems-part-2-demonstration.html

Rcarduino.blogspot.com

#### Paul Stoffregen

#13
##### Nov 09, 2012, 02:54 pm
The best way to measure a pulse width is using the 16 bit timer "input capture" feature.

Maybe you'll find the code in my FreqMeasure library helpful?

http://www.pjrc.com/teensy/td_libs_FreqMeasure.html

Go Up

Please enter a valid email to subscribe

### Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy