TUTORIAL: Interrupts

Intro

Interrupts are useful for automatically executing things in microcontroller programs and can solve timing problems. Good tasks for using an interrupt may include reading a rotary encoder or monitoring user input.
The Arduino Uno has 2 and the Mega has 6 external interrupt pins available:

  • Arduino Uno/Controllino MINI/INT.x interrupt:                   2/IN0/INT.0  –  3/IN1/INT.1 
  • Arduino Mega/Controllino MAXI,MEGA/INT.x interrupt: 2/D0/INT.0  –  3/D1/INT.1  –  18/IN0/INT.5  –  19/IN1/INT.4  –  20/SDA/INT.3  –  21/SCL/INT.2

On the other hand the pin change interrupts can be enabled on many more pins.

Hardware Required

  • Controllino MINI/MAXI/MEGA
  • 12/24V DC Power supply

Circuit

Note*
Pin header is working on 5V TTL levels. Voltage levels over 5.5V can damage the Controllino permanently.

Code

Controllino MINI/MAXI/MEGA

#include <Controllino.h>

const byte digital_output = CONTROLLINO_D0;
const byte interruptPin = CONTROLLINO_IN1;
volatile byte state = LOW;

void setup() {
 pinMode(digital_output, OUTPUT);
 pinMode(interruptPin, INPUT);
 attachInterrupt(digitalPinToInterrupt(interruptPin), blink, CHANGE);
}

void loop() {
 digitalWrite(digital_output, state);
}

void blink() {
 state = !state;
}

Share This Post

More Tutorials to explore

Board & Library Setup with Platform.io

Intro Platform.io together with VS Code is the current de-facto standard for advanced programmers to develop on Arduino boards. CONTROLLINO is natively compatible with Platform.io

Using a 16×2 LCD

Intro The LiquidCrystal library allows you to control LCD displays that are compatible with the Hitachi HD44780 driver. These are the LCDs that are usually connected over a