Arduino Interrupts Tutorial - The Robotics Back-End
Arduino Interrupts Tutorial - The Robotics Back-End
Menu
Table of Contents
1. What is an Interrupt pin?
1.1. A real life analogy
1.1.1. Example 1
1.1.2. Example 2
1.2. Interrupts on Arduino
https://roboticsbackend.com/arduino-interrupts/ 1/19
3/22/2021 Arduino Interrupts Tutorial - The Robotics Back-End
Example 1
https://roboticsbackend.com/arduino-interrupts/ 2/19
3/22/2021 Arduino Interrupts Tutorial - The Robotics Back-End
You are learning how to use Arduino to build your own projects?
Let’s add more details to this analogy: the email you’re about to
receive contains a special offer to get a discount on a given
website – and this offer is available only for 2 minutes. If you use
the “polling” technique, there is a chance that you miss some
https://roboticsbackend.com/arduino-interrupts/ 3/19
3/22/2021 Arduino Interrupts Tutorial - The Robotics Back-End
data (in this example, you’ll miss the discount). With interrupts,
you can be sure you won’t miss it.
Example 2
In both scenarios, you stop your current action. That’s why it’s
called an interruption. You have to stop what you’re doing to
handle the interruption, and only after you’re done with it,
you can resume your action.
Interrupts on Arduino
For example, if you are waiting for a user to press a push button,
you can either monitor the button at a high frequency, or use
interrupts. With interrupts, you’re sure that you won’t miss the
trigger.
https://roboticsbackend.com/arduino-interrupts/ 4/19
3/22/2021 Arduino Interrupts Tutorial - The Robotics Back-End
Note that for the real life analogies above, interrupts make much
more sense than the polling technique. However I want to point
that sometimes, polling can be a better choice. At human scale,
interrupts make much more sense. At a micro-controller scale,
where the frequency of execution is much higher, sometimes it
becomes complicated than that and the choice is not always
obvious. We’ll discuss more about it later in this post.
Here are the pins you can use for interrupts on the main Arduino
boards:
https://roboticsbackend.com/arduino-interrupts/ 5/19
3/22/2021 Arduino Interrupts Tutorial - The Robotics Back-End
Schematics
https://roboticsbackend.com/arduino-interrupts/ 6/19
3/22/2021 Arduino Interrupts Tutorial - The Robotics Back-End
Note that we are using the pin 3 for the button. As previously
stated, on Arduino Uno you can only use pin 2 and 3 for
interrupts. Pay attention when you have to choose a pin for an
interrupt. If the pin is not compatible with interrupts your
program won’t work (but still compile), and you’ll spend quite
some time scratching your head while trying to find a solution.
Types of interrupts
https://roboticsbackend.com/arduino-interrupts/ 7/19
3/22/2021 Arduino Interrupts Tutorial - The Robotics Back-End
what you want to monitor. For that you’ll have to modify the 3rd
parameter of the attachInterrupt() function:
Practically speaking, you could monitor when the user presses the
buttons, or when he/she releases the button, or both.
https://roboticsbackend.com/arduino-interrupts/ 8/19
3/22/2021 Arduino Interrupts Tutorial - The Robotics Back-End
Here, the main advantage you get is that there is no more polling
for the button in the loop() function. As soon as the button is
pressed, blinkLed() will be called, and you don’t need to worry
about it in the loop().
As you can guess, you should make the interrupt function as fast
as possible, because it stops the main execution of your program.
You can’t do heavy computation. Also, only one interrupt can be
handled at a time.
Let’s say you want to move a motor, and this action is triggered
by an interrupt. In this case, you could have a variable named
“shouldMoveMotor” that you set to “true” in the interrupt
function.
1. #define BUTTON_PIN 3
2.
3. volatile bool shouldMoveMotor = false;
4.
5. void setup() {
6. pinMode(BUTTON_PIN, INPUT);
7. attachInterrupt(digitalPinToInterrupt(BUTTON_PIN),
triggerMoveMotor, RISING);
8. }
9.
10. void loop() {
11. if (shouldMoveMotor) {
12. shouldMoveMotor = false;
13. moveMotor();
14. }
15. }
https://roboticsbackend.com/arduino-interrupts/ 11/19
3/22/2021 Arduino Interrupts Tutorial - The Robotics Back-End
16.
17. void triggerMoveMotor() {
18. shouldMoveMotor = true;
19. }
20.
21. void moveMotor() {
22. // this function may contains code that
23. // requires heavy computation, or takes
24. // a long time to execute
25. }
And you can do exactly the same for a heavy computation, for
example if the computation takes more than a few microseconds
to complete.
If you don’t keep the interrupts fast, you might miss important
deadlines in your code. For a mobile robot with 2 wheels, that
may make the motor movement jerky. For communication
between devices, you might miss some data, etc.
millis(): this will return the time spent since the Arduino
program has started, in milliseconds. This function relies on
some other interrupts to count, and as you are inside an
interrupt, other interrupts are not running. Thus, if you use
millis(), you’ll get the last stored value, which will be
https://roboticsbackend.com/arduino-interrupts/ 12/19
3/22/2021 Arduino Interrupts Tutorial - The Robotics Back-End
When you are inside an interrupt, the received Serial data may be
lost. Thus it’s not a good idea to use the reading functionalities of
Serial. Also if you make the interrupt too long, and read from
Serial after that in your main code, you may still have lost some
parts of the data.
Volatile variables
The compiler does many things to optimize the code and the
speed of the program. This is a good thing, but here we need to
tell it to “slow down” on optimization.
Also, when you use volatile it tells the controller to reload the
variable whenever it’s referenced. Sometimes the compiler will
use copies of variables to go faster. Here you want to make sure
that every time you access/modify the variable, either in the main
program or inside an interrupt, you get the real variable and not a
copy.
Note that only variables that are used inside and outside an
interrupt should be declared as volatile. You don’t want to
unnecessarily slow down your code.
Thus, the only way to share data with the main program is
through global volatile variables. In an interrupt you can also get
and set data from hardware pins, as long as you keep the
program short. For example, using digitalRead() or digitalWrite()
may be OK if you don’t abuse it.
Conclusion
https://roboticsbackend.com/arduino-interrupts/ 15/19
3/22/2021 Arduino Interrupts Tutorial - The Robotics Back-End
Arduino interrupts are very useful when you want to make sure
you don’t miss any change in a signal you monitor (on a digital
pin mostly).
However, during this post you saw that there are many rules and
limitations when using interrupts. This is something you should
handle with care, and not use too much. Sometimes, using simple
polling may be more appropriate, if for example you manage to
write an efficient and deterministic multitasking Arduino program.
Interrupts can also be used just to trigger a flag, and you keep
using the polling technique inside your main loop() – but this
time, instead of monitoring the hardware pin, you monitor the
software flag.
The main takeaway for you, if you want to use interrupts in your
code: keep your interrupts short. Thus you will avoid many
unnecessary and hard-to-debug problems.
Arduino Tutorials
Name*
John Doe
https://roboticsbackend.com/arduino-interrupts/ 17/19
3/22/2021 Arduino Interrupts Tutorial - The Robotics Back-End
Email Address*
john.doe@gmail.com
.....
SUBSCRIBE
https://roboticsbackend.com/arduino-interrupts/ 18/19
3/22/2021 Arduino Interrupts Tutorial - The Robotics Back-End
https://roboticsbackend.com/arduino-interrupts/ 19/19