Arduino Frequency Meter

A common way to do this is to use an interrupt. Just detect a rising edge and increment a count. Once in a while using millis for timing, calculate the frequency and zero the count. Don't forget to make the count variable volatile.

wildbill:
A common way to do this is to use an interrupt. Just detect a rising edge and increment a count. Once in a while using millis for timing, calculate the frequency and zero the count. Don't forget to make the count variable volatile.

Like this??
The IR pin output is 4.75V when no object is detected and 0 when object is detected. Hence I've taken falling edge. Will this code work?? (taken directly from a website)
int ir_pin=2;
float count=0;
int rpm;
int oldtime=0;
int _time;
void RPM()
{
count++;
}
void setup()
{
pinMode(ir_pin,INPUT);
Serial.begin(9600);
attachInterrupt(0,RPM,FALLING);
}
void loop()
{

delay(1000);
detachInterrupt(0);
_time=millis()-oldtime;
rpm=(count/_time)*60000;
oldtime=millis();
count=0;
Serial.println(rpm);
attachInterrupt(0,RPM,FALLING);
}

That's a crude version of the method, yes. Try it.

Note that you may get integer division effects here:

rpm=(count/_time)*60000;

But it should get you started.

Why is count a float?
Why isn't it qualified as volatile?

kzkhan98:
Like this??
The IR pin output is 4.75V when no object is detected and 0 when object is detected. Hence I've taken falling edge. Will this code work?? (taken directly from a website)

Please read how to use 'code tags' to post code. there are instructions in one of the sticky links at the top of every forum.

also, when referencing a website, it really helps to post a link to that website.

I took only part of your code and added comments.

this part ;
attachInterrupt(0,RPM,FALLING);
calls the void RPM() function to allow count to increment

void RPM()
{
count++;  // increments count when called from the interrupt
}



void loop()
{
  
delay(1000);
detachInterrupt(0);        //  stops interrupt while doing the math and printing
_time=millis()-oldtime;         
rpm=(count/_time)*60000;         // calculates RPM 
oldtime=millis();             
count=0;   //  re-zeros the count use in rpm()  after math is done
Serial.println(rpm);
attachInterrupt(0,RPM,FALLING);   //  allows the interrupt to start incrementing
}

wildbill:
A common way to do this is to use an interrupt. Just detect a rising edge and increment a count. Once in a while using millis for timing, calculate the frequency and zero the count. Don't forget to make the count variable volatile.

count needs to be set as an int or long ?
if you do not assign a value the very first reading, and only the very first reading on startup will be off, for one second. if you are concerned, make it =0 in startup()

int ir_pin=2;
//   float count=0;   
int count ;  //   need to create this variable before using it.
int rpm;
int oldtime=0;
int _time;

void RPM()
{
count++;
}

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.