XIAO nrf52840 Staying in deep sleep while on battery power

I haven’t been able to find great answers as to what is going on with my nrf52840. Consider the following code:

#include <Adafruit_SPIFlash.h>
#include <Arduino.h>
#include <U8g2lib.h>
#ifdef U8X8_HAVE_HW_SPI
#include <SPI.h>
#endif
#ifdef U8X8_HAVE_HW_I2C
#include <Wire.h>
#endif
#include <nrf52840.h>

Adafruit_FlashTransport_QSPI flashTransport;
U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/U8X8_PIN_NONE);

#define SLEEP_TIMEOUT 7000
#define PIN_WAKEUP (28) 

unsigned long startTimeoutTime = 0;

void QSPIF_sleep(void) {
  flashTransport.begin();
  flashTransport.runCommand(0xB9);
  flashTransport.end();
}

void setup() {
  u8g2.begin();

  pinMode(LED_RED, OUTPUT);
  pinMode(LED_GREEN, OUTPUT);
  
  digitalWrite(LED_RED, HIGH);
  digitalWrite(LED_GREEN, HIGH);

  QSPIF_sleep();

  digitalWrite(LED_GREEN, LOW);
  delay(1000);
  digitalWrite(LED_GREEN, HIGH);

  NRF_POWER->DCDCEN = 1;
}

void loop() {
  if (startTimeoutTime == 0) startTimeoutTime = millis();

  if (startTimeoutTime > 0 && ((millis() - startTimeoutTime) > SLEEP_TIMEOUT)) {
    startTimeoutTime = 0;
    enterStandbySleep();
  } else {
    u8g2.setPowerSave(0);
    u8g2.clearBuffer();
    u8g2.setFont(u8g2_font_profont11_mf);
    u8g2.setDisplayRotation(U8G2_R3);
    u8g2.setCursor(10, 10);
    u8g2.print(F("Hello!"));
    u8g2.sendBuffer();
  }
}

void enterStandbySleep() {
  digitalWrite(LED_RED, LOW);
  delay(1000);
  digitalWrite(LED_RED, HIGH);

  u8g2.setPowerSave(1);
  NRF_GPIOTE->INTENCLR |= 0x80000000;   // disable interrupt for event PORT
  nrf_gpio_cfg_sense_input(PIN_WAKEUP, NRF_GPIO_PIN_PULLDOWN, NRF_GPIO_PIN_SENSE_HIGH);
  NRF_GPIOTE->EVENTS_PORT = 0;          // Event not generated
  NRF_GPIOTE->INTENSET |= 0x80000000;   // enable interrupt for event PORT
  NRF_POWER->SYSTEMOFF = 1;
}

It seems to work fine when plugged in to my computer. Green LED blinks, display says “Hello!” for 7 seconds, red LED blinks, and it goes to deep sleep. Then clicking a button on pin 2 wakes it up.

If you power the XIAO with a battery though (in my case, and 9v passed through a buck converter to get it 3.7) it will not stay asleep. After the red LED blinks and it goes to sleep, it wakes right back up and the green LED blinks. It will occasionally actually stay asleep, but not often.

I am concerned this is because of the battery itself. I’ve read in a few posts that trying to power too much with a 9v can cause it to dip and reset the microcontroller. Kinda feel like just trying to put it to deep sleep shouldn’t be that power intensive though?

Anyway, been working on this days with nothing to show for it now.

Can you give me more information about the 9V battery and the buck converter and how they are connected to the XIAO?
Generally, buck converters require a large input current at startup. If the 9V battery cannot supply the start-up current, the output voltage may be well below 3.7V.

Your sketch works fine with a LiPo battery connected to the battery pad on the back side.

Yeah, this is what I was worried about. It is a rechargeable lithium ion battery hooked up to one of these and then (at the moment) on to the 5v pin.

Hi there,
Yea, that’s NOT gonna FLY… USE a LIPO connected to the Battery PAds. :v: if you want stability and repeatability.
HTH
GL :slight_smile: PJ :v:

Oh thanks guys, now I feel like I am getting somewhere. I guess this all goes back to - what else? - battery life. One of the reasons I went with the 9v was that form factor is an issue for my project, and the 9v fits really well, and some of the rechargeable ones have pretty high mAh. Higher than what I could find in a lipo battery of a similar form factor. To make that story even longer, the reason I wanted the highest mAh I could get is because I can’t get my power consumption very low, even while sleeping - 1.6mA. My device only lasts a dozen days and I’d love for it to last at the very least a month or two. I just figured my ability to get power consumption lower was probably a lost cause so opted for the largest battery I could.

So anyway, that is the whole story on why I have what is probably a stupid setup!

Please consider a 3.7V LiPo battery.
When the LiPo is connected to the battery pad, it can be charged from USB and the battery voltage can be read by the XIAO.

The XIAO itself consumes little current when it sleeps, but the display is likely to consume the most current.
What is connected to XIAO?
With a slight modification to the sketch, the sleep current can be reduced to 2.5uA by disconnecting the display from XIAO during sleep.