February 21, 2024, 11:31:59 AM

Pin mode 3 : frequency measuring

Started by johnny, June 18, 2016, 02:55:20 PM

Previous topic - Next topic



I have a project to measure the frequency of a sensor, and reading the manual, it states that there is a nice mode in de Duinomite to do this : setpin option 3, so I did a quick test program :

10 setpin 19,3
20 print pin(19)
30 goto 10

Simple, but :

error line 10 : invalid configuration value

Anyone tested this, anyone did get this very simple way of measuring frequency input / period input working ? Same error with other ports, do I miss something ?



Ok, read the manual....
On page 19 is tells us that this is only supported on some ports : 5,6,7 (Arduino port A4, A5 and D10)
Changed to port 5 and inject a square on A4, no more error's but the print tells me still 0...


Hello Johnny,

Nice to see you back on the forum :-)

Change your program as follows:

10 SETPIN 5,3
20 PAUSE 2000
40 GOTO 20

SETPIN will reset the value and you should give it the change to count at least during 1 sec.



Hallo Kees,

Yes indeed, this works fine,
I did some testing with a HP3325 with a accuracy of 5E-5 and the results are on my board :

Input (Hz)        printed
100               100
1000              1000
10.000            10.000
100.000           100.007
250.000           249.334
300.000           296.614
350.000           343.799
500.000           485.353
1.000.000         929.334

So the manual tells us upto 200.000Hz, it still counts on higher but not that precise any more..

A other thing I see and that may be useful, if you put eg. 0.4Hz on the input you get 3 times a 0 reading and 2 times a 1 reading.

Just quick question, is there a possibility to have a .1Hz reading, as the frequency's I'm monitoring are below 100Hz, it would improve my tool. Using multiple readings and interpolating makes it to slow.

Thanks once more for the info,



Hello Johnny,

Nice that it works now. Personally I never did an application on the frequency measurement, however I edited some of the Basic functionality for the counting functionality. In fact the frequency measurement is just the counter which is stored and reset every second. This time base is derived from the timer clock as far as I know. I have to check in the sources if it would be possible to change this time base in an easy way to e.g. 10 seconds. I will respond to this later.

Concerning the accuracy at higher frequencies: the counter (and frequency) inputs use the hardware interrupt function of the CPU. This means every time an edge of 0 to 1 occurs the Basic program is shortly interrupted and a counter is incremented. This is very short and the processor is capable of doing this up to 1 MHz without any problem, however ... other interrupt routines are also called a number of times per second for the millisecond timer, the video refresh and the USB communication. Eventually also for keyboard, sound, SPI, RS232 and I2S communication. For an application I switched off all low level interrupts and in that case I see all the edges up to 1 MHz. With the other interrupts enabled it simply misses a count every now and then and that is why all measurements above 100 kHz are lower than the actual value. But as I understood you are more interested in lower frequencies anyway.



Some addition to my earlier answer:

I looked into the sources and indeed the frequency measurement is like the counting function with a store and reset every 1000 msec, based on the mSec timer from the TIMER and SETTICK commands. This is hardcoded and cannot simply changed by some POKE statement.

Maybe you can use the counting function and simulate the store and reset in Basic.


10 SETPIN 5,5
20 SETTICK 10000,100
30 PAUSE 1000
40 GOTO 30
100 PRINT PIN(5)/10
110 SETPIN 5,5:REM this will reset the counter



Hallo Kees,

Yep, confirmed, I got my frequency up to 1/10 of a Hz.

Many thanks,