-->
Page 1 of 2

How to measure frequency ( 30-35 Khz ) ?

PostPosted: Mon Dec 26, 2016 6:48 am
by aphawk
Hi,

I'm trying to use one Storm detection module, but for the calibration procedure I must measure frequency about 31250 Hz with a good precision ( 1% is ok ).

Any ideas to make this with EspBasic ?

Thanks !

Paulo

Re: How to measure frequency ( 30-35 Khz ) ?

PostPosted: Tue Dec 27, 2016 1:40 pm
by PhilTilson
Assuming you have a reasonably 'digital' input waveform, I would think you could simply start a timer, count, say, 10000 input pulses, stop the timer and then perform simple arithmetic to get the frequency from that. Using, say, IO pin 5 as the input, something like:

Code: Select alldim start
dim stop
dim count
dim freq

start=millis()
do
   do
   loop until io(pi,5)=0
   count=count+1
   do
   loop until io(pi,5)=1
loop until count=10000

stop = millis()
freq=10000/(stop-start)

This should give the frequency in KHz with at least 1% accuracy - assuming the ESP clock is accurate!

Phil

Re: How to measure frequency ( 30-35 Khz ) ?

PostPosted: Tue Dec 27, 2016 7:34 pm
by aphawk
Phill,

The problem is assure that I have one exact millisecond.

The command you have posted catch the milissecond, but not at its start. I can get the 1.0000 millicond, but I can get the 1.7456 millisecond too !

I need the exact time of 1 millisecond to make the calculation without error.

Paulo

Re: How to measure frequency ( 30-35 Khz ) ?

PostPosted: Tue Dec 27, 2016 11:34 pm
by heckler
aphawk,

Without analyzing your exact needs in detail I would suggest you might want to consider offloading this task of accurately measuring frequency for calibration to a separate small 6-8 pin microcontroller.

My experience with this awesome espbasic (and it is awesome) is that it MAY not be ideal for some types of functions where you might need accurate frequency or timing.

For example I don't know what the temperature stability of the inbuilt clock is on various espmodules and the overhead of the OS may make it difficult to calibrate a routine to measure frequency.

Do you have an Oscilliscope?? that is a very useful tool when dealing with frequency of a given signal.

dwight