Periodic wakeup with SAMD21

Updated on 3 September 2021
dev board RobotDyn M0 Mini
chip SAMD21
features wakeup sleep periodic
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.

Before starting

Dependancies

Ensure the following dependancies are downloaded and available:

Code

Download code periodic-wakeup.ino
#include <ArduinoLowPower.h>
#define LED 13

void setup() {
  pinMode(LED, OUTPUT);
  digitalWrite(LED, LOW);

  // Serial1 UART is used because the USB does not work after wakeup
  Serial1.begin(9600);
  // while (!Serial1) {}
  delay(1000);

  Serial1.println("Start of program");
  Serial1.println("Fast blink 10 times");
  blink(10, 200);
}

void loop() {
  Serial1.println("Slow blink 10 times");
  blink(10, 1000);

  Serial1.println("Going to sleep for 10 seconds...");
  LowPower.sleep(10000);  // in milliseconds

  Serial1.println("\n\nAwake");

  Serial1.println("Blink 5 times");
  blink(5, 1000);
}

void blink(int times, int duration) {
  for (int i = 0; i < times; i++) {
    digitalWrite(LED, LOW);
    delay(duration);

    digitalWrite(LED, HIGH);
    delay(duration);
  }
}

Makefile

BOARD?=arduino:samd:mzero_bl
PORT := $(shell ls /dev/cu.usbmodem*)

.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 .*.bin
	rm -f .*.elf
	rm -f .*.hex

Prototype

A photo of the actual setup.

Periodic wakeup with SAMD21 prototype

Schematic

Wire up the hardware accordingly

Periodic wakeup with SAMD21 schematic

Description

Sleep for 30 minutes and wakeup to make 10 LED blinks before going back to sleep again for another 30 minutes.

Note that the serial prints will not work after wakeup. Hence use a USB to UART chip to get the Serial1 prints. This is because during sleep, the entire USB peripheral also sleeps along with the MCU, except for the RTC.

During sleep, only power indicator green LED ON will be lighted on the RobotDyn dev board.

References

Watch