Pages: [1] 2 3 ... 5   Go Down
Author Topic: Count how many pulses per second  (Read 5334 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Jr. Member
**
Karma: 0
Posts: 50
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?   
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 548
Posts: 46017
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

UK
Offline Offline
Faraday Member
**
Karma: 92
Posts: 3969
Where is your SSCCE?!?!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Why not visit my eBay shop? http://stores.ebay.co.uk/Majenko-Technologies
Replacement for the Arduino IDE: UECIDE - Proper serial terminal, graphing facilities, plugins, overhauled internals.
Java isn't bad in itself, but it has enabled morons to write programs.

Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 58
Posts: 3996
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.


Logged

Examples can be found in your IDE.

Offline Offline
Jr. Member
**
Karma: 0
Posts: 50
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

UK
Offline Offline
Faraday Member
**
Karma: 92
Posts: 3969
Where is your SSCCE?!?!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Why not visit my eBay shop? http://stores.ebay.co.uk/Majenko-Technologies
Replacement for the Arduino IDE: UECIDE - Proper serial terminal, graphing facilities, plugins, overhauled internals.
Java isn't bad in itself, but it has enabled morons to write programs.

Offline Offline
Jr. Member
**
Karma: 0
Posts: 50
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Jr. Member
**
Karma: 0
Posts: 50
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

UK
Offline Offline
Faraday Member
**
Karma: 92
Posts: 3969
Where is your SSCCE?!?!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Why not visit my eBay shop? http://stores.ebay.co.uk/Majenko-Technologies
Replacement for the Arduino IDE: UECIDE - Proper serial terminal, graphing facilities, plugins, overhauled internals.
Java isn't bad in itself, but it has enabled morons to write programs.

Offline Offline
Jr. Member
**
Karma: 0
Posts: 50
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 58
Posts: 3996
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Examples can be found in your IDE.

UK
Offline Offline
Faraday Member
**
Karma: 92
Posts: 3969
Where is your SSCCE?!?!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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 smiley-wink
Logged

Why not visit my eBay shop? http://stores.ebay.co.uk/Majenko-Technologies
Replacement for the Arduino IDE: UECIDE - Proper serial terminal, graphing facilities, plugins, overhauled internals.
Java isn't bad in itself, but it has enabled morons to write programs.

Offline Offline
Jr. Member
**
Karma: 0
Posts: 50
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 smiley-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.
Logged

UK
Offline Offline
Faraday Member
**
Karma: 92
Posts: 3969
Where is your SSCCE?!?!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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 smiley-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.
Logged

Why not visit my eBay shop? http://stores.ebay.co.uk/Majenko-Technologies
Replacement for the Arduino IDE: UECIDE - Proper serial terminal, graphing facilities, plugins, overhauled internals.
Java isn't bad in itself, but it has enabled morons to write programs.

Offline Offline
Jr. Member
**
Karma: 0
Posts: 50
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pages: [1] 2 3 ... 5   Go Up
Jump to: