sommeil-every.ino

Arduino Nano Every · File : sommeil-every.ino · Language : Arduino · Use case : Veille et réveil minute sur le 4809

The 4809 carries an RTC driven by the 32 kHz clock: this sketch uses it as a minute alarm and idles the core between wake-ups. The LED blinks every minute, proving the wake-up works while the CPU no longer runs.

Hardware

An Arduino Nano Every (ATmega4809 at 20 MHz, same footprint as the Nano, micro-B USB, separate UART chip, 5 V pins, drop-in compatibility with the classic Nano). Bare Nano Every; the RTC and its 32 kHz clock are on-chip.

Wiring

No wiring; LED_BUILTIN (pin 13) acts as the visual wake-up indicator.

How it works

The megaAVR core RTC (RTCzero class) raises a hardware interrupt on each alarm; the sleep instruction parks the core in idle, woken immediately by the IRQ.

Running it

Install the Arduino megaAVR Boards package in the IDE, select Arduino Nano Every, upload. The USB Serial is separate from hardware Serial1 (RX/TX pins): classic serial code works as is.

Variants

Move to SLEEP_MODE_STANDBY for deeper savings (RAM retained), or also wake on a button via PORT interrupt.

sommeil-every.ino

// Sommeil idle/standby sur le Nano Every avec reveil par RTC interne.
// Le core megaAVR expose une RTC 32 bits : on l'utilise comme reveil.
#include <RTC.h>

RTCzero rtc; // nom de classe du core megaAVR pour la RTC 4809
volatile bool minute = false;

void onMinute() { minute = true; }

void setup() {
  Serial.begin(9600);
  rtc.begin();
  // reveil toutes les minutes pile (MATCH_SS = secondes)
  rtc.setAlarmEvery(60);
  rtc.enableAlarm(rtc.MATCH_SS);
  rtc.attachInterrupt(onMinute);
  pinMode(LED_BUILTIN, OUTPUT);
  Serial.println(F("Endormi, reveil chaque minute"));
}

void loop() {
  if (minute) {
    minute = false;
    digitalWrite(LED_BUILTIN, HIGH); // temoin de reveil
    Serial.print(F("Reveil a "));
    Serial.print(rtc.getHours()); Serial.print(':');
    Serial.print(rtc.getMinutes()); Serial.print(':');
    Serial.println(rtc.getSeconds());
    digitalWrite(LED_BUILTIN, LOW);
  }
  // idle() economise sans perdre la RTC ni l'USB
  // (le 4809 garde l'horloge 32 kHz pour la RTC)
  asm("sleep"); // mode idle : reveil immediat sur interruption
}

← Back to the board