Arduino · 3 November 2012 6

analogComp, a library to manage the analog comparator

Today I’ll present you analogComp, a library to manage the analog comparator integrated inside the Atmel microcontrollers. An analog comparator is essentially an operational amplifier (op-amp) used as a voltage comparator which sets its output to High when the voltage on the non-inverting input is higher that the voltage present on its inverting input. The inputs can be two external pins at which connect the voltage that have to be compared.

THe library, that supports a good variety of chips, let the user manage a single comparation and also to set a routine to be called with an interrupt every time a particolar condition happens.

Methods and usage

As usual, download the package at the end of the article and unpack it, then copy the folder /analogComp that you’ve found inside the package into the folder /libraries present in your sketchs’ folder.

Befero to use the library, you have to include it with

#include "analogComp.h"

Now you can set what has to be connected to the inverting (AIN-) and non-inverting (AIN+) inputs of the analog comparator. Usually, the AIN+ is connected to external pin AIN0 and the AIN- is connected to external pin AIN1. The AIN+ can be connected to the internal voltage reference (usually 1V1), while the AIN- can be connected to any of the analog input pins of the microcontroller. (see “Supported Microcontrollers” for specific limitations)

To choose the analog comparator inputs use the method setOn():

analogComparator.setOn([AIN+, AIN-]);

For the AIN+ you can choose between the following:

  • AIN0: set the AIN0 pin as input
  • INTERNAL_REFERENCE: set the internal voltage reference as input

For the AIN- you can choose between the following:

  • AIN1: set the AIN1 pin as input
  • A0..Ax: set a specific analog pin as input (max. pin number depends on the MCU)

AIN+ and AIN- are optionals, if not set, AIN0 and AIN1 will be used.

You can enable an interrupt routine to be executed when an event occurs:

analogComparator.enableInterrupt(myFunction[, event]);

myFunction if the name of the function to be called when the event occurs. event can be:

  • CHANGE: when the comparation changes between AIN+>AIN- and AIN+
  • RISING: when the voltage on AIN+ becomes greater than the voltage on AIN-
  • FALLING: when AIN+ becomes smaller than AIN-

event is optional, if not set CHANGE will be choosen.

You can disable the interrupt by calling this method:

analogComparator.disableInterrupt();

You can wait for a comparation to occur:

analogComparator.waitComp([timeout]);

timeout is optional and rapresents the number of milliseconds to wait before to return (default is 5000). This method will return false (0) if voltage on AIN- will remain greater than the voltage on  AIN+ for the whole interval specified by timeout; it will return true (1) if AIN+ will become greater than AIN- during the interval.

If the analog comparator won’t be set up before to call the waitComp method, by default the library will use AIN0 and AIN1 pins.

To switch off the analog comparator and disable the interrupt, call the method setOff():

analogComparator.setOff();

Supported microcontrollers

Actually the library works with a wide variety of Atmel microcontrollers

and Arduino boards:

  • Attiny2313/4313*
  • Attiny24/44/84
  • Attiny25/45/85
  • Atmega344/644/1284
  • Atmega8
  • Atmega48/88/168/328 (Arduino UNO)
  • Atmega640/1280/1281/2560/2561 (Arduino MEGA)
  • Atmega32U4* (Arduino Leonardo)
*Specific limitations
  • Attiny2313/4313: due to the fact that these MCUs don’t have an integrated ADC, only AIN1 is allowed for AIN-
  • Atmega32U4: the Atmega32U4 AIN- can only be connected to an analog input pin because it has no AIN1 pin.
analogComp
analogComp
analogComp_1.2.2.zip
Version: 1.2.2
162.8 KiB
4312 Downloads
Details...