Engineers Garage

  • Electronic Projects & Tutorials
    • Electronic Projects
      • Arduino Projects
      • AVR
      • Raspberry pi
      • ESP8266
      • BeagleBone
      • 8051 Microcontroller
      • ARM
      • PIC Microcontroller
      • STM32
    • Tutorials
      • Audio Electronics
      • Battery Management
      • Brainwave
      • Electric Vehicles
      • EMI/EMC/RFI
      • Hardware Filters
      • IoT tutorials
      • Power Tutorials
      • Python
      • Sensors
      • USB
      • VHDL
    • Circuit Design
    • Project Videos
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • Electronic Product News
    • Business News
    • Company/Start-up News
    • DIY Reviews
    • Guest Post
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • DigiKey Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Learn
    • eBooks/Tech Tips
    • Design Guides
    • Learning Center
    • Tech Toolboxes
    • Webinars & Digital Events
  • Resources
    • Digital Issues
    • EE Training Days
    • LEAP Awards
    • Podcasts
    • Webinars / Digital Events
    • White Papers
    • Engineering Diversity & Inclusion
    • DesignFast
  • Guest Post Guidelines
  • Advertise
  • Subscribe

How to use inbuilt analog comparator of AVR microcontroller- (Part 29/46)

By Ashutosh Bhatt July 5, 2010

Analog comparator is a device which compares two input voltages and generates output accordingly. The article on IR sensor explains the use of comparator in sensor designing. Comparators form an integral part of circuit designing in majority of the applications.  AVR microcontrollers have in-built analog comparator. Using the in-built analog comparator of AVR, the controller can be used to compare the signal and process the signal as well. This reduces the external comparator components on our circuits. In this article proximity sensor is designed using in-built analog comparator of ATmega16.

 


 

The analog comparator needs two inputs positive and negative. The positive input is given on AIN0 (PB2) pin of controller. In ATmega16 nine pins are available to connect negative input of comparator. This means microcontroller can compare maximum of nine analog signals with one positive input voltage. Although, signals are not compared simultaneously but the time difference between two consecutive comparisons is of the order of microseconds which is quite low to identify. The negative input of comparator is applied on pin AIN1 (PB3). The negative input can also be applied on ADC channels (PA0-PA7) but the in-built ADC system must be turned off. The output of comparison affects the ACO bit of ACSR of analog comparator system. When the voltage of positive pin (AIN0) is greater than negative input voltage, the ACO bit of register ACSR is set by hardware, otherwise ACO remains low.
 
Analog comparator registers:
There are three registers which take part in configuration of the analog comparator.
1. SFIOR (Special Function IO Register) :
 

Bit Configuration of SFIOR in Analog comparator of AVR Microcontroller

Fig. 2: Bit Configuration of SFIOR in Analog comparator of AVR Microcontroller

ACME (Analog Comparator Multiplexer Enable) – When this bit is zero the negative analog input is applied only on AIN1 pin. When this bit is set and ADC system is disabled, the negative analog input can be given at ADC channel pins (ADC0-ADC7) which can be selected by ADMUX register.
 
2. ACSR (Analog Comparator Control and Status Register):
 
Bit Configuration of ACSR in Analog comparator of AVR Microcontroller
Fig. 3: Bit Configuration of ACSR in Analog comparator of AVR Microcontroller
 
ACD (Analog Comparator Disable) – This bit disables the analog comparator when set to one.
ACBG (Analog Comparator Bandgap Select) – When this bit is set to one, a fixed internal bandgap voltage VBG(1.15 V < VBG < 1.4 V) is selected as positive input of comparator. When this bit is set to zero, voltage at pin AIN0 will be considered as positive input voltage.
ACO (Analog Comparator Output) – When voltage at pin AIN0 is higher than the negative input pin, this bit is set by hardware. The analog comparator needs one or two clock cycle to synchronize with ACO bit.
The following bits (ACI, ACIE, ACIC and ASIC) are used for further application (for eg. ADC triggering, etc.) which is beyond the scope of this article. (You can skip for the time being) The brief explanation for these bits is given below. 
ACI (Analog Comparator Interrupt Flag) – This bit is set by hardware when a comparator output event triggers the interrupt mode which is defined ACIS1 and ACIS0 bits.
ACIE (Analog Comparator Interrupt Enable) – This bit is set in order to activate analog comparator interrupt.
ACIC (Analog Comparator Input Capture Enable) – This bit is used to enable the input capture function in Timer/Counter1. To enable the input capture along with ACIC bit the TICIE1 bit in TIMSK register is set to one. When this bit is set to zero, the analog comparator is disconnected with Timer system.
ASIC [1:0] (Analog Comparator Interrupt Mode Select) – This bit is used to select interrupt modes.
 
3. ADMUX (ADC Multiplexer Selection Register):
 

Bit Configuration of ADMUX in Analog comparator of AVR Microcontroller

Fig. 4: Bit Configuration of ADMUX in Analog comparator of AVR Microcontroller

 
When ADC is turned off (ADEN bit in ADCSRA register is made zero) and the ACME bit in SFIOR register is set, the ADC channel (ADC0-ADC7) can be selected as negative input pin by configuring the MUX [2:0] bits. The following table shows the bits setting to select ADC channel as negative input pins: 
 

Bit Setting to select ADC channel as negative input pins in AVR's Anaog Comparator

Fig. 5: Bit Setting to select ADC channel as negative input pins in AVR’s Anaog Comparator

 NOTE: x= don’t care.
 
Objective: To design three proximity sensors using IR pair and the in-built analog comparator of Atmega16.
 
Circuit description:
As shown in circuit diagram the positive voltage is applied to AIN0 (PB2) pin by using a variable resistor. The analog output of the three IR Tx and Rx pair (as shown in below diagram), are connected at ADC0 (PA0), ADC1 (PA1) and AIN1 (PB3) pins and compared output of the sensors are taken on pins PD0, PD1 and PD2 respectively.
Circuit Diagram of Proximity sensor designed using in-built analog comparator of ATmega16.
Fig. 6: Circuit Diagram of Proximity sensor designed using in-built analog comparator of ATmega16.
 
Programming steps:
1. Clear the ACO bit.
2. Turn off the ADC (clear ADEN bit in ADCSRA register).3. Send zero to ACME bit in SFIOR register if AIN1 pin is used as negative input of comparator.
4. Send one to ACME bit in SFIOR register if ADC channel is used as negative input of comparator.
5. Set the MUX [2:0] in ADMUX register to select the channel as shown in above table.
6. Give a  time delay of 1or 2 microseconds to synchronize analog comparator with ACO bit.
7. Detect the ACO bit output and send the output on corresponding output pin (PD0, PD1, or PD2 for this experiment).

 

Project Source Code

###

//Program to use inbuilt analog comparator of AVR microcontroller
#include<avr/io.h>
#include<util/delay.h>


int main()
{
DDRD=0x07; // set PD0, PD1 and PD2 as output pin
ACSR=0x00; //to set ACO bit as zero.
ADCSRA&=~(1<<ADEN); // to disable ADC
while(1)
{
SFIOR|=(1<<ACME); // bit is set to select the ADC channel
ADMUX=0x00; // ADC0 channel is selected
_delay_us(1); // delay for synchronization
if(bit_is_set(ACSR,ACO)) // if bit is set to 1
PORTD|=(1<<PD0); // send one to PD0 bit
else // if bit is set to zero
PORTD&=~(1<<PD0);

SFIOR|=(1<<ACME);
ADMUX=0x01; // ADC1 channel is selected
_delay_us(1);
if(bit_is_set(ACSR,ACO))
PORTD|=(1<<PD1);
else
PORTD&=~(1<<PD1);

SFIOR&=~(1<<ACME); // to select AIN1 pin as negative input
_delay_us(1);
if(bit_is_set(ACSR,ACO))
PORTD|=(1<<PD2);
else
PORTD&=~(1<<PD2);
}
}
 

###

 


Circuit Diagrams

Circuit-Diagram-of-How-to-use-inbuilt-analog-comparator-of-AVR-microcontroller

Project Components

  • ATmega16
  • LCD

Project Video


Filed Under: AVR
Tagged With: analog comparator, atmega16, avr, microcontroller
 

Next Article

← Previous Article
Next Article →

Questions related to this article?
👉Ask and discuss on Electro-Tech-Online.com and EDAboard.com forums.



Tell Us What You Think!! Cancel reply

You must be logged in to post a comment.

EE TECH TOOLBOX

“ee
Tech Toolbox: 5G Technology
This Tech Toolbox covers the basics of 5G technology plus a story about how engineers designed and built a prototype DSL router mostly from old cellphone parts. Download this first 5G/wired/wireless communications Tech Toolbox to learn more!

EE Learning Center

EE Learning Center
“engineers
EXPAND YOUR KNOWLEDGE AND STAY CONNECTED
Get the latest info on technologies, tools and strategies for EE professionals.

HAVE A QUESTION?

Have a technical question about an article or other engineering questions? Check out our engineering forums EDABoard.com and Electro-Tech-Online.com where you can get those questions asked and answered by your peers!


RSS EDABOARD.com Discussions

  • Inconsistent Charge Termination Voltage with battery charger
  • 21V keeps getting shorted to my UART line.
  • Voltage mode pushpull is a nonsense SMPS?
  • Voltage mode push pull with extra DC blocking capacitor
  • NXP library issue in awr

RSS Electro-Tech-Online.com Discussions

  • Is AI making embedded software developers more productive?
  • Why can't I breadboard this oscillator?
  • using a RTC in SF basic
  • Parts required for a personal project
  • Cataract Lens Options?

Featured – RPi Python Programming (27 Part)

  • RPi Python Programming 21: The SIM900A AT commands
  • RPi Python Programming 22: Calls & SMS using a SIM900A GSM-GPRS modem
  • RPi Python Programming 23: Interfacing a NEO-6MV2 GPS module with Raspberry Pi
  • RPi Python Programming 24: I2C explained
  • RPi Python Programming 25 – Synchronous serial communication in Raspberry Pi using I2C protocol
  • RPi Python Programming 26 – Interfacing ADXL345 accelerometer sensor with Raspberry Pi

Recent Articles

  • GigaDevice launches GD32C231 MCU series with 48MHz Cortex-M23 core and 64KB Flash
  • Advanced Energy releases 425 W CF-rated medical power supply in 3.5 x 6 x 1.5-inch format”
  • LEM combines shunt and Hall effect sensing in 2000 A current measurement unit
  • What is AWS IoT Core and when should you use it?
  • AC-DC power supply extends voltage range to 800 V DC

EE ENGINEERING TRAINING DAYS

engineering

Submit a Guest Post

submit a guest post
Engineers Garage
  • Analog IC TIps
  • Connector Tips
  • Battery Power Tips
  • DesignFast
  • EDABoard Forums
  • EE World Online
  • Electro-Tech-Online Forums
  • EV Engineering
  • Microcontroller Tips
  • Power Electronic Tips
  • Sensor Tips
  • Test and Measurement Tips
  • 5G Technology World
  • Subscribe to our newsletter
  • About Us
  • Contact Us
  • Advertise

Copyright © 2025 WTWH Media LLC. All Rights Reserved. The material on this site may not be reproduced, distributed, transmitted, cached or otherwise used, except with the prior written permission of WTWH Media
Privacy Policy

Search Engineers Garage

  • Electronic Projects & Tutorials
    • Electronic Projects
      • Arduino Projects
      • AVR
      • Raspberry pi
      • ESP8266
      • BeagleBone
      • 8051 Microcontroller
      • ARM
      • PIC Microcontroller
      • STM32
    • Tutorials
      • Audio Electronics
      • Battery Management
      • Brainwave
      • Electric Vehicles
      • EMI/EMC/RFI
      • Hardware Filters
      • IoT tutorials
      • Power Tutorials
      • Python
      • Sensors
      • USB
      • VHDL
    • Circuit Design
    • Project Videos
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • Electronic Product News
    • Business News
    • Company/Start-up News
    • DIY Reviews
    • Guest Post
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • DigiKey Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Learn
    • eBooks/Tech Tips
    • Design Guides
    • Learning Center
    • Tech Toolboxes
    • Webinars & Digital Events
  • Resources
    • Digital Issues
    • EE Training Days
    • LEAP Awards
    • Podcasts
    • Webinars / Digital Events
    • White Papers
    • Engineering Diversity & Inclusion
    • DesignFast
  • Guest Post Guidelines
  • Advertise
  • Subscribe