Triggering Analogue Interrupts

How do you set an interrupt on A0 to trigger?

Consider the code below:

#include "application.h"

SYSTEM_MODE(AUTOMATIC);

//Settings
int led1 = D0;
int led2 = D7;
int photoresistor = A0;
int power = A5;
int analogvalue;

void setup() {
    pinMode(led1,OUTPUT);
    pinMode(led2,OUTPUT);
    pinMode(photoresistor,INPUT);
    pinMode(power,OUTPUT);
    digitalWrite(power,HIGH);
    attachInterrupt(photoresistor, IncrementPulseCount, RISING);
}

void loop() {
    analogvalue = analogRead(photoresistor);
    if(analogvalue > 200) {
        digitalWrite(led1,HIGH);
    }
    else {
        digitalWrite(led1,LOW);
    }
}

void IncrementPulseCount() {
    digitalWrite(led2,HIGH);
}

I am using the same hardware setup as shown in this example:

https://docs.particle.io/guide/getting-started/examples/photon/#read-your-photoresistor-function-and-variable

D0 reacts to flashing a light on the photoresistor, but the one in the interrupt function (in this case D7) remains off.

What do you want to achieve with this?

Using analogRead() somehow counteracts the purpose of attachInterrupt().
attachInterrupt() would trigger on a change from digital LOW to digital HIGH (or vice versa or both), while analogRead() by definition is not meant for dealing with digital states.

I don’t really need an analogue read. The reason it is there is for verifying that the photo resistor is working. All I need is a digital signal when light is flashed on the photo resistor and an interrupt to be triggered by that signal. I rewired the circuit by connecting the the photo resistor between 3V3 and D1, pulling it down with a resistor to GND and changing the code to the one below but it did not work.

#include "application.h"

SYSTEM_MODE(AUTOMATIC);

//Settings
int led1 = D0;
int led2 = D7;
int photoresistor = D1;
int DigitalValue;

void setup() {
    pinMode(led1,OUTPUT);
    pinMode(led2,OUTPUT);
    pinMode(photoresistor,INPUT);
    attachInterrupt(photoresistor, IncrementPulseCount, RISING);
}

void loop() {
    DigitalValue = digitalRead(photoresistor);
    if(DigitalValue == 1) {
        digitalWrite(led1,HIGH);
    }
    else {
        digitalWrite(led1,LOW);
    }
}

void IncrementPulseCount() {
    digitalWrite(led2,HIGH);
}

@osprey, a photoresistor is not a digital device since it is really a resistor, so your circuit with the “pull-down” is really a voltage divider. For the photoresistor to properly function, the voltage at the digital pin will need to go from below the “LOW” voltage threshold to above that threshold to be seen as a rising edge. This suggests a “digital” swing in light level, which is not realistic. Here is a great forum post with someone having the same issue on an Arduino:

:smile:

3 Likes

I was hoping a simple voltage divider could act as a digital input. Given that it won’t work, or at least not as simply as I imagined, I will settle for a dedicated digital device like this one. Thanks for the link though! :smile:

2 Likes

@osprey, good choice of devices! It will also give you more control on the trigger light level. :smiley:

1 Like