Wakeup on interrupt

Updated on 31 August 2021
dev board Arduino UNO
firmware Arduino
chip ATmega328P
features wakeup interrupt
This tutorial is more than 1 year old. If the steps below do not work, then please check the latest versions and the documentations of the individual tools used.

Buy the components

Code

Download code wakeup-on-interrupt.ino
#include <avr/sleep.h>
#define interruptPin 2  // To wake up, press button to connect PIN 2 to GND

void setup() {
  Serial.begin(115200);
  Serial.println("");
  Serial.println("");
  Serial.println("Start");

  pinMode(LED_BUILTIN, OUTPUT);
  pinMode(interruptPin, INPUT_PULLUP);
}

void loop() {
  doTask();
}

void goToSleep() {
  sleep_enable();
  attachInterrupt(0, wakeUp, LOW);
  set_sleep_mode(SLEEP_MODE_PWR_DOWN);
  digitalWrite(LED_BUILTIN, LOW);
  delay(1000);
  sleep_cpu();

  // Program will start from here on after wake up
  Serial.println("Do task after wakeup");
  doTask();
}

void wakeUp() {
  Serial.println("");
  Serial.println("Interrrupt fired");
  sleep_disable();
  detachInterrupt(0);
}

void doTask() {
  digitalWrite(LED_BUILTIN, HIGH);
  delay(5000);

  Serial.println("Going to sleep");
  Serial.println("Short Pin 2 to GND to wake up!");
  goToSleep();
}

Makefile

BOARD?=arduino:avr:uno
PORT?=/dev/cu.usbmodem14*

.PHONY: default lint all flash clean

default: lint all flash clean

lint:
	cpplint --extensions=ino --filter=-legal/copyright *.ino

all:
	arduino-cli compile --fqbn $(BOARD) ./

flash:
	arduino-cli upload -p $(PORT) --fqbn $(BOARD) ./

clean:
	rm -f .*.hex
	rm -f .*.elf

Schematic

Wire up the hardware accordingly

Wakeup on interrupt schematic

Serial console

Serial output from the firmware.

Wakeup on interrupt serial console

Description

Wakeup the Arduino UNO on interrupt. Press a button wired to PIN 2 and short it to GND to wake it up.