Go Down

### Topic: Count how many pulses per second (Read 16526 times)previous topic - next topic

#### himym

##### Jun 13, 2012, 12:02 pm
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?

#### PaulS

#1
##### Jun 13, 2012, 12:57 pm
Quote
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.

Quote
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.

#### majenko

#2
##### Jun 13, 2012, 01:00 pm
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.

#### GoForSmoke

#3
##### Jun 13, 2012, 01:26 pm
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.

2) http://gammon.com.au/serial
3) http://gammon.com.au/interrupts

#### himym

#4
##### Jun 13, 2012, 01:34 pm

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.

#### majenko

#5
##### Jun 13, 2012, 01:45 pm

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.

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.

#### himym

#6
##### Jun 13, 2012, 02:03 pm

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.

#### himym

#7
##### Jun 13, 2012, 02:06 pm

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.

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.

#### majenko

#8
##### Jun 13, 2012, 02:11 pm
Quote
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. http://arduino.cc/playground/Code/InfraredReceivers

#### himym

#9
##### Jun 13, 2012, 02:14 pm
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.

#### GoForSmoke

#10
##### Jun 13, 2012, 02:39 pm

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.
2) http://gammon.com.au/serial
3) http://gammon.com.au/interrupts

#### majenko

#11
##### Jun 13, 2012, 02:46 pm
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

#### himym

#12
##### Jun 13, 2012, 03:13 pm

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

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.

#### majenko

#13
##### Jun 13, 2012, 03:21 pm

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

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.

#### himym

#14
##### Jun 13, 2012, 03:47 pm
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.

Go Up