# Help! Analog Comparator+Interrupt Rising Edge

**URL:** <https://forum.arduino.cc/t/help-analog-comparator-interrupt-rising-edge/71567>\
**Category:** Programming\
**Created:** [September 13, 2011, 5:49pm UTC](https://forum.arduino.cc/t/help-analog-comparator-interrupt-rising-edge/71567 "2011-09-13T17:49:56Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [September 13, 2011, 5:49pm UTC](https://forum.arduino.cc/t/help-analog-comparator-interrupt-rising-edge/71567/1 "2011-09-13T17:49:56Z")

</div>

Is the code below sufficient to use analog comparator to compare AIN0 and AIN1? How can I see the pulses/levels when AIN0 \>AIN1 at the Interrupt Rising Edge. Any sample codes, please?

---

<div class="post-metadata">

**Author:** ![dc42](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/dc42/32/214539_2.png) [@dc42](https://forum.arduino.cc/u/dc42)\
**Post date:** [September 13, 2011, 5:51pm UTC](https://forum.arduino.cc/t/help-analog-comparator-interrupt-rising-edge/71567/2 "2011-09-13T17:51:20Z")

</div>

I don't see any code.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [September 13, 2011, 5:53pm UTC](https://forum.arduino.cc/t/help-analog-comparator-interrupt-rising-edge/71567/3 "2011-09-13T17:53:57Z")

</div>

Sorry, here's it:

```cpp
ACSR = 
 (0<<ACD) |  // Analog Comparator: Enabled
 (0<<ACBG) |  // Analog Comparator Bandgap Select: AIN0 is applied to the positive input
 (0<<ACO) |  // Analog Comparator Output: Off
 (1<<ACI) |  // Analog Comparator Interrupt Flag: Clear Pending Interrupt
 (1<<ACIE) |  // Analog Comparator Interrupt: Enabled
 (0<<ACIC) |  // Analog Comparator Input Capture: Disabled
 (1<<ACIS1) | (1<ACIS0);  // Analog Comparator Interrupt Mode: Comparator Interrupt on Rising Output Edge

```

---

<div class="post-metadata">

**Author:** ![dc42](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/dc42/32/214539_2.png) [@dc42](https://forum.arduino.cc/u/dc42)\
**Post date:** [September 13, 2011, 6:49pm UTC](https://forum.arduino.cc/t/help-analog-comparator-interrupt-rising-edge/71567/4 "2011-09-13T18:49:04Z")

</div>

That code looks OK to me. You should also write 0 to ADCSRB to ensure that the negative input comes from AIN1 and not the ADC multiplexer.

To hook the interrupt, see [http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1272923299](http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1272923299).

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 5, 2012, 9:03am UTC](https://forum.arduino.cc/t/help-analog-comparator-interrupt-rising-edge/71567/5 "2012-05-05T09:03:45Z")

</div>

I need a little help.  
I'm using the ACSR to trigger an interrupt on a rising edge above about 1.4v.  
My problem is that I can detect the first pulse sent to pin 6, which triggers the interrupt and sets logPin to HIGH, but for some reason it stays HIGH for ever despite the code in the loop to bring it back to LOW. Any ideas?

```cpp
int counter = 0;
int logPin = 3;

void setup()
{
  Serial.begin(9600);
  pinMode(logPin, OUTPUT);
  digitalWrite(logPin, LOW);
  ACSR =
  (0<<ACD) |  // Analog Comparator: Enabled
  (0<<ACBG) |  // Analog Comparator Bandgap Select: AIN0 is applied to the positive input
  (0<<ACO) |  // Analog Comparator Output: Off
  (1<<ACI) |  // Analog Comparator Interrupt Flag: Clear Pending Interrupt
  (1<<ACIE) |  // Analog Comparator Interrupt: Enabled
  (0<<ACIC) |  // Analog Comparator Input Capture: Disabled
  (1<<ACIS1) | (1<ACIS0);  // Analog Comparator Interrupt Mode: Comparator Interrupt on Rising Output Edge
}

void loop()
{
  if (logPin == HIGH)
  {
    counter++;
    delay(100);
    digitalWrite(logPin, LOW);
  }

  Serial.print("Counter:");
  Serial.println(counter, DEC);

}

ISR(ANALOG_COMP_vect)
{
  digitalWrite(logPin, HIGH);
}

```

---

<div class="post-metadata">

**Author:** ![dc42](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/dc42/32/214539_2.png) [@dc42](https://forum.arduino.cc/u/dc42)\
**Post date:** [May 5, 2012, 10:42am UTC](https://forum.arduino.cc/t/help-analog-comparator-interrupt-rising-edge/71567/6 "2012-05-05T10:42:44Z")

</div>

The line "if (logPin == HIGH)" should be "if (digitalRead(logPin) == HIGH)". The expression "logPin == HIGH" is equivalent to "3 == 1".

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 6, 2012, 8:22am UTC](https://forum.arduino.cc/t/help-analog-comparator-interrupt-rising-edge/71567/7 "2012-05-06T08:22:23Z")

</div>

Ok, makes sense. Thanks for the help, I'll test it asap.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 5, 2021, 7:35pm UTC](https://forum.arduino.cc/t/help-analog-comparator-interrupt-rising-edge/71567/8 "2021-05-05T19:35:59Z")

</div>

Closed on May 5, 2021, 7:35pm UTC


