arduino / ArduinoCore-megaavr

Arduino Core for the ATMEGA4809 CPU
103 stars 62 forks source link

Sleeping does not decrease current draw #137

Open Electric131 opened 1 month ago

Electric131 commented 1 month ago

I'm trying to get my Arduino Nano Every to go into a low-power / deep sleep mode. Even while sleeping, the board still draws ~20mA. I'm trying to get the consumption to (hopefully) <5mA if not less.

I've tried using the Adafruit_SleepyDog library, but MegaAVR seems to be incompatible with that library. Here's the code I've tried, which does sleep but actually increases the mA draw and still stays at >20mA: (I found most of this code from a forum post, and added a CPU prescaler in an attempt to decrease power draw)

#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);
    CPU_CCP = CCP_IOREG_gc;                         // write access for four CPU instructions
    CLKCTRL_MCLKCTRLB = CLKCTRL_PDIV_16X_gc | CLKCTRL_PEN_bm;   // prescaler 16, enable prescaler
  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 / 16);                       // wait for a second
  digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
  delay(200 / 16);                       // wait for a second

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

Quick note that I am trying to run it at ~4V where it draws ~21mA while sleeping. And, because I was curious, at ~8V it still draws ~18mA. Also I am powering it through the VIN pin.