0

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

int senserpin=8;
int sensordelay=1000; 
float duration;
float freq;

void setup{
pinMode(senserpin,INPUT);
serial.begin(9600);
}

void loop()
{
duration=pulsein(senserpin,high)
freq=1/(duration*1000000)// multiply duration *1000000 to convert from microseconds to seconds 
serail.print("frequency");
serail.print(freq);

delay(sensordelay)
}
1
  • 1
    Please provide code that compiles (if how to fix compiler errors is not your question) Commented May 15, 2018 at 10:52

2 Answers 2

1

You measured only high time. You need to measure both for calculating frequency and period. I hope this will work for you.

int highTime;    //integer for storing high time
int lowTime;     //integer for storing low time
float period;    // integer for storing period
float freq;      //storing frequency

void setup()
{
    pinMode(8,INPUT);  //Setting pin as input
}

void loop()
{
    highTime=pulseIn(8,HIGH);  //read high time
    lowTime=pulseIn(8,LOW);    //read low time
    period = highTime+lowTime; // Period = Ton + Toff
    freq=1000000/period;       //getting frequency with totalTime is in Micro seconds
    delay(1000);
}
Sign up to request clarification or add additional context in comments.

1 Comment

pulseIn returns an unsigned long
0

Frequency is the time between two identical events (e.g. from one rising edge to the next rising edge)

pulseIn only measures the time of a part of that period (e.g. how long the signal is HIGH)

You either know that HIGH phase has the same duration as LOW phase, or you have to measure both.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.