How to use inbuilt analog comparator of AVR microcontroller

Summary of How to use inbuilt analog comparator of AVR microcontroller


This article details designing a proximity sensor using the ATmega16 microcontroller's in-built analog comparator. It explains how the device compares input voltages on AIN0 and AIN1 (or ADC channels) to generate digital output signals, reducing external component needs. The text outlines the configuration of three specific registers: SFIOR, ACSR, and their respective control bits like ACME, ACD, and ACBG for setting up the comparison logic and enabling features like bandgap voltage selection or interrupt triggers.

Parts used in the Proximity Sensor:

  • ATmega16 Microcontroller
  • Analog Comparator (In-built)
  • Ain0 Pin (PB2)
  • Ain1 Pin (PB3)
  • SFIOR Register
  • ACSR Register
  • ADC Channels (PA0-PA7)
  • Bandgap Voltage Source

A comparator looks at two voltages and tells you which one is higher. Many AVR chips have one built in, so you can check a sensor level, a battery voltage or a zero crossing without using the ADC. This page explains the registers of the ATmega16 comparator and gives a short interrupt driven program that lights an LED when the voltage on AIN0 is above the voltage on AIN1.

IN SHORT
  • The positive input is AIN0 (PB2). The negative input is AIN1 (PB3), or, if you set ACME and switch the ADC off, one of the ADC0 to ADC7 pins.
  • The result is the ACO bit of ACSR: 1 when AIN0 is higher than the negative input.
  • Setting ACIE and the global interrupt flag runs the ANA_COMP interrupt each time the output changes.
  • The program compiles for the ATmega16 to 152 bytes; we did not run it on hardware.
2
analog inputs
1 bit
result (ACO)
152 B
program size
0
ADC needed
Chain: AIN0 pin, analog comparator in ACSR, ACO flag and interrupt, LED on PC0
From two voltages to one decision

The registers

Everything is in ACSR, the Analog Comparator Control and Status Register, plus one bit (ACME) in SFIOR.

Bit Name What it does
7 ACD Comparator disable. Write 1 to turn it off and save power. Clear it before ACIE is set.
6 ACBG Replaces AIN0 with the internal bandgap reference (see the datasheet for its value).
5 ACO The comparator output, read only.
4 ACI Interrupt flag. Set on the selected output edge; cleared when the interrupt runs.
3 ACIE Interrupt enable.
2 ACIC Connects the output to the Timer1 input capture unit.
1, 0 ACIS1, ACIS0 00 interrupt on output toggle, 10 on falling edge, 11 on rising edge (01 is reserved).

The negative input is normally AIN1. To compare against an ADC pin instead, switch the ADC off (clear ADEN), set ACME in SFIOR and choose the pin with MUX2:0 in ADMUX. The comparator then uses that pin as its negative input, which gives you eight selectable inputs from one comparator.

A simple test circuit

Pin Connection
PB2 (AIN0) Wiper of a potentiometer between 5 V and ground (the signal you test)
PB3 (AIN1) Second potentiometer or a resistor divider that sets the threshold
PC0 LED with a resistor to ground

Turn the signal potentiometer above the threshold and the LED lights; turn it below and the LED goes out. Because both inputs are analog, no pull-ups or pin direction bits are needed on PB2 and PB3.

The program

/* ATmega16 built-in analog comparator. Positive input AIN0 (PB2), negative input AIN1 (PB3) or, with ACME set and the ADC
   off, one of the ADC0..ADC7 pins. Output flag ACO in ACSR. An interrupt on any output toggle lights the LED on PC0.
   Build: avr-gcc -Os -mmcu=atmega16 -DF_CPU=8000000UL -o comp.elf analog_comparator.c  (compiled only, not run) */
#ifndef F_CPU
#define F_CPU 8000000UL
#endif
#include <avr/io.h>
#include <avr/interrupt.h>

#ifndef USE_ADC_PIN
#define USE_ADC_PIN 0
#endif
/* 1: negative input comes from ADC pin number ADC_PIN instead of AIN1 */
#define ADC_PIN     2

ISR(ANA_COMP_vect)             /* output of the comparator changed */
{
    if (ACSR & (1 << ACO)) PORTC |= (1 << PC0);    /* AIN0 above the negative input: LED on */
    else                   PORTC &= (uint8_t)~(1 << PC0);
}

int main(void)
{
    DDRC |= (1 << PC0);
#if USE_ADC_PIN
    ADCSRA &= (uint8_t)~(1 << ADEN);               /* the ADC must be off to share its multiplexer */
    SFIOR |= (1 << ACME);
    ADMUX = ADC_PIN & 0x07;
#endif
    ACSR = (1 << ACIE);                            /* comparator on (ACD = 0), interrupt on output toggle (ACIS = 00) */
    sei();
    for (;;) { }
}

Compile it with the command in the first comment. To use an ADC pin as the negative input, add -DUSE_ADC_PIN=1; the pin number comes from ADC_PIN. The comparator output has no hysteresis, so a slowly changing or noisy input can toggle the output many times near the threshold. Add a small amount of positive feedback with a resistor, or filter the input, if that matters.

If it does not work

What you see What to check
LED never changes Check that ACD is 0 and that the inputs are not floating.
Interrupt fires continuously The input is noisy near the threshold: filter it or add hysteresis.
ADC pin option does nothing ADEN must be 0 and ACME set; another part of the code may be turning the ADC on.
Flag stays set ACI clears when the ISR runs; in a polling loop clear it by writing 1 to it.

Quick answers

Can I use it instead of the ADC?
For a yes or no decision against a threshold, yes. For a number, use the ADC.
Does it work in sleep modes?
It can, but it draws a little current; the datasheet lists which sleep modes keep it running.
Is the interrupt vector name the same on other AVRs?
Usually ANA_COMP_vect, but check the header for your chip.

Downloads and references

Share this project
X
LinkedIn
Reddit
WhatsApp

Quick Solutions to Questions related to Proximity Sensor:

  • How does the ATmega16 compare analog signals?
    The controller uses its in-built analog comparator to compare signals on the positive AIN0 pin against negative inputs on AIN1 or ADC channels.
  • Can the negative input be connected to ADC pins?
    Yes, but the in-built ADC system must be turned off before applying the negative input to ADC channel pins.
  • What happens when the positive pin voltage is higher than the negative input?
    The ACO bit of the ACSR register is set by hardware to indicate the positive voltage condition.
  • Which register enables connecting the negative input to ADC channels?
    The SFIOR register controls this via the ACME bit when set to one.
  • How can a fixed internal voltage be used as the positive input?
    Setting the ACBG bit in the ACSR register selects the internal bandgap voltage instead of the AIN0 pin voltage.
  • Does the microcontroller compare multiple signals simultaneously?
    No, signals are not compared simultaneously, though the time difference between comparisons is very low.
  • What is the function of the ACIE bit?
    The ACIE bit is set to activate the analog comparator interrupt functionality.

About The Author

Author avatar

Ibrar Ayyub

I am an experienced technical writer holding a Master's degree in computer science from BZU Multan, Pakistan University. With a background spanning various industries, particularly in home automation and engineering, I have honed my skills in crafting clear and concise content. Proficient in leveraging infographics and diagrams, I strive to simplify complex concepts for readers. My strength lies in thorough research and presenting information in a structured and logical format.

Follow Us:
LinkedinTwitter

Stay in the loop

New AVR projects, tutorials and schematics, every week.

Subscribe Browse projects

Scroll to Top