Go Down

Topic: pulseIn() function problem for measure frequency (Read 1 time) previous topic - next topic

kk6682

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, 8)

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


PaulS

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 pm Last 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

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

Go Up