Arduino programming guide series

The interrupt service routine and volatile variables

Interrupt service routines (ISR) must be as small as possible. If your sketch has to do time-consuming work after an interrupt event, you can use volatile variables to capture data and process it outside of the ISR.

If your sketch is trying to do too many things inside an interrupt service routine, it may will run into problems that will be very difficult to troubleshoot.

For example, trying to update a port expander (such as the MCP23017 from Microchip) from an ISR will fail.

Why?

The problem with doing a write operation to the expander is that the operation may be take too much time to complete through the SPI interface.

In general, the workload inside an interrupt service routine should be very small. How much is "very small"? Think of operations as simple as updating an microcontroller register or memory location. 

That's it.

Operations like writing to the serial monitor that take more than a few processing cycles should be avoided.

In place of longer-running operations inside the ISR, you should write code that simply updates a volatile variable. When your sketch returns to the loop() function, it can read the data from the same volatile variable and process it as needed.

In other words, a volatile variable allows you to get data out of an ISR, to the rest of your sketch. This way, you can do the longer processing you need, without increasing the footprint of the ISR.

To declare a volatile variable, use the “volatile” keyword, like this:

volatile int temp = 0;

Then, you can use it like any other variable. Inside the ISR:

void isr()
{
      temp = analogRead(0); //Keep this block very short!
}

Finally inside the loop():

loop()
{
     Serial.println(temp); // plus lots more code
}

The volatile keyword informs the compiler that the flagged variable can change at any time, from any part of the sketch or program. In the Arduino that only has one processing unit and no true parallel programming is possible, this happens in the interrupt service routine only.

If you want to learn more about port expanders, interrupts and the volatile keyword, consider our comprehensive course Arduino Step by Step Getting Serious.

Tech Explorations Arduino intermediate level

Done with the basics? Looking for more advanced topics?

Arduino Step by Step Getting Serious is our comprehensive Arduino course for people ready to go to the next level.

Learn about Wi-Fi, BLE and radio, motors (servo, DC and stepper motors with various controllers), LCD, OLED and TFT screens with buttons and touch interfaces, control large loads like relays and lights, and much much MUCH more.


Last Updated 9 months ago.

We publish fresh content each week. Read how-to's on Arduino, ESP32, KiCad, Node-RED, drones and more. Listen to interviews. Learn about new tech with our comprehensive reviews. Get discount offers for our courses and books. Interact with our community. One email per week, no spam; unsubscribe at any time

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}