Trying to incorporate analogcomp.h on Arduino

I am using the analogComp.h library from Leonardo Miliani. From the website:

GitHub - leomil72/analogComp: analogComp is a library to manage the integrated analog comparator of some Atmel MCUs

The sample code I was trying to run from the above github repository is: `/*
This is a simple sketch to demonstrate the use of analogComp, a
library to manage the analog comparator included in a wide 
variety of Atmel microcontrollers

This sketch enables an interrupt to be raised when on the analog input 0
(pin AIN0) there is a voltage greater than the voltage on the
analog input 1 (pin AIN1). To test the sketch, build the following
circuit:
- connect pin AIN1 to pin 3V3
- connect pin AIN0 to GND using a pull-down resistor (10Kohm or greater)
- connect pin AIN0 to pin 5V to activate the interrupt


More info on the analog comparator can be found reading the datasheet.

Please read the README file to know how to use this library.

Written by Leonardo Miliani <leonardo AT leonardomiliani DOT com>
	
This code and the analogComp library are free software; you can redistribute 
and/or modify them under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 3.0 of the License,
or (at your option) any later version.

This work is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 

*/

//include the library
#include "analogComp.h"

//global variables
const byte LED13 = 13; //set the output LED
volatile boolean enableLed = false; //used to check if the interrupt has raised

//let's set up the hardware
void setup() {
    pinMode(LED13, OUTPUT); //LED pin as output
    analogComparator.setOn(AIN0, AIN1); //we instruct the lib to use voltages on the pins
    analogComparator.enableInterrupt(changeStatus, CHANGE); //we set the interrupt and when it has to be raised
}

//main loop
void loop() {
    if (enableLed) { //let's check if the analog comparator has raised the interrupt
        //yes, so we do a little blink of the LED
        digitalWrite(LED13, HIGH);
        delay(200);
        digitalWrite(LED13, LOW);
        enableLed = false;
    }
}

//interrupt to be raised by the analog comparator
void changeStatus() {
    enableLed = true; //let's inform the main loop that the condition has been reached by the analog comparator
}`

Firstly, I was under the impression that the AIN1 will be connected to 3.3V and for the AIN0 we will have two wires: one connecting one end of resistor to GND on breadboard and another will be the one which we can switch between GND and 5V on Arduino board. However, this did not work. then I went to the pin Assignment for ATMEGA 328P where I saw that the pins AIN0 and AIN1 correspond to PD6 and PD7 which are the digital pins not Analog which has confused me.

Now, what I did was I connected the AIN1 which was connected to 3.3V to D7 and connect AIN0's +ve connection to A1. The built-in LED lit up but doesn't seem to go LOW. I wanted to know if what I did here was correct or is there some other way to implement the Analog Comparator library.

What are you trying to do, and what went wrong?

I recommend the short analog comparator tutorial by Nick Gammon.

Hello @jremington ,

Thank you for your reply. I was able to find the way to implement the Analog Comparator. The thing is, I was under the impression that since it is a Analog Comparator it would uses Analog Signals or an Analog Signal and a Power Source for comparison. However, As per the website you gave and looking back into pin assignment of Atmega 328P it indeed are Pins: D6 and D7. So, when the value at D6 is at higher value compared to D7 the LED would light up caused by EnableInterrupt() function.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.