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
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:
- Set pinMode to INPUT for the wakeup pin
- Attach Interrupt for this pin RISING
- Use
nrf_gpio_cfg_sense_input
to attach a second interrupt for the chip to recognize this pin also for the wakeup?
- 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
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