Javatpoint Logo
Javatpoint Logo

Arduino Interrupt

The interrupts() in Arduino allows to certain essential tasks to be executed in the background, and these are enabled by default.

The disabling of the interrupts() can interfere with the Arduino functions and may impact communication. It means that some functions in Arduino will not work if the interrupts() is disabled.

Interrupts can sometimes disturb the code timings. To prevent such disturbances, interrupts can be disabled for a critical section of the code.

The syntax is:

Let's understand with an example.

Consider the below code:

Features of Interrupts()

The features of interrupts() are listed below:

  • It saves timing problems.
  • It includes reading a rotary encoder or monitors a user input.
  • It can free the processor to get some other stuff without missing the input.
  • It helps the system to react quickly and efficiently.

External Interrupts

The external interrupts are listed below:

  • attachInterrupt()
  • detachInterrupt()

attachInterrupt()

The first function to set the interrupt is: attachInterrupt( )

We are required to use the digitalPinToInterrupt(pin) to translate the real digital pin to the interrupt number (a specified number).

For example,

If we want to connect it to pin number 5, we need to use the parameter as:

It is the first parameter that is passed to the attachInterrupt().

Digital pins usable for Interrupts

Let's discuss some digital pins for various Arduino boards, which are usable for Interrupts.

  • Arduino UNO, Mini, or Nano

PIN- 2, 3

The pin number will be similar for all the boards based on 328 Microcontroller.

  • Arduino Mega ADK, Mega 2560, or Mega

PIN- 2, 3, 18, 19, 20, 21

  • Arduino Zero

It includes all digital pins except the pin number 4.

  • Arduino MKR Family boards

PIN- 0, 1, 4, 5, 6, 7, 8, 9, A1, A2

  • Arduino Due

It includes all digital pins that can be used as interrupts.

Note: The variables modified within the attached function should be declared as volatile.
Here, the delay( ) function will not work inside the attached function. It needs an interrupt to work.

The syntax is:

The above syntax is mostly recommended.

The syntax that is not recommended is:

The syntax that is suitable for Due, Uno WiFi Rev2, Arduino SAMD Boards, and 101 is:

where,

interrupt: It includes the number of the interrupt.

The data type used here is int.

pin: It includes the specified pin number of the Arduino board.

mode: It determines when the interrupt might be triggered in Arduino.

ISR: It stands for Interrupt Service Routine. It is called when the interrupts arise. It does not include any parameters.

There are four modes or constants, which are listed below:

  • LOW

It is used to trigger the interrupt when the specified pin is low.

  • FALLING

It is used to trigger the interrupt when the specified pin goes from high to low.

  • CHANGE

It is used to trigger the interrupt when the specified pin changes the value.

  • RISING

It is used to trigger the interrupt when the specified pin goes from low to high.

Code Example

Let's understand with an example.

Consider the below code:

detachInterrupt()

The detachInterrupt( ) is used to turn off the given or current interrupt.

The syntax is:

The above syntax is probably recommended.

The syntax that is not recommended is:

The syntax that is suitable for Due, Uno WiFi Rev2, Arduino SAMD Boards, and 101 is:

where,

interrupt: It includes the number of the interrupt that we want to disable.

pin: It is the pin number of the Arduino board, which is used for the interrupt to disable.


Next TopicArduino SPI





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA