Arduino Frequency Meter

Hello all, I’m new to Arduino and was making a frequency counter using pulseIn function. The problem i’m facing is that the output values are very absurd…such as 66334, 883339,12, 34 etc and not the actual frequency. I’m using 1Khz square wave from 555 (shows perfect on CRO). Below is the code: Please help me out:

int in_pin=2;
float t_high;
float t_low;
float t_total;
float freq;
void setup()
{
pinMode(in_pin,INPUT);
Serial.begin(9600);
}

void loop()
{
t_low=pulseInLong(in_pin,HIGH);
t_high=pulseInLong(in_pin,LOW);
t_total=t_high + t_low;
freq= 1000000.0/t_total;
Serial.println(freq);
}

Where is 'pulseInLong" defined?

Edit: well, that’s a new one on me.

Sorry.

Possibly I'm misreading it, but the documentation for pulseInLong says:

Reads a pulse (either HIGH or LOW) on a pin. For example, if value is HIGH, pulseInLong() waits for the pin to go from LOW to HIGH, starts timing, then waits for the pin to go LOW and stops timing

In other words, it is edge-triggered. Hence:

  t_low=pulseInLong(in_pin,HIGH);
  t_high=pulseInLong(in_pin,LOW);

the first call to pulseInLong() should return after the HIGH->LOW edge has been detected.

However, the next call is going to be looking for the same edge in order to start timing, i.e. it is looking for the pin going from HIGH to LOW. But that edge already occurred, because we used it to trigger the return from the previous pulseInLong call. So I think you end up capturing the "high" part of one waveform, and the "low" part of the following one.

So that seems like a potential problem with the approach, although it doesn't explain why you're getting totally bonkers values out. Have you tried just logging the t_high and t_low values to see whether they make any sense?

I've heard of time drifting ... but not floating

dlloyd:
I've heard of time drifting ... but not floating

Yeah, I wondered about that too. I assume the idea is to use a better type for the division that comes later.

I tried your code here on an Uno with a Mega generating a ~1kHz waveform and it seemed to report the correct values.

.
.
.
984.25
988.14
984.25
988.14
992.06
984.25
984.25
980.39
980.39
984.25
988.14
988.14
984.25
.
.
.

O-scope shows 984.9kHz.

Are you certain about the quality of the 555 output?

Is the 555 circuit at the same ground potential as the Arduino?

Are you sure you're connecting the 555 output to pin 2 of your Arduino?

What Arduino are you using?

Thank you for reply guys... actually I'm using Arduino Nano and intend to make RPM meter using ir sensor...and sorry i wrote t_low in place of t_high and vice versa.
The ir sensor produces a low output when it detets fan blade and a high output when the fan blade passes...so basically i should get a square wave. But still I'm getting absurd values...any ideas??

kzkhan98:
Thank you for reply guys… actually I’m using Arduino Nano and intend to make RPM meter using ir sensor…and sorry i wrote t_low in place of t_high and vice versa.
The ir sensor produces a low output when it detets fan blade and a high output when the fan blade passes…so basically i should get a square wave. But still I’m getting absurd values…any ideas??

Is your fan blade plastic or metal? Makes a big difference.
Paul

What does it look like on the scope?

Paul_KD7HB:
Is your fan blade plastic or metal? Makes a big difference.
Paul

Plastic

TheMemberFormerlyKnownAsAWOL:
What does it look like on the scope?

Square wave

I can’t really see how a propeller gives a square wave - I would expect a rectangular wave, unless the blade is unusually broad, or your sensor is very close to the hub.

kzkhan98:
Plastic

IS it opaque or transparent to IR?
Paul

Paul_KD7HB:
IS it opaque or transparent to IR?
Paul

opaque(black colour)

Hi,
If you are using LM555 as your frequency generator for testing, have you got the gnd of the LM555 connected to the gnd of the Nano.

Can you post a circuit diagram please?
NOT a Fritzy image!

Thanks.. Tom... :slight_smile:

kzkhan98:
opaque(black colour)

just, don't be so sure ... half of the plastic that appear totally black to visible light, are not totally opaque (and in some cases, transparents) to IR ... :wink:

Also, your "variable" result make me think that there is some disturbs or some false contact somewhere ... other than a correct schematic, as TomGeorge say, may be good to have a pair of clear pics of the assembly ...

Hello all, I'm in a huge problem. I'm trying to measure rpm using ir sensor and arduino nano. The code written is fine according to me but i get very weird values in the output. Have a look: (Only FAN OFF is correct):

FAN OFF
FAN OFF
FAN OFF
504197
2142857
372668
1428565
2862
779220
2931
1071428
2857140
857142
357137
3102

Here is the code:

int ir_pin=2;
unsigned long t_high;
unsigned long t_low;
unsigned long t_total;
unsigned long freq;
void setup()
{
pinMode(ir_pin,INPUT);
Serial.begin(9600);
}

void loop()
{
t_low=pulseIn(ir_pin,LOW);
t_high=pulseIn(ir_pin,HIGH);
t_total=t_high + t_low;
freq = 1000000/t_total;
Serial.println(freq);
if (t_total == 0)
Serial.println("FAN OFF");
else
{
Serial.println((freq*60)/7); //rpm formula for 7 blade fan...I printed normal frequency values too but they were
delay(500); // also incorrect ://
}
}

When i measure the IR output through dso, i get the correct frequency and rpm. I'm attaching the images of my setup and the dso output. Please help me out.

delay(500) ?
the answer to your problem lies in "blink-without-delay"
check your examples and there is lots of help on-line with tutorial and videos, etc

you scope lists mS in each of the first 3 lines.
your delay is 500 mS, so your delay is longer than any reading you want

frequency requires time be part of the equation

start note start time
count
if 10 seconds then end

how many did you count in 10 seconds ?

or

start note start time
count to 100
end note end time

how long did it take to get to 100 ?

dave-in-nj:
delay(500) ?
the answer to your problem lies in blink without delay.
check your examples and there is lots of help on-line with tutorial and videos, etc

you scope lists mS in each of the first 3 lines.
your delay is 500 mS, so your delay is longer than any reading you want

frequency requires time be part of the equation

start note start time
count
if 10 seconds then end

how many did you count in 10 seconds ?

or

start note start time
count to 100
end note end time

how long did it take to get to 100 ?

what are you trying to say?? I don;t understand anything...okay lets say the "delay(500)" is causing problem...but the normal frequency values are also absurd?? what about that?

I would start by printing the values of t_low and t_high to see if they are correct.