# Reading from the ADC and using an interrupt on the same pin A0

**URL:** <https://community.particle.io/t/reading-from-the-adc-and-using-an-interrupt-on-the-same-pin-a0/16650>\
**Category:** Troubleshooting\
**Created:** [October 17, 2015, 4:35pm UTC](https://community.particle.io/t/reading-from-the-adc-and-using-an-interrupt-on-the-same-pin-a0/16650 "2015-10-17T16:35:25Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jhodapp](https://avatars.discourse-cdn.com/v4/letter/j/4af34b/32.png) [@jhodapp](https://community.particle.io/u/jhodapp)\
**Post date:** [October 17, 2015, 4:35pm UTC](https://community.particle.io/t/reading-from-the-adc-and-using-an-interrupt-on-the-same-pin-a0/16650/1 "2015-10-17T16:35:25Z")

</div>

I’m trying to do the following on a Photon:

1. Attach an interrupt to pin A0 with a rising input trigger.
2. When the A0 ISR is called, I set a volatile bool flag to true.
3. In loop(), I am checking for the bool flag from #2 to set to true.
4. When the flag from #2 is true, then I start reading from A0 with analogRead.
5. At some point I set the flag from #2 back to false.

This all works correctly, the problem then is it will not function a second time. I never see the A0 ISR get called ever again. My guess is there’s some interaction with A0 having and attached ISR and also being read via analogRead(), which from my understanding also uses interrupts. I’ve even tried detaching the ISR when the flag from #2 is true and then reattaching the ISR when the flag from #2 is set back to false. That doesn’t do anything.

Code basically looks like this (I’ve left out a lot of the other stuff):

```auto
volatile bool do_read_adc = false;

void ButtonChangeISR(void)
{
    Serial.println("ButtonChangeISR");
    do_read_adc = true;
}

void setup()
{
    pinMode(A0, INPUT);
    attachInterrupt(A0, ButtonChangeISR, RISING);
}

loop
{
    if (do_read_adc)
        val = analogRead(A0);

    // Some code that determines when to set do_read_adc to false
}

```

---

<div class="post-metadata">

**Author:** ![BulldogLowell](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/bulldoglowell/32/4305_2.png) [@BulldogLowell](https://community.particle.io/u/BulldogLowell)\
**Post date:** [October 17, 2015, 7:22pm UTC](https://community.particle.io/t/reading-from-the-adc-and-using-an-interrupt-on-the-same-pin-a0/16650/2 "2015-10-17T19:22:22Z")

</div>

```cpp
void ButtonChangeISR(void)
{
    Serial.println("ButtonChangeISR");
    do_read_adc = true;
}

```

have you tested removing the `Serial.println()` from your ISR? Serial uses interrupts.

you could try instead lighting the on-board LED on D7 inside the ISR to debug.

---

<div class="post-metadata">

**Author:** ![jhodapp](https://avatars.discourse-cdn.com/v4/letter/j/4af34b/32.png) [@jhodapp](https://community.particle.io/u/jhodapp)\
**Post date:** [October 17, 2015, 7:31pm UTC](https://community.particle.io/t/reading-from-the-adc-and-using-an-interrupt-on-the-same-pin-a0/16650/3 "2015-10-17T19:31:56Z")

</div>

Yes I have tried removing it and it doesn’t seem to make any difference. It’s my understanding that reading from the ADC also uses interrupts for sampling, so it’s my guess that this is the interaction that isn’t cooperating with the RISING trigger interrupt.

---

<div class="post-metadata">

**Author:** ![BulldogLowell](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/bulldoglowell/32/4305_2.png) [@BulldogLowell](https://community.particle.io/u/BulldogLowell)\
**Post date:** [October 17, 2015, 7:46pm UTC](https://community.particle.io/t/reading-from-the-adc-and-using-an-interrupt-on-the-same-pin-a0/16650/4 "2015-10-17T19:46:46Z")

</div>

> [@jhodapp](#):
>
> pinMode(A0, INPUT);

I can't find the thread but there may be a need to reset `pinMode()` to deal with a possible bug...

you could try adding it just before or after you reset the ISR flag to false.

---

<div class="post-metadata">

**Author:** ![jhodapp](https://avatars.discourse-cdn.com/v4/letter/j/4af34b/32.png) [@jhodapp](https://community.particle.io/u/jhodapp)\
**Post date:** [October 19, 2015, 3:02am UTC](https://community.particle.io/t/reading-from-the-adc-and-using-an-interrupt-on-the-same-pin-a0/16650/5 "2015-10-19T03:02:06Z")

</div>

If you’re able to find the thread you’re thinking about that’d be greatly appreciated.
