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.
Since AIN0 is not available on an Arduino Mega 2560, what do I use instead?
I need to trigger an interrupt when an analog pin (e.g. A0) voltage is greater than 0.5 v
I’d very much appreciate if you could reply directly to my emai: dougr@telus.net
Leonardo, I was really glad to come across this library which at first blush appears to be straight forward to use. I’m running into an issue though with my Arduino YUN. It uses the 32U4 chip, so as far as I could determine from the libraries code the AIN1 will default to ADC0 or A5 on the Arduino YUN since the 32U4 has no AIN1. I built the test circuit you suggested in the analogComp_enableInterrupt example, namely A5 is plugged straight into the 3V terminal, and D7 (AIN0) is plugged into one end of a 10K resistor with the other end connected to ground. I upload the example touch a 5V jumper cable to the D7 jumper cable and nothing is happening. Any ideas? Thanks!
Never mind Leonardo, I figured it out. On the Atmega32U4 the interrupt function is ISR by default. Bravo, great library!
On my ATmega382p there are 8 analogue inputs. i couldn’t use ADC6 and ADC7. I think that is error. so probably change AnalogueComp.h the first contdition #define NUM_ANALOG_INPUTS 6 to #define NUM_ANALOG_INPUTS 8.
then it will work over the whole multiplexed input.
Nice library
Thanks
Not exactly an error, I didn’t consider the SMD version of the chip, that has more pins than the DIP one. If I change that setting by default, a user with a DIP chip could experience issues if he accidentally set a wrong input. I think that it’s better that anyone changes that setting to fit his own needs.
I can put a note into the documentation to inform users of this possibility.