Go Down

Topic: i want to read frequency realtime (Read 17177 times) previous topic - next topic

copachino

still this is to much speed for a car wheel, so this code is more than perfect for my aplicattion, whta ar you using to inject the signal to arduino??

nickgammon

A Rigol function generator. Lets me dial up any frequency between about 1 Hz and 5 MHz, sine waves, square waves, etc.
Please post technical questions on the forum, not by personal message. Thanks!

More info: http://www.gammon.com.au/electronics

copachino

pretty good jeeje e im using a ne555 but frequency its not always 1khz..... but this week i will try with the sensor from and see how it behaves.... probably not so accurate due an engine never have constants speed but reasonable accurate...... thanks for all the help

greets

retrolefty


My pleasure. At 1 KHz I got a pretty accurate reading:

Code: [Select]
Took: 16015 counts. Frequency: 999.06 Hz.
Counted up to: 520
Took: 16015 counts. Frequency: 999.06 Hz.
Counted up to: 522
Took: 16017 counts. Frequency: 998.94 Hz.
Counted up to: 521
Took: 16017 counts. Frequency: 998.94 Hz.
Counted up to: 521
Took: 16016 counts. Frequency: 999.00 Hz.
Counted up to: 518
Took: 16016 counts. Frequency: 999.00 Hz.


That's only 0.1% out.


Nick, you are an arduino software god in my eyes.  ;)

nickgammon

Aww, thanks Lefty! But it was your description that got me started. I suppose that is how scopes measure frequency too. You have on the screen a signal and it computes the period and thus the frequency, as long as you have at least one sample.

Anyway, I am hoping to improve the accuracy. After watching Dave Jones' video about surplus Rubidium Frequency Standards available on eBay cheap, I've ordered one. That is supposed to deliver a super-accurate 10 MHz signal. By using that as the source for Timer 1 rather than the internal clock you should be able to get a very accurate count (within the resolution that 10 MHz gives you).
Please post technical questions on the forum, not by personal message. Thanks!

More info: http://www.gammon.com.au/electronics

retrolefty

#20
Feb 11, 2012, 01:51 am Last Edit: Feb 11, 2012, 02:02 am by retrolefty Reason: 1
Quote
Anyway, I am hoping to improve the accuracy. After watching Dave Jones' video about surplus Rubidium Frequency Standards available on eBay cheap, I've ordered one. That is supposed to deliver a super-accurate 10 MHz signal. By using that as the source for Timer 1 rather than the internal clock you should be able to get a very accurate count (within the resolution that 10 MHz gives you).


I saw that video also and was very impressed at the prices of those used surplus clock sources and considered buying one. However I long ago bought a 10mhz OCXO oscillator on e-bay for like $15 and to be honest haven't used it much other then as a standard to check other instruments against. So while the Rubidium standard carries a much higher coolness factor, a good OCXO is much more cost effective, much smaller, and good enough for anything outside of a laboratory environment.

Typical offerings:

http://www.ebay.com/itm/ISOTEMP-OCXO131-191-10Mhz-OCXO-OSCILLATOR-SQUARE-WAVE-/180808600834?pt=BI_Cellular_Optical_Television_Test_Equipment&hash=item2a19084d02

And it's spec sheet showing accuracy: frequency stablity is +/- 25 ppb (yes billion), so about 1000X better then a typical microcontroller crystal.

http://www.isotemp.com/wp-content/uploads/2011/03/OCXO-131.pdf


Lefty

nickgammon

Please post technical questions on the forum, not by personal message. Thanks!

More info: http://www.gammon.com.au/electronics

Techone

@Nick Gammon

I did my best to program my frequency counting code. It look basic, but it work. retrolefty just discuss about a high frequency feed into a test signal, and count those fast pulse. I juts sudgest that in the tread : measure a 60 Hz precisely. I did a code fot it and it work. I was using pulseIn(). Your code is better.

Heh, I am doing my best...

nickgammon

That's fine: "go with what works", I say. And if I write something, and it works for the situation at hand, I am happy. The simpler the better. :)

In this case however, where 10 KHz was quoted, we had to try something else.
Please post technical questions on the forum, not by personal message. Thanks!

More info: http://www.gammon.com.au/electronics

Techone

Yeah...I agree. My code work because I use the 1 second baseline and count pulse inside that 1 second. And my 60 / 50 hz monitor work by using pulseIn() for the High and also for the LOW.  But your code is better...

About the hardware side, just a squarewave from 0-5 at X frequency going into a digital pin 2 ( interrupt 0 ) ? Right ?  or it is a +/- wave using a zero crosing, just my up-dated schematic in the tread : http://arduino.cc/forum/index.php/topic,87699.0.html, in the last posting.

I will use your code and study it so I can understand it, if you don't mind.

Anyway... good work !  ;)

Techone

@Nick Gammon

Just a question :

Code: [Select]


TCCR1A = 0;
  TCCR1B = 0;
  // Timer 1 - interrupt on overflow
  TIMSK1 = _BV (TOIE1);   // enable Timer1 Interrupt
  // zero it
  TCNT1 = 0;     
  // start Timer 1
  TCCR1B =  _BV (CS20);  //  no prescaling



This section of your code. It is came from the ATMEGA328P datasheet ? I wonder where can I get this info.

nickgammon

Yes it's all in the datasheet. It's the primary font of knowledge about the chip.

Be prepared to read it many times (like I did) before it all sinks in. ;)
Please post technical questions on the forum, not by personal message. Thanks!

More info: http://www.gammon.com.au/electronics

Techone

Thank Nick.

I will read it many time... :smiley-roll:

copachino

you guys are all amazings...... techone your code works really well but i couldnt use cos i need faster updates in frequency than 1 second but if a reduce the sample time it just gives errors....... nick you code its very accurate, but i still have something i would like to think of..... its that for signal comming out from a sensor signal its not that perfect square, so how i was thinkig that a low-pass filter with bessel eq. could work to clean that, but to be honests i know bessel but i dont know how to applied it to a program to filter the signal....


still your code its amazing, probably this would become a playground link to your code......

but i will follow your lesson and read many many time atmega 328 datasheet jeje

Techone

@copachino

Check the link in my post in this tread. To measure a 60 Hz sine wave into a square wave, I have to build an op-amp, comfigure into a comparator. The LM339 did not work. I was force to use a 741 op-amp, using a dual voltage ( two set of 4.5 battery pack ) , a diode and a transistor using a 5 V to make sure the output of the transistor is TTL compatible.

Here the link again and check the last post for the schematic. It will give you an idea. 
http://arduino.cc/forum/index.php/topic,87699.0.html


Go Up