XIAO BLE Sense in deep sleep mode

No, you are getting confused. We are talking about the external flash chip that is on board the XIAO not the internal flash on the nrf that your code is written to.

If you dont need the external flash chip disable it like I showed. Then just use delay, the nrf core automatically turns off most things not in use. If you really care about low power you need to get a power profiler to test with.

1 Like

Thanks for reply - now a bit more clear for me :slight_smile:
sad that seeed is not publishing that info … looks like everyting must be “discovered” by users like You.
Thanks for help.

Hi! It’s so amazing you managed to figure out how to put it on sleep in platformio. I tried to follow the guide to build using adafruit 1.0.0 library and platformio. I created a small sketch to test this out. But I find I might miss some constants like D1, D2, D3 used in disconnectpin. Apart of flashTransport that seems I cannot find the proper library.

My platformio is similar to this one:

[env:adafruit_feather_nrf52840]
platform = nordicnrf52
board = xiao_ble_sense
framework = arduino
lib_deps =
    olikraus/U8g2@^2.28.8
    SPI
    adafruit/Adafruit SPIFlash@^4.0.0
    adafruit/SdFat - Adafruit Fork@^2.2.1
    seeed-studio/Seeed Arduino LSM6DS3@^2.0.3

Could you please share the platformio.ini file you’re using including the main.cpp to test this? I’ve been searching this anywhere but seems there’s still little or no information.

Also it’s a discouraging that you managed to discover the flash issue even before of Seedstudio team releasing it for us. I was really happy with Xiao (SAMD) but this board seems to be a timewaster, no matter what you try to do on it as wearable, I’m alwayis hitting on a rock.

I appreciate so much your effort to uncover these hidden details.

[env:adafruit_feather_nrf52840]
platform = nordicnrf52
board = xiao_ble_sense
framework = arduino
lib_deps = 
	adafruit/Adafruit SPIFlash@^4.0.0
	adafruit/SdFat - Adafruit Fork@^2.2.1

Platform.ini looks fine, maybe you are missing the Arduino.h include for your Dx pin defs. Note that when calling nrf api directly through nrf functions, macros or register manipulations you must use nrf pin numbers, these are mapped via the g_ADigitalPinMap global array, e.g. nrfPin = g_ADigitalPinMap[D0]

#include <Arduino.h>
#include <bluefruit.h>
#include <Adafruit_FlashTransport.h>

#define PIN_POT   A0
#define PIN_BTN   D7
#define PIN_POT_GND D10

Hey, quite a big discussion. I read a lot of it multiple times and still didn’t manage to get the XIAO nrf52840 to sleep and wake on interrupt.
Indeed it seems like everyone is just comparing their sleep current, but no-one ever made it work to wake up the board. At least there is no complete functioning code in this discussion.

@Rhys3logy Asked for the same thing that I’m about, but he never got an answer:

So I’ll give exact information on what I’m doing:
Board: XIAO nRF52840
Boards Manager: Speed nRF52 Board
IDE: Arduino IDE

This is my minimal code to turn the chip off, which doesn’t work:

void setup() {
  pinMode(LED_GREEN, OUTPUT);
  pinMode(LED_RED, OUTPUT);
}

int c;
void loop() {
  c++;
  digitalWrite(LED_GREEN, c % 2 == 0 ? HIGH : LOW);
  delay(1000);
  if (c == 10) {
    digitalWrite(LED_RED, LOW);
    sd_power_system_off();
  }
}

This should put the board to sleep right? Well sd_power_system_off() does exactly nothing. It executes and the program just continues.
I managed to get the board to shut down via the mentioned NRF_POWER->SYSTEMOFF = 1; but I tried everything to wake it up, but the only way to do it seems to be a reset via reset button, or killing the power and plugging it back in.

So this sample is so minimal, and the task ist so basic. Is there anyone who actually got this to work and can post steps to reproduce? The fact that sd_power_system_off() doesn’t do anything on my side makes me suspicious, maybe this could hint something?

I’m new to hardware programming and this is my second project and the first with this chip. What I want to do in the very end (getting the thing to shut down and at least restart upon button press would be the first step) - is to:

  • Go into sleep
  • Wake up every now and again (just like delay() but non-blocking)
  • Check some variables and the time (they must persist)
  • Go back to sleep
  • Additional: Wake Up on button press and run the program normally

Maybe some of the experts here can help, we spent a whole day with 2 experienced programmers and didn’t manage to do that.

Greetings from Germany,
Noblauch

1 Like

Hi , this may not be exactly what you want but I’m using Sense version and double tap to wake up from sleep; here is a snip from the link here, I have a video on here showing it work as well.

void goToPowerOff() {
  setLedRGB(false, false, false);
  Serial.println("Going to System OFF");
  u8x8.clearDisplay();
  u8x8.setCursor(0, 3);
  u8x8.print("SLEEP");
  setupDoubleTapInterrupt(); // not needed here, if already applied..
  delay(1000); // delay seems important to apply settings, before going to System OFF
  //Ensure interrupt pin from IMU is set to wake up device
  nrf_gpio_cfg_sense_input(digitalPinToInterrupt(int1Pin), NRF_GPIO_PIN_PULLDOWN, NRF_GPIO_PIN_SENSE_HIGH);
  delay(2000); // delay seems important to apply settings, before going to System OFF 
  NRF_POWER->SYSTEMOFF = 1;
}

The Nrf52840 supports a couple levels of sleep and even more when you turn off peripherals etc.
the Nordic forum has more on the topic as well.
HTH
GL :slight_smile:

As I said somewhere above unless you start the soft device none of your sd_* calls will do anything, it isn’t started automagically by the arduino core…

One way to start the soft device is Bluefruit.begin()

what is it “soft device” .
I found that term several times but can not find the explanation of it.
is it an adafruit library ? ble library ?

SoftDevice is the Nordic supplied software binary that implements the wireless protocols and other functions ontop of the nrf hardware. A bit like the binary blob on the RPI. If you never use any sd_* calls then you don’t need to start it, however to do anything wireless other than messing directly with the radio via registers you go via SD.

Hey @PJ_Glasso & @devmonkey thanks for your replies!

I already tried some hours in my freetime, but your code also doesn’t work for the XIAO nRF52840 :confused:

Thanks for talking about the “soft device”. I’m not using that and atm I don’t need to. Also I googled Bluefruit and saw this is kind of a breakout board. I don’t have all this, I only have the bare XIAO nRF52840 and plain Arduino IDE, nothing else.

@PJ_Glasso I destilled your code to the essential parts to make it compile (don’t have the display and so on) and tried it out, without success. I see the important parts are potentially:

  1. Set pinMode to INPUT for the wakeup pin
  2. Attach Interrupt for this pin RISING
  3. Use nrf_gpio_cfg_sense_input to attach a second interrupt for the chip to recognize this pin also for the wakeup?
  4. Use NRF_POWER->SYSTEMOFF = POWER_SYSTEMOFF_SYSTEMOFF_Enter to go to sleep

I was not able to wake the controller up, here is my final code:

#include "Wire.h"

const int wakeupPin = 10;        // the number of the pushbutton pin
uint8_t interruptCount = 0;      // Amount of received interrupts
uint8_t prevInterruptCount = 0;  // Interrupt Counter from last loop

void setup() {
  Serial.begin(9600);
  delay(1000);
  Serial.println("Processor came out of reset.\n");

  pinMode(LED_RED, OUTPUT);
  pinMode(LED_GREEN, OUTPUT);
  pinMode(LED_BLUE, OUTPUT);
  setLedRGB(false, false, true);  // set blue led

  pinMode(wakeupPin, INPUT);
  attachInterrupt(digitalPinToInterrupt(wakeupPin), interrupt, RISING);
}

void interrupt() {
  Serial.println("Interrupt");
  interruptCount++;
}

void loop() {
  setLedRGB(false, false, true);
  Serial.print("Iterrupt Counter: ");
  Serial.println(interruptCount);

  // if interrupt was received in this cycle
  if (interruptCount > prevInterruptCount) {
    Serial.println("Interrupt received!");
    setLedRGB(false, true, false);  // set green only
  }

  prevInterruptCount = interruptCount;

  delay(500);
  if (interruptCount >= 5) {
    // Trigger System OFF after 5 interrupts
    goToPowerOff();
  }
}

void goToPowerOff() {
  setLedRGB(false, false, false);
  Serial.println("Going to System OFF");
  delay(1000);
  //Ensure interrupt pin from IMU is set to wake up device
  nrf_gpio_cfg_sense_input(digitalPinToInterrupt(wakeupPin), NRF_GPIO_PIN_PULLDOWN, NRF_GPIO_PIN_SENSE_HIGH);

  // Trigger System OFF
  NRF_POWER->SYSTEMOFF = POWER_SYSTEMOFF_SYSTEMOFF_Enter;
}

void setLedRGB(bool red, bool green, bool blue) {
  if (!blue) {
    digitalWrite(LED_BLUE, HIGH);
  } else {
    digitalWrite(LED_BLUE, LOW);
  }
  if (!green) {
    digitalWrite(LED_GREEN, HIGH);
  } else {
    digitalWrite(LED_GREEN, LOW);
  }
  if (!red) {
    digitalWrite(LED_RED, HIGH);
  } else {
    digitalWrite(LED_RED, LOW);
  }
}

Also what makes me wonder is the fact that you didn’t set the button pinmode input is not set to pullup or down, which makes the interrupt trigger as crazy automatically, but hey it shouldnt affect the rest of the code. However its not doing anything but shutting the chip down :frowning:

Not sure why it seems so impossible, but I really can’t get this to work, just plain vanilla Arduino and the bare XIAO. I’m simultaniously on different platforms and dedicated to get this to work. If I ever do, I’ll post my findings here so other people can get the sleep to work too!

Best,
Noblauch

2 Likes

I’m starting to think tha we all would benefit if we create a github repo with a schematic of the bare xiao ble sense with a simple demostration of making this board to deep sleep AND recover via some pulse/Button.

Some simple schematic with two buttons. Sleep AND wake. Something with a basic arduino IDE project and a platform.io alternative. I’m very basic at electronics, but I managed to get the basic xiao samd to sleep and recover in just 2 hours. And i’ve spent days trying to figure out this one (without success) which is crazy…

If no one does this I will create a public repo in a few days with some of my failures and some basic stuff we could use to learn how to deep sleep this xiao

1 Like

The Bluefruit is a library built to work with the Nordic softdevice. If you want to use BLE then you have three choices. Write your own register level code. Use the Mbed version of the XIAO BLE Sense library for BLE or use the Adafruit (version 1) of the XIAO BLE Sense library, which is Bluefruit.

Many people seem to have problems with the Mbed libraries and use the Adafruit instead because they are much better documented.

In most cases, the Mbed library, and the Adafruit library are not compatible. You have to make a choice.

1 Like

There are already codes posted above using the Adafruit library where you could bring the sleep down to at least 23 µA and in some cases down to 3.

I don’t know if they work on Mbed.

… or the 4’th option to use arduinoBLE library …???
writing “BLE” You meant “XIAO BLE” or BLE (bluetooth) ?

Hi thanks for replying!, which code do you think is valid for bare XIAO BLE SENSE (no groove, just plain xiao) with Adafruit bootloader?

I’ve tested many of the added codes but some of them include specific interrupts for groove or just makes the xiao to go shutdown and restart constantly.

As soon as I manage to get this one working I’ll share a github with just an example of this. I’m still find quite difficult to follow some conclusions of this thread, including some others like @Noblauch that seem still having issues.

Hi, Seems like apples and oranges, I’m using successfully the “double tap” for interrupt generation and the accelerometer config to generate it. and for the shut down or sleep, the most important is the delay after

nrf_gpio_cfg_sense_input(digitalPinToInterrupt(int1Pin), NRF_GPIO_PIN_PULLDOWN, NRF_GPIO_PIN_SENSE_HIGH);
delay(2000); // delay seems important to apply settings, before going to System OFF 
  NRF_POWER->SYSTEMOFF = 1;

otherwise the system wakes back up immediately.
My demo with code will stay asleep for as long as I don’t touch(Tap) it. 2-3 days easy. :slight_smile:
HTH
GL :-p

1 Like

Uses Adafruit version 1.0 of the board files.

Seeed has two versions of the board files (not the hardware).

  1. uses the Mbed structure/libraries
  2. uses Adafruit libraries and bootloader and the Nordic softdevice.

Mbed has their own BLE library and Adafruit has its own BLE library.

The ArduinoBLE library that is used with the Arduino Nano BLE 33 board is also used with the XIA BLE board that was setup for Mbed.

The Adaftuit BLE library is called with Bluefruit.begin() and is used with the XIAO BLE that is using the Adafruit setup… Programs written for the Mbed setup of the XIAO BLE most of the time do not work with XIAO BLE board using the Adafruit setup.

So when you install your XIA BLE board in the Arduino IDE, you need to decide which board files (and libraries) you want to use: Mbed or Adafruit.

1 Like

Good explanation ! Thanks. It’s clearing the view of XIAO BLE boards.:clap::+1:

One more question regarding this part :

Is ArduinoBLE library working properly with XIAO BLE board that was setup for Adafruit (not Mbed) ?

It will not compile properly.