Help needed sleeping the Atmega4809 Arduino Nano Every

Sleep mode handling is completely different, and much more flexible, with the ATmega4809 on which the new Nano Every is based. This chip has an RTC peripheral and you have to configure this by register entries. Probably, the PIT (periodic Interrupt timer) is the best in your case if you need the Power Down sleep mode.

This may help: http://ww1.microchip.com/downloads/en/AppNotes/TB3213-Getting-Started-with-RTC-90003213A.pdf

If not, put together a very simple sketch for the old Nano which simply demonstrates forcing it to sleep and waking afterwards and I (or someone else) may give you some pointers to convert it for the Nano Every.

EDIT

Here is a simple example of Sleep mode. It performs a cycle of blinking 5 times, then sleeping for 8 seconds.

/*
  Nano Every: Cycle of Blink 5 times then Sleep for 8 seconds
  Configuration option: No ATmega328P Register emulation

*/

#include <avr/sleep.h>
uint8_t loopCount = 0 ;


void RTC_init(void)
{
  while (RTC.STATUS > 0) ;    /* Wait for all register to be synchronized */
 
  RTC.CLKSEL = RTC_CLKSEL_INT1K_gc;        // Run low power oscillator (OSCULP32K) at 1024Hz for long term sleep
  RTC.PITINTCTRL = RTC_PI_bm;              // PIT Interrupt: enabled */

  RTC.PITCTRLA = RTC_PERIOD_CYC8192_gc | RTC_PITEN_bm;     // Set period 8 seconds (see data sheet) and enable PIC                      
}

ISR(RTC_PIT_vect)
{
  RTC.PITINTFLAGS = RTC_PI_bm;          // Clear interrupt flag by writing '1' (required) 
}

void setup() {
  pinMode(LED_BUILTIN, OUTPUT);
  RTC_init();   
  set_sleep_mode(SLEEP_MODE_PWR_DOWN);  // Set sleep mode to POWER DOWN mode 
  sleep_enable();                       // Enable sleep mode, but not going to sleep yet 
}


void loop() {
  digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(200);                       // wait for a second
  digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
  delay(200);                       // wait for a second

  if( ++ loopCount == 5 ) {
    loopCount = 0 ;
    sleep_cpu();                     // Sleep the device and wait for an interrupt to continue
  }
}
1 Like