Count how many pulses per second

Hi.
I'm sending in pulses on the anloginput on my arduino uno. It's not square shaped pulses more like sine wave shaped. When I read the analog value and send it to my serial monitor I can see that I get one high value (around 350). And a low value (0-3). Now i want to count how many pulses i get in per second. And I don't have any idea how to do that?

When I read the analog value and send it to my serial monitor I can see that I get one high value (around 350). And a low value (0-3).

It seems, then, that you are not sampling often enough. You should be getting a range of values between the low and high values, too.

Now i want to count how many pulses i get in per second. And I don't have any idea how to do that?

Note what time it is (millis()). Execute a wile loop that runs for one second (is now minus then greater than one second?). In that loop, count how many high values you get. At the end, print it out.

Firstly you need to sample the signal at least at twice the frequency of the signal itself (see Nyquist Shannon sampling theorem). Then you need to decide upon some threshold value.

When the value crosses this threshold in one specific direction (high to low, say), note the time. When it crosses again in the same direction, note the time again. Compare the two times, and you have the period of the waveform.

Take that period and find the reciprocal (1/period), and you have the frequency.

It takes about 105 microseconds to read analog and you should consider could that be enough to miss a transition?

Suppose you feed your pulses to an NPN transistor through a resistor, +5V to the collector and emitter to your pin to get a 0V-5V range. You can then read the pin as digital in 1 to 3 cycles, 3/16th microsecond, or use a pin change interrupt and IRQ to do the watching and counting for you.

Digital pins change from LOW to HIGH at a higher voltage than change from HIGH to LOW. What voltages depends on Arduino voltage but you are always sure to have crossed states that way.

majenko:
Firstly you need to sample the signal at least at twice the frequency of the signal itself (see Nyquist Shannon sampling theorem). Then you need to decide upon some threshold value.

When the value crosses this threshold in one specific direction (high to low, say), note the time. When it crosses again in the same direction, note the time again. Compare the two times, and you have the period of the waveform.

Take that period and find the reciprocal (1/period), and you have the frequency.

I'm afraid I'm not good enough at english to understand that wiki. And I don't know what sampling is.

Sampling is the reading of the ADC.

The theory is that basically you need to read the ADC at least twice as fast as the pulses come in - once to catch the "high" portion of the wave, and once for the "low" portion.

The faster you read the ADC the better.

You don't need to remember the values you read from the ADC, you're only looking for where the value changes from the "high" part of the pulse to the "low" part (or the other way around, it matters not, as long as you stick to one or the other).

Remembering the time at which it makes this change from high to low, or low to high, is the critical part. The faster you sample the ADC, the more accurate you can be at measuring this time.

If, for example, you have a signal that can contains pulses at up to 20 pulses per second you will need to read the ADC at least 40 times per second. If the pulses are narrow, you will need to read even more often to capture them.

A more reliable way might to be to use the internal "Capture/Compare" module which is perfectly suited to looking for when a pulse appears on an analogue line.

GoForSmoke:
It takes about 105 microseconds to read analog and you should consider could that be enough to miss a transition?

Suppose you feed your pulses to an NPN transistor through a resistor, +5V to the collector and emitter to your pin to get a 0V-5V range. You can then read the pin as digital in 1 to 3 cycles, 3/16th microsecond, or use a pin change interrupt and IRQ to do the watching and counting for you.

Digital pins change from LOW to HIGH at a higher voltage than change from HIGH to LOW. What voltages depends on Arduino voltage but you are always sure to have crossed states that way.

I have tried to feed it to an NPN, but the signal comes from the third pin on a CPU-fan and it looks like the signal is not strong enough. Becuase it dosn't work either.

majenko:
Sampling is the reading of the ADC.

The theory is that basically you need to read the ADC at least twice as fast as the pulses come in - once to catch the "high" portion of the wave, and once for the "low" portion.

The faster you read the ADC the better.

You don't need to remember the values you read from the ADC, you're only looking for where the value changes from the "high" part of the pulse to the "low" part (or the other way around, it matters not, as long as you stick to one or the other).

Remembering the time at which it makes this change from high to low, or low to high, is the critical part. The faster you sample the ADC, the more accurate you can be at measuring this time.

If, for example, you have a signal that can contains pulses at up to 20 pulses per second you will need to read the ADC at least 40 times per second. If the pulses are narrow, you will need to read even more often to capture them.

A more reliable way might to be to use the internal "Capture/Compare" module which is perfectly suited to looking for when a pulse appears on an analogue line.

How do I use the "Capture/Compare" module.

How do I use the "Capture/Compare" module.

On the Arduino I have no clue (I'm a PIC user primarily). Someone has probably written a library to do it. I know there is some sample code that uses it to work with an IR remote control on the playground. Arduino Playground - InfraredReceivers

This is propably to advanced for me. I just wanted to know how many rpm my fan was doing. But thanks for all the help.

himym:
I have tried to feed it to an NPN, but the signal comes from the third pin on a CPU-fan and it looks like the signal is not strong enough. Becuase it dosn't work either.

If you are seeing high values of 350 then you should be getting 1.5V or more. That is probably too much for direct to the NPN grid pin but just now I am not sure how much resistor should be between your fan and the transistor but if you have a 1k or better potentiometer then you can find out easily without Arduino.
Put the pot turned to be most ohms between the CPU fan and transistor grid. Then put 5V to the transistor collector. Then 220 ohm resistor to the transistor emitter and a led between that and ground. If the led does not flash, slowly turn the pot down until it flashes brightly. Whatever ohms the pot is then should work.

Arduino has for every pin a pin change interrupt possible. It will catch a change from digital HIGH or LOW and cause interrupt code you wrote to run. That code must be short, just to add 1 to a variable declared as volatile. The regular code is stopped during the time the interrupt code (called IRQ) runs and resumes when the IRQ is done. Your regular code can watch for 1 second and the variable will hold 2x the pulse frequency. If you go longer you can get more accuracy.

Sorry but your English is massively better than my Francais (in 40 years no parlez I have forgotten almost all) so I hope this is not too much trouble.

Just doing some experiments here.

I have a CPU fan, and I am seeing an AC waveform (in pulses) coming from the HE sensor. Passing that through a resistor into the base of a BC337, with a pot to set an offset to get a good operating point (I could calculate the right value, but there's no point just for this experiment - a pot will do), and I can get it to switch a digital input (pulled up) quite happily. It's a 12V fan running off 5V, and counting the transitions that occur in a second and dividing by 2 gives me 37rps. Doing it over a 10 second period and muliplying by 3 gives me 2214RPM. - until I stick my finger in there when it goes down :wink:

majenko:
Just doing some experiments here.

I have a CPU fan, and I am seeing an AC waveform (in pulses) coming from the HE sensor. Passing that through a resistor into the base of a BC337, with a pot to set an offset to get a good operating point (I could calculate the right value, but there's no point just for this experiment - a pot will do), and I can get it to switch a digital input (pulled up) quite happily. It's a 12V fan running off 5V, and counting the transitions that occur in a second and dividing by 2 gives me 37rps. Doing it over a 10 second period and muliplying by 3 gives me 2214RPM. - until I stick my finger in there when it goes down :wink:

What colors are your wires to the fan. Maybe i have conected it wrong, Becuase i cant gett this conection to work. I conected red to +5v, black to GND, blue to my pwm output so I can controll the speed. And yellow to the base on my bc547, trough a 400 ohms resistor.

himym:

majenko:
Just doing some experiments here.

I have a CPU fan, and I am seeing an AC waveform (in pulses) coming from the HE sensor. Passing that through a resistor into the base of a BC337, with a pot to set an offset to get a good operating point (I could calculate the right value, but there's no point just for this experiment - a pot will do), and I can get it to switch a digital input (pulled up) quite happily. It's a 12V fan running off 5V, and counting the transitions that occur in a second and dividing by 2 gives me 37rps. Doing it over a 10 second period and muliplying by 3 gives me 2214RPM. - until I stick my finger in there when it goes down :wink:

What colors are your wires to the fan. Maybe i have conected it wrong, Becuase i cant gett this conection to work. I conected red to +5v, black to GND, blue to my pwm output so I can controll the speed. And yellow to the base on my bc547, trough a 400 ohms resistor.

I don't have a PWM input to my fan. Blue on mine is the HE sensor output.

Now I have tried with 3 deifferent fans and non of them works with the led experiment. I don't know what I do wrong.

You may not be connected at the right place on the fan.
You may not have the transistor hooked up right.
Or some other part. And no way to tell from here.

I'm not sure how you got analog reading of 350 from the fan but that should have enough power to switch an NPN transistor.

GoForSmoke:
You may not be connected at the right place on the fan.
You may not have the transistor hooked up right.
Or some other part. And no way to tell from here.

I'm not sure how you got analog reading of 350 from the fan but that should have enough power to switch an NPN transistor.

It must be that pin on the fan.
The transistor is right.
So I don't know what to do.

Connect your fan and transistor up like this:

Using that circuit with a BC337 I get this nice square wave on my trusty old analog o'scope:

Nice. But i don't have an o'scope so how should i test it. Tried to write a litle code for my arduino, so if the input was high an led shoul turn on. But that didn't work.