Samd21g18a Sleep Current too high compared to Arduino IDE

I recently moved to PlatformIO for my Moteino M0 (samd21g18a) projects that are battery operated.
Using the Arduino IDE, I measure a sleep current less than 10 uA for the bare board.
When a port the same project to PlatformIO is get 310 uA.
The code is copied from here using the RTCZero library. The following is the Build log.

CONFIGURATION: https://docs.platformio.org/page/boards/atmelsam/moteino_zero.html
PLATFORM: Atmel SAM 4.0.0 > Moteino M0
HARDWARE: SAMD21G18A 48MHz, 32KB RAM, 256KB Flash
DEBUG: Current (atmel-ice) External (atmel-ice, blackmagic, cmsis-dap, jlink)
PACKAGES:
 - framework-arduinosam 4.4.191002
 - toolchain-gccarmnoneeabi 1.70201.0 (7.2.1)
LDF: Library Dependency Finder -> http://bit.ly/configure-pio-ldf
LDF Modes: Finder ~ chain, Compatibility ~ soft
Found 19 compatible libraries
Scanning dependencies...
Dependency Graph
|-- <RTCZero> 1.6.0
Building in release mode
Checking size .pio\build\moteino_zero\firmware.elf
Advanced Memory Usage is available via "PlatformIO Home > Project Inspect"
RAM:   [=         ]   9.0% (used 2960 bytes from 32768 bytes)
Flash: [          ]   4.9% (used 12976 bytes from 262144 bytes)

I tried another code example using the Arduino Low Power library and also got a large sleep current. Has anyone else checked the sleep current for their PlatformIO projects. I would hate to go back to the Ardunio IDE.

What exact versions for the SAMD platform and the RTCZero library have you installed in the Arduino IDE? What board setting are you using the Arduino IDE? What’s your complete platformio.ini?

So this code?

#include <RTCZero.h>

RTCZero zerortc;

// Set how often alarm goes off here
const byte alarmSeconds = 3;
const byte alarmMinutes = 0;
const byte alarmHours = 0;

volatile bool alarmFlag = false; // Start awake

#if defined (MOTEINO_M0)
  #if defined(SERIAL_PORT_USBVIRTUAL)
    #define Serial SERIAL_PORT_USBVIRTUAL // Required for Serial on Zero based boards
  #endif
#endif

void setup()
{
  Serial.begin(115200);
  delay(1000); // Wait for console
  
  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, LOW);

  zerortc.begin(); // Set up clocks and such
  
  resetAlarm();  // Set alarm
  zerortc.attachInterrupt(alarmMatch); // Set up a handler for the alarm
}

void loop()
{
  if (alarmFlag == true) {
    alarmFlag = false;  // Clear flag
    digitalWrite(LED_BUILTIN, HIGH);
    Serial.println("Alarm went off - I'm awake!");
  }
  resetAlarm();  // Reset alarm before returning to sleep
  Serial.println("Alarm set, going to sleep now.");
  digitalWrite(LED_BUILTIN, LOW);
  zerortc.standbyMode();    // Sleep until next alarm match
}

void alarmMatch(void)
{
  alarmFlag = true; // Set flag
}

void resetAlarm(void) {
  byte seconds = 0;
  byte minutes = 0;
  byte hours = 0;
  byte day = 1;
  byte month = 1;
  byte year = 1;
  
  zerortc.setTime(hours, minutes, seconds);
  zerortc.setDate(day, month, year);

  zerortc.setAlarmTime(alarmHours, alarmMinutes, alarmSeconds);
  zerortc.enableAlarm(zerortc.MATCH_HHMMSS);
}

Thanks for your interest. The above code is the correct example.

On Arduino IDE 1.8.10, I am using:
Arduino SAMD Board 1.8.4
LowPowerLab SAMD Boards 1.5.0
RTCZero 1.5.2

My platformio.ini

[libraries]
rtczero = RTCZero@1.6.0

[env:moteino_zero]
platform = atmelsam
board = moteino_zero
framework = arduino

lib_deps = 
  ${libraries.rtczero}

build_flags =
  -w ; supress all warnings

I tried using RTCZero 1.5.2 in PlaformIO and RTCZero 1.6.0 in Arduino IDE.
No change Ardunio sleep current is under 10 uA. PlatformIO is 400 uA.

SOLUTION

Upgrading the platform fixes this problem
PLATFORM: Atmel SAM 4.1.0 > Moteino M0
Those on battery power need to re-build there projects!

This is outdated. Please run pio platform update or do the udpate via the VSCode GUI.

For me I get

CONFIGURATION: https://docs.platformio.org/page/boards/atmelsam/moteino_zero.html
PLATFORM: Atmel SAM 4.1.0 > Moteino M0
HARDWARE: SAMD21G18A 48MHz, 32KB RAM, 256KB Flash
DEBUG: Current (atmel-ice) External (atmel-ice, blackmagic, cmsis-dap, jlink)
PACKAGES: 
 - framework-arduino-samd-moteino 1.3.0 
 - framework-cmsis 1.40500.0 (4.5.0) 
 - framework-cmsis-atmel 1.2.0 
 - toolchain-gccarmnoneeabi 1.70201.0 (7.2.1)

Though we also see that PlatformIO currently uses the 1.3.0 version which is slightly outdated, so that’s a possible cause. I’ll see what steps need to be done in order to temporarily get the newer framework version.

Updating to Atmel SAM 4.1.0 > Moteino M0 was the SOLUTION for me.
Thanks for the help.

1 Like

Alright great because upgrading to LowPowerLab SAMD Boards 1.5.0 threw a bunch of very weird USB related errors for me so glad that that was all it needed…

Have you read this update with respect to the LowPowerLab SAMD Boards 1.5.0 ?
This update seems sensible in that it changes SerialUSB.print() to Serial.print(). This was done to be compatible with all the other Arduino boards using the USB serial port. Still need to add this to your code:

#if defined (MOTEINO_M0)
  #if defined(SERIAL_PORT_USBVIRTUAL)
    #define Serial SERIAL_PORT_USBVIRTUAL
  #endif
#endif

On the Moteino M0 there are two other ports now called Serial0 and Serial1 which can be used to communicate with other terminals. Unfortunately, the RTC sleep example above, doesn’t actually work with USB port as it does not recover from going in and out of sleep. Apparently, the other ports ( Serial0 and Serial1) can be used to get around this problem.

Does this explain the USB errors you saw? If so, can the PlatformIO board support be updated to include LowPowerLab SAMD Boards 1.5.0 ?

But for the USB port, USBDevice.detach(); and USBDevice.attach(); exists.

Well you made me revisit my work and I realized my mistake :sweat_smile:. You can get the latest version working with a manual tweak.

  1. Download MoteinoSAMD1.5.zip
  2. Find your C:\Users\<user>\.platformio\packages\framework-arduino-samd-moteino folder
  3. Remove everything but the package.json file in this folder
  4. Extract the contents of the downloaded MoteinSAMD1.5 ZIP file in that folder
  5. Adapt the PlatformIO to add certain macros, include paths and a change of the variant folder name:
[libraries]
rtczero = RTCZero@1.6.0

[env:moteino_zero]
platform = atmelsam
board = moteino_zero
framework = arduino
board_build.variant = moteino_m0
lib_deps = 
  ${libraries.rtczero}
build_flags = 
	-D ARM_MATH_CM0PLUS
	-D USE_TINYUSB
	-I C:\\Users\\Maxi\\.platformio\\packages\\framework-arduino-samd-moteino\\cores\\arduino\\TinyUSB
	-I C:\\Users\\Maxi\\.platformio\\packages\\framework-arduino-samd-moteino\\cores\\arduino\\TinyUSB\\Adafruit_TinyUSB_ArduinoCore
	-I C:\\Users\\Maxi\\.platformio\\packages\\framework-arduino-samd-moteino\\cores\\arduino\\TinyUSB\\Adafruit_TinyUSB_ArduinoCore\\tinyusb\\src

Here the paths all have to be adapted as well (username at least)

Then the RTC sketch compiles as previously

RAM:   [=         ]  13.0% (used 4256 bytes from 32768 bytes)
Flash: [=         ]   6.5% (used 16996 bytes from 262144 bytes)
========================= [SUCCESS] Took 5.28 seconds =========================

A feature request in Issues · platformio/platform-atmelsam · GitHub should be filed for proper integration though.