Tutorial 4: Interrupts

Hardware interrupts are an incredibly useful feature of the Arduino. Interrupts make it such that you don't miss an input event like the push of a button or a pulse from another device. Instead of actively polling a pin for input you can let your program do other things until the interrupt is triggered. Once interrupted, the Arduino will call a specific subroutine. Interrupts are important for time sensitive tasks such as button presses or timing signals.

DIFFICULTY
EASY
CIRCUITRY KNOWLEDGE
LITTLE
C++ PROGRAMMING
LITTLE
ABOUT
0
MINUTES
  • When to use an interrupt as compared to a digitalRead
  • How to identify and connect to an Arduino’s hardware interrupts
  • How to program your Arduino such that you can receive and use interrupts
You can copy / paste the code below if you’re having issues with typos or want a shortcut. However I recommend that you follow along in the tutorial to understand what is going on!


int buttonPin = 8 ;
int LED_Pin = 13 ;
bool buttonPress ;

void buttonFunc(){
  buttonPress = true ;
}

void setup() {
  pinMode(LED_Pin, OUTPUT) ;
  attachInterrupt(0, buttonFunc, RISING) ;
}

void loop() {
  //buttonPress = digitalRead(buttonPin) ;

  if (buttonPress) {
    digitalWrite(LED_Pin, HIGH) ;
    buttonPress = false ;
    delay(1000) ;
    digitalWrite(LED_Pin, LOW) ;
  }
  //else {
  //  digitalWrite(LED_Pin, LOW) ;
  //}

  delay(4000) ;
  
}