Department of Engineering

Arduino interrupts

What is an interrupt? - It's something that interrupts the normal activity of a program.

When are they used? - When something might happen but you don't know when. They shouldn't be used for predictable, sequential events.

How do you use them? - You need to specify what will trigger the interrupt, and what will happen when it's triggered. You use the attachInterrupt function to do this - see the arduino interrupt documentation.

After the interrupt, the program will continue what it was doing when it was interrupted.

The interrupt service routine (the function that's run when there's an interrupt) is special in various ways -

  • It takes and returns no values (so global variables need to be used, and those variables, if the routine changes them, should be volatile)
  • The delay function can't be used in the routine

The routine should do as little as possible, returning as fast as possible - serial data received while the function's running may be lost.