how to measure frequency value in arduino mega using pulseIn() function?

i am newbie in arduino and i have small project i read that pulsein() function return length of the pulse in microseconds and

frequency(HZ)=1/time(second)

so i hope below code to measure frequency value is correct otherwise please notify me

nt Htime;              //integer for storing high time
int Ltime;                //integer for storing low time
float Ttime;            // integer for storing total time of a cycle
float frequency;        //storing frequency

void setup()
{
    pinMode(8,INPUT);
    serial.begin(9600);
}
void loop()
{
Htime=pulseIn(8,HIGH);      //read high time
    Ltime=pulseIn(8,LOW);        //read low time
    
    Ttime = Htime+Ltime;
 frequency=1000000/Ttime;    //getting frequency with Ttime is in Micro seconds
Serial.println(frequncy)
  delay(1000);
}

Does pulseIn() return a float?

There are a number of spelling mistakes in your code. Have you tried compiling it ?

The code doesn't compile so it definitely won't work.

setup needs its parentheses (). I've never heard of serail.print() and I bet the compiler hasn't either.

Are you sure that 1 microsecond = 1 million seconds?

And by the way that formula for frequency only works if you have a true square wave. If the duty cycle isn't exactly 50% the frequency will be wrong.

Steve

hello
i made necessary modification

Hi,
Did you google

measuring frequency with pulseln

Have you looked here;

Code and circuit reference;
https://www.pjrc.com/teensy/td_libs_FreqCount.html

Tom... :slight_smile:

Did it work?

Note that it is rude to make large edits which invalidate replies already given. Post updated code in a new post.