Wakeup on interrupt

dev board Arduino UNO
firmware Arduino
chip ATmega328P
features wakeup interrupt

Buy the components

Description

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

Code

Download code
#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

Serial console

Serial output from the firmware.

Wakeup on interrupt serial console

Schematic

Wire up the hardware accordingly

Wakeup on interrupt schematic