Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Arduino interrupts


May 15, 2021 Arduino


Table of contents


Interrupt stops Arduino's current work so that some other work can be done.

Suppose you're sitting at home talking to someone. /b10> Suddenly the telephone rang. /b11>You stop chatting, pick up the phone and talk to the caller. /b12> When you're done talking on the phone, you go back and talk to the person before the phone rings.

Similarly, you can think of the main program as chatting with someone, and the ringtone stops you from chatting. /b10> An interrupt program is the process of talking on a phone. /b11> When the call is over, you go back to the main program you chatted with. T his example accurately explains how interrupts enable the processor to perform operations.

The main program runs in the circuit and performs some functions. /b10> However, when an interrupt occurs, the main program stops when another program executes. /b11> When the program ends, the processor returns to the main program again.

Arduino interrupts

Important characteristics

Here are some important features about interrupts:

  • Interruptions can come from a variety of sources. /b10> In this case, we are using a hardware interrupt triggered by a state change on the digital pin.

  • Most Arduino designs have two hardware interrupts (called "interrupt0" and "interrupt1") that are hard-connected to digital I/O pins 2 and 3, respectively.

  • The Arduino Mega has six hardware interrupts, including additional interrupts on pins 21, 20, 19, and 18 ("interrupt2" to "interrupt5").

  • You can define a program using a special function called a Interrupt Service Program, commonly known as an ISR.

  • You can define the program and specify the conditions for the rising, falling, or both. /b10> Under these specific conditions, interrupts are processed.

  • The function can be executed automatically each time an event occurs on the input pin.

The type of interruption

There are two types of interrupts:

  • Hardware interrupts - They occur in response to external events, such as external interrupt pins becoming high or low.

  • Software outages - They occur in response to instructions sent in the software. /b10> The only type of interrupt supported by the Arduino language is the atachInterrupt() function.

Use interrupts in Arduino

Interrupts are useful in Arduino programs because they help resolve timing issues. /b10> A good application for interrupts is to read the rotary encoder or observe user input. I n general, ISRs should be as short and fast as possible. /b12> If your sketch uses more than one ISR, you can only run one at a time. /b13> Other interrupts will be executed after the current completion, depending on their priority.

Typically, global variables are used to pass data between the ISR and the main program. T o ensure that variables shared between the ISR and the main program are updated correctly, declare them volatile.

In Arduino, there are mainly clock interrupts and external interrupts, which in this article refer to external interrupts. E xternal interrupts in Arduino are usually triggered by a change in the level of the Pin port (digital Pin, not analog port). The Arduino version of each model has several Pins that can be used to register interrupts, as follows:

Development board Can be used to register a broken Pin port
Uno, Nano, Mini, other 328-based 2, 3
Uno WiFi Rev.2 All numeric mouths
Mega, Mega2560, MegaADK 2, 3, 18, 19, 20, 21
Micro, Leonardo, other 32u4-based 0, 1, 2, 3, 7
Zero All numeric mouths except port 4
MKR Family boards 0, 1, 4, 5, 6, 7, 8, 9, A1, A2
Due All numeric mouths
101 All digital port (only 2, 5, 7, 8, 10, 11, 12, 13 digital port can CHANGE interrupt, interrupt type is described below)

Registration interruptions are attachInterrupt() function, which is prototyped as:

void attachInterrupt(uint8_t interruptNum, void (*userFunc)(void), int mode);
  1. The first argument is the interrupt number, and each Pin port on Arduino that can register the interrupt is assigned an interrupt number, which needs to be passed in instead of the Pin slogan. B ut the interrupt number assignments on different Arduino boards are not exactly the same. T he Pin slogans and interrupt numbers for each board correspond to the following:
    Development board Interruption number 0 Interruption Number 1 Interruption Number 2 Interruption Number 3 Interruption Number 4 Interruption number 5
    Uno, Ethernet PIN 2 PIN 3
    Mega2560 PIN 2 PIN 3 PIN 21 PIN 20 PIN 19 PIN 18
    32u4-based development boards such as Leonardo, Micro PIN 3 PIN 2 PIN 0 PIN 1 PIN 7
    As you can see from the table above, the same Pin port may have different interrupt numbers on different boards, which is bound to affect the portability of the program. F ortunately, Arduino also provides another function, digitalPinToInterrupt(int). A s you can see from the name, this function can enter the Pin slogan and output the corresponding interrupt number. I t is important to note that the Pin slogan you entered needs to be in the support list above. T herefore, Arduino officially recommends that we use it
    attachInterrupt(digitalPinToInterrupt(pin), ISR, mode); 
    This way to register the interrupt number.
  2. The second argument is a function pointer to the interrupt service routine (ISR), which can be written directly in C/C? W hen triggered, the function is called. The function must have no arguments and no return values.
  3. The third argument is the interrupt trigger condition, which consists of several optional values:
    LOW is triggered when the Pin port where the interrupt is located is low
    CHANGE is triggered when the Pin level at which the interrupt is located changes
    RISE is triggered when the Pin port where the interrupt is located changes from low to high (rising edge).
    FALLING is triggered when the Pin port where the interrupt is located changes from high to low (falling edge).
    For due, Zero and MKR1000 development boards, there is also a HIGH that indicates that the Pin port where the interrupt is located is triggered when it is high

Example

int pin = 2; //define interrupt pin to 2
volatile int state = LOW; // To make sure variables shared between an ISR
//the main program are updated correctly,declare them as volatile.

void setup() {
   pinMode(13, OUTPUT); //set pin 13 as output
   attachInterrupt(digitalPinToInterrupt(pin), blink, CHANGE);
   //interrupt at pin 2 blink ISR when pin to change the value
} 
void loop() { 
   digitalWrite(13, state); //pin 13 equal the state value
} 

void blink() { 
   //ISR function
   state = !state; //toggle the state when the interrupt occurs
}

attachInterrupt statement syntax

attachInterrupt(digitalPinToInterrupt(pin),ISR,mode);//recommended for arduino board
attachInterrupt(pin, ISR, mode) ; //recommended Arduino Due, Zero only
//argument pin: the pin number
//argument ISR: the ISR to call when the interrupt occurs; 
   //this function must take no parameters and return nothing. 
   //This function is sometimes referred to as an interrupt service routine.
//argument mode: defines when the interrupt should be triggered.

There are issues to be aware of when using interrupts in Arduino

  1. Because interrupts interrupt normal code, the ISR should be executed as quickly as possible.
  2. Global variables modified in the ISR are modified with volatile modifiers to prevent compiler optimization
  3. Other functions implemented with interrupts, such as millis() delay(), cannot be used in isR. Delays can be achieved using delayMicroseconds(), which are not implemented with interrupts.