Can I safely connect a digital output pin to the external interrupt pin on the same board?

Hi everyone! I'm using Arduino Uno and I'm new to electronic hardwares.

What I am trying to do is to:

  1. generate a low frequency square wave (pulses) from a digital output pin
  2. connect the output pin to the external interrupt pin and count the number of pulses
  3. Switch on a LED every 10 pulses detected

The reason I do this is because right now I don't have a frequency generator at hands and still want to check if the LED light-up based on number of pulses detected works. Later on I will use a real source of pulses to connect to Arduino, instead of using the Arduino itself like in Step 1 described above.

I know that connect an output pin to an input pin on the same Arduino should be fine. But I am not sure when I configure a pin as external interrupt would be safe as well. Is external interrupt pin equal to an input pin? Is there a risk to damage my board?

Here are my codes:

/*
  -generate pulses in pin 10 (output)
  -count the pulses through external interrupt pin 2 (?input?);
  -light the led in pin 13 every ten pulses (output);
*/

int pulseGnPin = 10;
int pulseDetPin = 2;
int ledPin = 13;

long count = 0;  //number of pulses

int period = 50;  //half period of generated pulses
long lastTime = 0;  
bool pulseGnState = false;  //used for generating pulses without delay()

void setup() {
  pinMode(pulseGnPin, OUTPUT);
  pinMode(ledPin, OUTPUT);
  attachInterrupt(pulseDetPin, pulseCount, RISING);
  lastTime = millis();
}

void loop() {
  pulseGn();
  if (count%10==0) {
    digitalWrite(ledPin, HIGH);
    delay(10);
    count = 0; //clear pulse count number before it gets too large.
  }
}

void pulseGn() {
  if (millis() - lastTime > period) {
    lastTime = millis();
    pulseGnState = !pulseGnState;
    digitalWrite(pulseGnPin, pulseGnState);
  }
}

void pulseCount() {
  count++;
}

Thanks in advance!

I think that you should be OK but as a matter of interest have you got a second Arduino ?

I can't think of any reason that would not work, but just to be extra safe you could connect a 4.7K or 10K resistor between the pins, then even if both pins were set to output it would not draw enough current to damage anything.

The variable count needs to be declared volatile because it is used in both the interrupt service routine and the main code, and you should temporarily disable interrupts when accessing that variable in the main code because (most) arduino boards can only access one byte at a time, making it possible for an interrupt to occur between bytes of the long. A common technique is to disable interrupts, copy count to another variable, then re-enable interrupts, that minimizes the amount of time interrupts are disabled when count will be used multiple times.

int pulseDetPin = 2;
. . . 
attachInterrupt(pulseDetPin, pulseCount, RISING);

That doesn't work.
But also explicitly define pulseDetPin as an input pin.

make sure the ISR pin is set as an INPUT

attachInterrupt(digitalPinToInterrupt(pin), ISR, mode);

The digitalPinToInterrupt picks the right interrupt (0 or 1) based on the pin you using (2 or 3).

Just a repeat, I thought this was automatically done.

a7

Yes that's the default. but just in case.

Belt and suspenders.

I cane across no examples where the setting of the interrupt pin as INPUT or INPUT_PULLUP was not explicitly part of the setup.

a7

It is just the the default setting of the data direction registers [AVR name DDRx] is 0 so all pins are input by default. It is nothing specific about an external interrupt pin.

Noted. I have more trouble remember something like what you point out than I do just making things explicit. And when reading code (not always for AVR) it helps.

I go so far as to

 int myVariable = 0;

globals, for example, both to assure myself and perhaps communicate that I am relying on it.

The downside of course is the verbosity can be obfuscatory. Cough.

a7

Actually I'm the same: being explicit is a way of expressing your design intent to others. Relying on defaults doesn't do that so well. Especially, as has been said, if you want to port your code to another platform.

1 Like

With an ESP32 board, all the GPIO pins can be configured to function as interrupt request inputs!

Should be ‘unsigned long’