Arduino noInterrupts, interrupts, sei() & cli() Functions

In this tutorial, we’ll learn how to use the Arduino noInterrupts() function and sei() & cli() macros to control global interrupts enable/disable. We’ll discuss how to create critical sections in the Arduino code using this feature and why you shouldn’t use it excessively throughout your project. Without further ado, let’s get right into it!

Table of Contents

  1. Arduino Global Interrupts Control
  2. Arduino noInterrupts() Function
  3. Arduino Interrupts() Function
  4. Arduino cli() Function
  5. Arduino sei() Function
  6. Wrap Up

Arduino Global Interrupts Control

Sometimes, under certain circumstances, you may need to have a critical section in your code. Which is usually a couple of instructions that you want the CPU to run through without getting interrupted. This can be achieved by disabling and re-enabling the global interrupts in the microcontroller.

Arduino (Atmega328p) has a global interrupt enable/disable control bit that you can use for this purpose. There are two wrapper functions for this functionality that you can use instead of direct bit manipulation.

  • noInterrupts(): Disables global interrupts.
  • interrupts(): Re-enables global interrupts after being disabled.

Keep in mind that you should minimize the utilization of this feature as much as possible. And it’s going to disrupt the timing functions (like millis, micros, and delay functions).

Equivalently, there are two Arduino macros (functions) that also provide the exact same functionality but they are much faster since they are macros. Those two macros are as follows:

  • cli(): Clear interrupt global enable flag bit (disable all interrupts).
  • sei(): Set interrupt global enable flag bit (re-enable interrupts after being disabled).

Obviously, cli() function is similar to noInterrupts() function. And sei() function is similar to interrupts() function. But the difference is speed as stated earlier. There are actually SEI & CLI assembly instructions in the instruction set of Arduino’s Atmega328p microcontroller.

Arduino-sei-cli-Functions

❕ Note

Excessive usage of critical sections in your code can and will disrupt timing functions and make things harder to track. So try to minimize the usage of this feature and use it reasonably when you’re in very bad need of it.


Arduino noInterrupts() Function

Disables interrupts (you can re-enable them with  interrupts()). Interrupts allow certain important tasks to happen in the background and are enabled by default. Some functions will not work while interrupts are disabled, and incoming communication may be ignored. Interrupts can slightly disrupt the timing of code, however, and may be disabled for particularly critical sections of code.

Syntax

It takes no parameter and returns nothing.


Arduino Interrupts() Function

Re-enables interrupts (after they’ve been disabled by the  noInterrupts() function. Interrupts allow certain important tasks to happen in the background and are enabled by default. Some functions will not work while interrupts are disabled, and incoming communication may be ignored. Interrupts can slightly disrupt the timing of code, however, and may be disabled for particularly critical sections of code.

Syntax

It takes no parameter and returns nothing.

Code Example


Arduino cli() Function

The Arduino cli() macro is similar to the noInterrupts() function, as it also disables the global interrupts flag bit. The difference is that it’s a macro for an assembly instruction (CLI) so it runs much faster.

Syntax

It takes no parameter and returns nothing.


Arduino sei() Function

The Arduino sei() macro is similar to the interrupts() function, as it also re-enables global interrupts after begin disabled. The difference is that it’s a macro for an assembly instruction (SEI) so it runs much faster.

Syntax

It takes no parameter and returns nothing.

Code Example


Parts List

Here is the full components list for all parts that you’d need in order to perform the practical LABs mentioned here in this article and for the whole Arduino Programming series of tutorials found here on DeepBlueMbedded. Please, note that those are affiliate links and we’ll receive a small commission on your purchase at no additional cost to you, and it’d definitely support our work.


Wrap Up

To conclude this short tutorial, we’ll highlight the fact that Arduino noInterrupts() and interutps() functions are very useful to control global interrupts and to create critical sections in your code when needed. There are also alternatives which are the sei() & cli() macros that will run even faster and do the same functionality as we’ve discussed earlier.

If you’re just getting started with Arduino, you need to check out the Arduino Getting Started [Ultimate Guide] here.

And follow this Arduino Series of Tutorials to learn more about Arduino Programming.

???? Also Read
Getting Started With Arduino Programming For Beginners

This is the ultimate guide for getting started with Arduino for beginners. It’ll help you learn the Arduino fundamentals for Hardware & Software and understand the basics required to accelerate your learning journey with Arduino Programming.

???? Also Read

This is the complete Arduino Interrupts Tutorial. It’ll provide you with more in-depth information about interrupts in general and specifically in Arduino, how it works, how to write efficient ISR handlers, and other tips & tricks.


FAQ & Answers

What is CLI and SEI?

The Arduino cli() & sei() macros are used to control the global interrupt flag bit. Calling the cli() will clear the flag bit (disable interrupts), while calling the sei() will set the flag bit (re-enable interrupts). They are similar to the Arduino noInterrupts() and interrupts() functions.

How to disable interrupts in Arduino?

You can disable Arduino interrupts using the Arduino noInterrupts() function or the cli() macro both are similar in functionality. And will disable all interrupts in the system. Just be careful that this will disrupt the timing functions in your system, serial communication, and others. So use it carefully to create critical sections in your code and try to keep it reasonable.

How to enable interrupts in Arduino?

If you’ve disabled global interrupts in Arduino, you can re-enable interrupts using the Arduino sei() macro or the interrupts() function. Both are similar in functionality, and will re-enable all interrupts in the system. Interrupts are enabled by default in Arduino, but if you did use cli() or noInterrupts() function, you can still re-enable the interrupts using Arduino sei() or interrutps() function.

Share This Page With Your Network!
Join Our +25,000 Newsletter Subscribers!

Stay Updated With All New Content Releases. You Also Get Occasional FREE Coupon Codes For Courses & Other Stuff!

Photo of author
Author
Khaled Magdy
Embedded systems engineer with several years of experience in embedded software and hardware design. I work as an embedded SW engineer in the Automotive & e-Mobility industry. However, I still do Hardware design and SW development for DSP, Control Systems, Robotics, AI/ML, and other fields I'm passionate about.
I love reading, writing, creating projects, and teaching. A reader by day and a writer by night, it's my lifestyle. I believe that the combination of brilliant minds, bold ideas, and a complete disregard for what is possible, can and will change the world! I will be there when it happens, will you?

Leave a Comment