0
\$\begingroup\$

I'm experimenting with the comparator inside a ATmega328 to see if it can be used for zero detection of a DC 2.5V superimposed AC signal monitored on one of the ADC inputs.

I use the code as shown below. The code switches between the ATmega ADC/comparator used as ADC and used as comparator. You can't use them both with fixed register settings at the same time in case you use the ADCmux for the comparator input. I use a potentiometer connected to A1 as a source to change the ADC A1 voltage.

First I read the ADC value of the selected input A1 and next I convert the Atmega ADC/comparator to a comparator to see if it generates an interrupt. The interrupts are counted by cnt++. I use the internal vref 1.1V as reference voltage for the comparator +input. The ADC Vref is VCC with is in my case 4.8V. The potentiometer is connected between VCC GND and A1

The comparator trigger level is expected to be between 213 and 256 as a result of the spread in Vref which is between 1.0 and 1.2 volt with center at 1.1V.

At 1.1V the expected trigger level is 234.

The printout of my code shows that around 233 I see the interrupts coming in.

Around this value 233 many interrupts are generated, I assume due to noise at the inputs of the comparator. What surprises me is that the interrupts continue to appear in the window 233 +/- 5 and even more.

Does anyone have experience with the use of this comparator? Is it normal to have a noise window of +/- 5 in which it generates spurious interrupts coming from the comparator? If that is the case this comparator is not suitable be used as an AC zero detection which I at this moment do based on ADC readings.

While I was writing this article I suddenly realized that the noise at VCC of cause appears on the input of A1 as a result of the use of the potentiometer. After connecting a capacitor between A1 and ground, the noise disappeared and the comparator was switching between ADC value +/- 1.

I decided to post this article anyway, maybe someone had similar problems. It looks to me that this comparator can be use as AC zero detection of an AC signal. Anyway let me know your experience.

enter image description here

\$\endgroup\$

2 Answers 2

2
\$\begingroup\$

You can "fake" hysteresis in a comparator by lowering the threshold when it's crossed from lower to higher and raising it when it crosses from higher to lower. That should prevent a lot of the spurious interrupts.

\$\endgroup\$
1
  • \$\begingroup\$ Thanks I will keep it in mind \$\endgroup\$
    – wogoos
    Feb 14, 2022 at 6:07
1
\$\begingroup\$

Depending on how fast your signal is changing, you can probably handle this in software in exactly the same way that you would debounce a mechanical switch. Make a note of the exact time that each interrupt occurs. If it has been only a short time since the last one, then exit the ISR without doing anything. A simple RC low pass filter on the input of the ADC might help as well.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ I added an RC filter and added a line of code in the ISR to disable the Int of the Comparator. It works now \$\endgroup\$
    – wogoos
    Feb 14, 2022 at 6:58

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.