Simple Arduino Hardware Interrupt Example

 Arduino Hardware Interrupt allows the Arduino to react to external events without having to continuously poll the state of a pin. Example of external events can be push button, sensor data etc. When external event is detected by the interrupt pins, the main program control is transferred to interrupt service routine(ISR) and code inside the ISR is executed. Once the code statements in ISR is completed, the control is transferred back to the main program. Hardware interrupt helps in efficient program execution by not tying up the program to monitor pin state. Not only that, hardware interrupt ensures that the event change is detected by Arduino and that if detected ISR is launched. For example, without hardware interrupt using software interrupt, button press or sensor signal may go undetected if the signal transition happens in microseconds. 

Consider a simplest example of how to use hardware interrupt in Arduino. A push button is connected to digital pin 2 and a LED is connected to the digital pin 13. When the push button is pressed, the hardware interrupt is triggered and ISR(Interrupt Service Routine) is launched which causes the LED to turn on. The following is the hardware wiring diagram.

Arduino hardware interrupt

 How we can use the hardware interrupt to turn on the LED when the tactile push button is pressed is illustrated by the following hardware interrupt program.


const int ledPin = 13;
const int interruptPin = 2;

void setup(){
pinMode(ledPin, OUTPUT);
attachInterrupt(digitalPinToInterrupt(interruptPin),Indicator, FALLING);
}

void loop(){
}

void Indicator(){
digitalWrite(ledPin, HIGH);
}

In the setup() function we configure the hardware interrupt using the following line of code.


attachInterrupt(digitalPinToInterrupt(interruptPin),Indicator, FALLING);

To setup the hardware interrupt we use the built in function attachInterrupt() which takes in 3 arguments- the interrupt pin with the function digitalPinToInterrupt(), the user function to be executed once the hardware interrupt happens, Indicator(), and the mode which is the type of state change which is FALLING in this case. This is the recommended way of using Arduino hardware interrupt by Arduino official website.

attachInterrupt(digitalPinToInterrupt(pin), ISR, mode)

The digitalPinInterrupt() is built in function which takes in the interrupt pin number. For Arduino Uno and Arduino Nano this is either pin 2 or 3. That is Arduino Uno has only two hardware interrupt pins.The number of hardware interrupt depends upon different Arduino board. For example Arduino Mega has more hardware interrupt pins and in board like the Arduino Due all pins are capable of generating hardware interrupt. The following table shows which pins are hardware interrupt capable in different Arduino boards.


So when the push button is pressed, the pin 2 detects the signal transistion on falling edge and the hardware interrupt is generated. Once generated, the user function called Indicator() is called. In the function Indicator() we turn on the LED on pin 13.

The mode of interrupt used in the above example is FALLING. But there are other interrupt modes as shown in the following table.

interrupt modes

 Internal Pull-up resistor

In the above hardware connection, we have used an external 1KOhm pull-up resistor in series at one end of the push button. Instead of this external pull-up resistor we can use the in-built pullup resistor. To use the internal pull-up resistor we enable the internal pull-up resistor on the hardware interrupt pin 2 and then use that pin during the hardware interrupt setup as shown by the program fragment below.

void setup(){
pinMode(ledPin, OUTPUT);
pinMode(2, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(interruptPin),Indicator, RISING);
}

This then will reduce the resistor component in the hardware wiring and simplify the connection as illustrated by the diagram below.

See other Arduino tutorials:

- High Frequency Counter with Arduino

- Arduino 8MHz Signal Generator with ISR

Post a Comment

Previous Post Next Post