Interrupts are very important processes that occur in many circuits. Many circuits have a process that monitors what is going on and if the correct parameter(s) are met then an interrupt will kick in – it interrupts what was happening! We can split interrupts into hardware and software.
These occur when an external source changes the state (LOW or HIGH).
These occur when the software instructs an interruption.
We can split interrupts on the Arduino into two, external interrupts and pin change interrupts.
External interrupts are interpreted by the hardware and occur at a very fast speed. We can set these interrupts to trigger on RISING, FALLING or LOW levels.
The amount of pins available to use for interrupts depends on which Arduino you use, see table 1.
|Arduino model||External interrupt pins|
|UNO, NANO||2, 3|
|MEGA||2, 3, 18, 19, 20, 21|
You can increase the amount of pin interrupts by using pin change interrupts. For Arduinos that use ATmega 168/328, up to 20 signal pins can be used. They can use RISING or FALLING levels.
To use interrupts you need to understand some terminology and concepts:
Interrupt Service Routine (ISR)
An ISR or Interrupt handler is an event that has a small set of instructions in it. When an external event occurs, the processor first executes the code that is present in the ISR and returns back to the state where it was when it left the normal execution.
The syntax for ISR in Arduino is attachInterrupt(digitalPinToInterrupt(pin), ISR, mode);
ISR is a function that is called when an external interrupt is done.
mode is the type of transition that you want to trigger on.
RISING is when the interrupt will be triggered by a transition from HIGH to LOW.
FALLING is when the interrupt will be triggered by a transition from LOW to HIGH.
CHANGE is when the interrupt will be triggered from HIGH to LOW or LOW to HIGH.
Two conditions for interrupts are that the ISR should be as short as possible and the DELAY() function doesn’t work inside ISR.