How to Use Arduino Interrupts
Introduction
Interrupt is extremely useful when dealing with all kinds of processes that needs attention at unexpected time or when expecting a certain event or signal at indefinite and unknown time.
For example you send a chat message to your crush. You are very excited but also nervous. So every second you are checking your messenger if she already replied. This is tiring and it takes most of your time and energy. You will not be able to do other important tasks and responsibilities. In microcontroller jargon, it is called polling.
What you can do is to turn the notification and go on with your normal life, do your chores and routines. When someone send you a chat message, you will hear a notification sound. That is the time for you to check your mobile phone.
The notification sound serves as interrupt call and checking of mobile phone is the Interrupt Service Routine. Efficient, right?
So how does the interrupt really works?
When an event occurs which triggers the interrupt, the current process will be put on hold and the ISR or Interrupt Service Routine will be executed. All other interrupt will be disabled.
When the ISR finish executing, the previous process before the interrupt will continue and all other interrupts will be re-enabled.
It is highly recommended to make the ISR as short as possible because an ISR that takes lets say 5 seconds to executes means a 5 seconds of uncertainty, WHY? Because all other processes are disabled during ISR execution, right? As a rule of thumb, just set variable flag or save a sensor value inside the ISR and do the other remaining processes inside the main loop.
There are two kinds of interrupt: Internal interrupt – or software interrupt is triggered by internal peripherals like timers. External interrupt – or hardware interrupt is triggered by an external device.
External Interrupt pins:
How to set interrupt:
attachInterrupt(INTERRUPT_NUM, ISR_FUNCTION, TRIGGER_MODE)
INTERRUPT_NUM – is the interrupt number associated to interrupt pin.
ISR_FUNCTION – is the ISR function to be executed when the interrupt is detected
TRIGGER_MODE – is the type of detection of interrupt. It could be a RISING, FALLING, CHANGE, or LOW.
RISING – triggers when the pin goes from low to high FALLING – triggers when the pin goes from high to low CHANGE – triggers whenever the pin changes value LOW – triggers when the pin is low.
Keep in mind the following rules when using interrupt:
- Keep it short. As I already discussed earlier.
- Do not use delay functions as per the first rule said, keep interrupt short while delay function prolong the execution of the function.
- Do not use serial. Serial use and trigger interrupts.
- Make global variable volatile. This is as per C standard, variable should be declared volatile when there is chance that the variable will be use outside the normal execution flow (outside the main loop, which is the ISR function). Volatile disables the variable optimization.
Video Demonstration
Source Code
1#define INTERRUPT_PIN 2
2#define INDICATOR_PIN 3
3
4volatile int LED_state = 1;
5
6void setup() {
7 // put your setup code here, to run once:
8 pinMode(INTERRUPT_PIN, INPUT);
9 pinMode(INDICATOR_PIN, OUTPUT);
10 attachInterrupt(digitalPinToInterrupt(INTERRUPT_PIN), Button_ISR, RISING);
11}
12
13void loop() {
14 // put your main code here, to run repeatedly:
15}
16
17void Button_ISR () {
18 LED_state = !LED_state;
19 digitalWrite(INDICATOR_PIN, LED_state);
20}
Call To Action
If you found this tutorial useful, kindly share this to your friends. Thank you and have a good day.
Posts in this series
- How to Get Started with ATTiny85 in Arduino IDE
- Tutorial: How to use MFRC522 RFID module using Arduino
- SOS Flasher Using Millis Function with Enable Switch
- Tutorial: How to use DS3231 RTC in Arduino
- Tutorial: How to use 0.96 OLED - a small and cute display
- Tutorial: Getting Started with the NRF24L01 | How to use | Arduino
- Tutorial: How to use SIM800L GSM Module for Controlling Anything | Arduino
- Tutorial: How to use Keypad | Text Entry Mode | Arduino
- Tutorial: How to use 4x4 Keypad | Arduino
- Project Idea: Arduino Voltmeter
- Project Idea: Door Lock Security | Arduino
- Multitasking with Arduino | Relay Timer Controller | using millis
- Tutorial Understanding Blink Without Delay | How to millis
- Arduino Simple LCD Menu
- How to use SIM800L GSM Module using Arduino | Make or Answer Voice Calls
- Tutorial: How to Use Arduino Uno as HID | Part 2: Arduino Mouse Emulation
- Tutorial: How to Use Arduino Uno as HID | Part 1: Arduino Keyboard Emulation
- Tutorial: How to use SIM800L DTMF to Control Anything | Arduino
- Tutorial: Arduino EEPROM
- How to use SIM800L GSM Module | Arduino | Send and Receive SMS
- 16x2 LCD Menu for Arduino
- Tutorial: Arduino GPIO | How to use Arduino Pins
- MIT App Inventor for Arduino
- RC Car using L298N, HC-06, and Arduino Uno
- How to Use LCD Keypad Shield for Arduino
- Project: Automatic Alcohol Dispenser
- TUTORIAL: How to use HC-SR04 Ultrasonic Sensor with Arduino
- Source Code: Astronomia Meme and Funeral Dance | melodies the Arduino way
- How to Get Started with L293D Motor Driver Shield with Arduino
- How to Get Started with L298N Motor Driver module using Arduino
- Part 2: Wav Music Player with Lyrics Using Arduino and SD Card
- Interfacing Infrared to Arduino Uno
- Part 1: Wav Music Player Using Arduino Uno and SD Card
- How to Interface Stepper Motor to Arduino Uno
- How To Play MP3 Files on Arduino from SD Card
- What is Arduino Software Serial
- How to Interface SD card to Arduino (without SD card shield)?
- Playing Melodies Using Arduino
- 8 Degrees Of Freedom (DOF) Robot Using Arduino Uno
- How to Interface PS2 Controller to Arduino Uno
- Part 3: DF Player Mini Tinkering with Arduino Nano and LCD
- How to Interface HC-06 to Arduino
- How to make a Remote Control RC car using Arduino and HC-06 bluetooth module
- Part 2: DF Player Mini Tinkering with Arduino Nano
- Part 1: DF Player Mini - a mini cheap mp3 player
No comments yet!