Computing > Embedded Computing

Strange values while I'm measuring frequency with arduino

(1/6) > >>

Hi to all,
I'm measuring frequency of a sine to square modified voltage. I realized two separate circuit, one with a comparator LM393 and another with a transistor configured as switch. I measure the square wave frequency with an arduino and a good library I found.

The sine wave generator is a little ac generator with max 40 volt peak-to-peak, with frequency generated proportional to the speed.

Both circuits works quite well. The only problem is that both the circuit are randomly and rarely affected of a sort of "false" measurement.
E.g. I'm measuring 350 Hz and sometimes appear 1 measure to 6000Hz. Or on the contrary I'm measuring 350 Hz and sometimes appear 1 measure to 100 Hz, without the motor has changed his velocity (see attached picture). I noticed that the positive spike is the same each time, around 6000 Hz.

I asked in another forum too, but at the moment I didn't find a solution.

I think is not due to the circuit, once both have the problem. Maybe the generator has some interference or the routine I using (I tested several routines). The only solution would be to filter via software the spike, considering that is impossible for the generator to pass from a value to immediately another not close value.

Any suggestion?


instead of using your wave generator, splice off the ends of a 3.5mm headphone jack and use this website to create your sine waves:

if the amplitude is not large enough for your circuit, you can use a simple inverting op amp with the desired gain to increase it. this is very stable and constant and should allow you to debug the issue. if the same random values appear, then there is something wrong with your sine to square wave converter (which you can eliminate by changing the output of the tone generator website to square wave and connecting it to the arduino directly to measure the frequency and test your code) or in your arduino sketch where you measure the frequency.

It's possible that your circuit is responding to some occasional noise, although that wouldn't in itself explain the low frequency reading.  It's also possible (maybe more likely) that there's an issue with your Arduino program that causes occasional glitches in the computed frequency.  Could be an integer overflow, or an issue with non-atomic accesses of timer registers, or an inconveniently-timed interrupt.  It's impossible to say without knowing how exactly you've implemented the frequency measurement and preferably seeing your code.

If there is an issue with your program, it would be best to find and solve it, but you may still want to add some software filtering to smooth out any jitter in the readings, but of course any sort of filtering is always a compromise between reducing noise and slowing response time, so you'll have to figure out what's tolerable. 

Thanks for your answers.
Attached is the circuit I'm using. The code I'm using is the gammon code here

I noticed that if I take the out at the pull up resistor R29, the circuit doens't work well, with a lot of incorrect measures (high frequency measure). If I take the out at the end of the series resistor R36 the circuit works well (despite the casual spike I described). Why?

So now I have a problem and a doubt!


I would add a resistor between the junction of D7 & D9 and the junction of R39 & IC1A-3 to allow the positive feedback from R39 to add hysteresis.  I suggest a value of 1K or 2K.

I would remove the filter formed by R36 & C4.  At this point the input signal should be digital with short rise and fall times.  Increasing these times will make the circuit susceptible to electrical noise at the digital threshold levels.


[0] Message Index

[#] Next page

There was an error while thanking
Go to full version