I am doing a project that reads frequency of audio signals generated from Android device through the headset jack. The audio jack is connected to Atmega microcontroller as shown in the figure.!

I am using the analog comparator to compare between a reference voltage 2.61v and the audio signal (DC biased; AC + 2.5v). The audio signal range is 200mv p-p so after DC biasing it will be from 2.3 to 2.7. When the comparator input 1 passes 2.61 (reference volt), a timer starts to measure the frequency. But the problem is I am not getting a stable reading of frequency. For example, if the signal is 1000Hz, I read it between 4000 and 5000 Hz. The values always change. Sometimes the comparator outputs one for few microseconds even there is no audio signal (the output should be zero).!!

I think the problem is one of these:

  1. The power supply has a high ripple so I cannot read low-level signals properly.
  2. I should use a higher blocking capacitor C2 like 10uF instead of 100nF.
  3. The 200 mv is in the range of noise signals, I should amplify it before read.
  4. The voltage divider should use high values of resistors like 3MOhm as in the Hijack project below.

What do you think.

This is similar to the hijack project proposed by UMICH. Their schematic is shown below. enter image description here

  • 2
    \$\begingroup\$ Keep in mind that music has lots of different frequencies in it. You didn't say what the audio from the android device is, but for your initial tests it should be a sine wave. If it is a recording of someone playing a single note on the piano then you need to do a lot more than just use the comparator input on the MCU. \$\endgroup\$
    – user3624
    Dec 19, 2012 at 23:11
  • \$\begingroup\$ I am generating specific since wave. like 1000 Hz or 2000 Hz. \$\endgroup\$
    – yasserbn
    Dec 19, 2012 at 23:30
  • \$\begingroup\$ Why bias it with 2.5V? Check the internal reference voltages available, some controller types have other references too, which increases resolution. If you know the range of frequency, you can adopt the software to do sanity checks on the samples before you use them. It is very well possible that noise being introduced may trigger the comparator near the zero-crossings. \$\endgroup\$
    – jippie
    Dec 20, 2012 at 7:47

2 Answers 2


There a few things missing here. Maybe too many questions for a comment, but maybe enough to help you see where it's going wrong.

First of all, a 200mV peak to peak signal on 2.5V will give a range of 2.4V-2.6V, not 2.3V to 2.7V. Unless you mean it's 400mV p-p or 200mV amplitude. We'll assume you are dealing with a 400mV p-p 1kHz sine wave, since the other wouldn't work except to measure noise.

When you say a timer starts to measure the frequency, are you just seeing how long the signal is above your 2.61V reference? What are you expecting to find? A 314ns pulse from the comparator? Or are you just waiting for next rising edge of the comparator? What time resolution are you measuring at? What's your calculation?

Does the comparator have any hysteresis? If so, are you accounting for it?

Do you have any code for this project? Can you show it to us?

You should know that your method will only work for pure tone signals and it is very susceptible to noise. We don't know your ultimate application, so it's difficult to suggest alternatives.


I brought an oscilloscope to diagnose the problem and I figured out what is going on:

I am using a comparator to compare between two values. The reference which is 2.61v and the audio signal which is after adding the DC (2.3 - 2.7 v). When the audio signal passes the 2.61v the timer starts counting until the audio signal passes the 2.61 again. Then, the value of the timer will be the T of the signal.

That looks great. Not really!!

There is a noise from my 5v power supply around 50mv p-p (very high frequency). This makes the reference voltage 2.61v oscillate. At the moment the audio signal is 2.61, the comparator output may oscillate several times due to the noise of the reference voltage. That's why I am reading values of a frequency higher than what it should be.

Note that this only happens with very low voltage signals like our case. In other words, the noise is 50mv p-p and my signal is 400mv p-p so the range of the edge detection is 1/8 T out the signal and that's a long time. If I amplify the signal or suppose the p-p volt is 5v, then the range of the edge detection is 1/100 out of the signal. In such a case the problem can be barely happened especially with high frequencies.

I am wondering how that research paper (hijack project) measure the frequency in this way. One thing is that he is using 3 Mega Ohm resistors for voltage divider. Does it eliminate the noise?

I think the solution is either

1) use internal voltage reference (it should be well-regulated) but it cannot work with my tiny signal.

2) or use the ADC to read the signal and compare it with a reference value.

  • \$\begingroup\$ Filter your power supply and also filter your reference voltage. However, this type of solution may always be a bit finicky. \$\endgroup\$ Dec 24, 2012 at 4:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.