Triggering an ISR with digitalWrite

Hi,

I am wondering if it is possible to trigger an ISR routine with a digitalWrite i.e., by software rather than hardware (if that makes sense?).
I have provided some code below which I am trying to get to work.

The Arduino I am using is a Mega 2560 where the interrupt pins are 2, 3, 18, 19, 20, 21.

#define InterruptPin_1  19
#define InterruptPin_2  20
#define InterruptPin_3  21

// the setup function runs once when you press reset or power the board
void setup() {
    
    Serial.begin(115200);
    Serial.println("  ");
    Serial.println(" Start  ");

    pinMode(InterruptPin_1, INPUT_PULLUP);
    pinMode(InterruptPin_2, INPUT_PULLUP);
    pinMode(InterruptPin_1, INPUT_PULLUP);

    // Write High First
    digitalWrite(InterruptPin_1, HIGH);
    digitalWrite(InterruptPin_2, HIGH);
    digitalWrite(InterruptPin_3, HIGH);

    attachInterrupt(digitalPinToInterrupt(InterruptPin_1), []() {        
        Serial.println("First Interrupt Pin Triggered");
        }, RISING);// Attach First Interrupt

    attachInterrupt(digitalPinToInterrupt(InterruptPin_2), []() {
        Serial.println("Second Interrupt Triggered");
        }, RISING);// Attach Second Interrupt

    attachInterrupt(digitalPinToInterrupt(InterruptPin_3), []() {
        Serial.println("Third Interrupt Triggered");
        }, RISING);// Attach Third  Interrupt

    Serial.println("Interrupts Attached");

    delay(5000);
    digitalWrite(InterruptPin_1, LOW);
    delay(1000);
    digitalWrite(InterruptPin_1, HIGH);
    // "First Interrupt Pin Triggered" should be printed

    delay(5000);
    digitalWrite(InterruptPin_2, LOW);
    delay(1000);
    digitalWrite(InterruptPin_2, HIGH);
    // "Second Interrupt Pin Triggered" should be printed

    delay(5000);
    digitalWrite(InterruptPin_3, LOW);
    delay(1000);
    digitalWrite(InterruptPin_3, HIGH);
    // "Third Interrupt Pin Triggered" should be printed

    Serial.println("Digital Writes Completed");
}

void loop() {
  
}

Not sure what your end goal is, however you don't need interrupts in the shown code.

    delay(5000);
    digitalWrite(InterruptPin_1, LOW);
    delay(1000);
    digitalWrite(InterruptPin_1, HIGH);
attachInterrupt(digitalPinToInterrupt(InterruptPin_1), []() {        
        Serial.println("First Interrupt Pin Triggered");
        }, RISING);// Attach First Interrupt

simply replace:

digitalWrite(InterruptPin_1, LOW);

with:

 Serial.println("First Interrupt Pin Triggered")

Switching the pull-up on and off does not set levels.
Set the pins to output to control the pin level directly.
ISRs should not use serial print.

I think we understand what you are trying to do, but why you are trying to do it does not make sense, so please explain because you may have a misconception about something fundamental.

As mentioned by @Whandall , when a pin is in INPUT mode, writing HIGH or LOW enables or disables the internal pull-up resistor on the pin. This is not guaranteed to change the voltage on the pin, unless you have a suitable external pull-down resistor (eg. ~500K).

1 Like

Hi,

@Whandall
The changes suggested got it working for me.
Also, I was unaware about the potential issue of the Serial.println in an ISR. It doesn't seem to be a problem in this case but good to know for proper projects.

@PaulRB
I am basically just trying to simulate a poke that is a IR - phototransistor pair which I do not have access to at the moment.
The Phototransistor Pin is an input pin, and the IR LED is an output pin. When the light is blocked, the voltage rises on the transistor pin and triggers an ISR.

#define InterruptPin_1  19
#define InterruptPin_2  20
#define InterruptPin_3  21

// the setup function runs once when you press reset or power the board
void setup() {
    
    Serial.begin(115200);
    Serial.println("  ");
    Serial.println(" Start  ");

    pinMode(InterruptPin_1, OUTPUT);//  Change to OUTPUT $
    pinMode(InterruptPin_2, OUTPUT);//  Change to OUTPUT $
    pinMode(InterruptPin_3, OUTPUT);//  Change to OUTPUT $

    // Write High First
    digitalWrite(InterruptPin_1, LOW); //  Change to Low $
    digitalWrite(InterruptPin_2, LOW); //  Change to Low $
    digitalWrite(InterruptPin_3, LOW); //  Change to Low $

    attachInterrupt(digitalPinToInterrupt(InterruptPin_1), []() {        
        Serial.println("First Interrupt Pin Triggered");
        }, RISING);// Attach First Interrupt

    attachInterrupt(digitalPinToInterrupt(InterruptPin_2), []() {
        Serial.println("Second Interrupt Triggered");
        }, RISING);// Attach Second Interrupt

    attachInterrupt(digitalPinToInterrupt(InterruptPin_3), []() {
        Serial.println("Third Interrupt Triggered");
        }, RISING);// Attach Third  Interrupt

    Serial.println("Interrupts Attached");

    delay(5000);
    digitalWrite(InterruptPin_1, LOW);
    delay(1000);
    digitalWrite(InterruptPin_1, HIGH);
    // "First Interrupt Pin Triggered" should be printed

    delay(5000);
    digitalWrite(InterruptPin_2, LOW);
    delay(1000);
    digitalWrite(InterruptPin_2, HIGH);
    // "Second Interrupt Pin Triggered" should be printed

    delay(5000);
    digitalWrite(InterruptPin_3, LOW);
    delay(1000);
    digitalWrite(InterruptPin_3, HIGH);
    // "Third Interrupt Pin Triggered" should be printed

    Serial.println("Digital Writes Completed");
}

// the loop function runs over and over again until power down or reset
void loop() {
  
}

Sorry, I should have been clear, I was trying to simulate something. Apologies.
Thank you for your time though!

I assume that's a typo but I can't figure out what you meant to type.

Why don't you use an output pin, and connect it to the input pin, instead of trying to use digitalWrite() on an INPUT pin?

Interrupt on a pin is not connected to input/output configuration,
but only on the pin levels.

No need to make that pin input and tie it to an output pin,
just make it output and write it directly.

Pin change interrupts will occur even if the change happen due to the processor changing an output…

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