Pages: [1]   Go Down
Author Topic: pulseIn() function problem for measure frequency  (Read 1425 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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, smiley-cool

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
Logged

Dubai, UAE
Offline Offline
Edison Member
*
Karma: 22
Posts: 1675
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

what happens if you print it to serial instead ?

Duane B

rcarduino.blogspot.com
Logged


Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 615
Posts: 49388
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
    lcd.print(duration);
to
Code:
    lcd.print("0.0198");

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

Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

 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
« Last Edit: November 05, 2012, 09:00:40 am by kk6682 » Logged

Dubai, UAE
Offline Offline
Edison Member
*
Karma: 22
Posts: 1675
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged


nr Bundaberg, Australia
Online Online
Tesla Member
***
Karma: 129
Posts: 8529
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Rob Gray aka the GRAYnomad www.robgray.com

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 361
Posts: 17294
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

« Last Edit: November 05, 2012, 10:25:49 am by retrolefty » Logged

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12630
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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).
Logged

I only provide help via the forum - please do not contact me for private consultancy.

nr Bundaberg, Australia
Online Online
Tesla Member
***
Karma: 129
Posts: 8529
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Rob Gray aka the GRAYnomad www.robgray.com

Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Dubai, UAE
Offline Offline
Edison Member
*
Karma: 22
Posts: 1675
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Duane B

rcarduino.blogspot.com
Logged


Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Dubai, UAE
Offline Offline
Edison Member
*
Karma: 22
Posts: 1675
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Realistically if the OP is after frequency he only needs to measure the time between rising edges using an interrupt.

Duane B


Logged


0
Offline Offline
God Member
*****
Karma: 26
Posts: 610
Always making something...
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Logged

Pages: [1]   Go Up
Jump to: