Hello, after much research I found the best battery optimization for MKR1310 :
here is the code :
#include <LoRa.h>
#include <ArduinoLowPower.h>
#include <Arduino_PMIC.h>
//#define DEBUG
#ifdef DEBUG
#define DEBUG_PRINT(x) Serial.print(x)
#define DEBUG_PRINTLN(x) Serial.println(x)
#else
#define DEBUG_PRINT(x)
#define DEBUG_PRINTLN(x)
#endif
#define LED_BUILTIN 6
unsigned long previousMillis = 0;
int PMIC_Status;
bool PMIC_PowerGood;
const int pin = 0;
void setup() {
#ifdef DEBUG
Serial.begin(9600);
while (!Serial);
#endif
for (int i = 0; i <= 14; i++) {
pinMode(i, INPUT_PULLUP);
}
if (!PMIC.begin()) DEBUG_PRINTLN("PMIC initialization error!");
else DEBUG_PRINTLN("PMIC initialized!");
// LoRa Example
if (!LoRa.begin(868E6)) DEBUG_PRINTLN("LoRa startup failed!");
else DEBUG_PRINTLN("LoRa startup OK !");
LoRa.setSpreadingFactor(7);
LoRa.setSignalBandwidth(500E3);
LoRa.setCodingRate4(6);
LoRa.enableCrc();
// For a 3.7V 5000mAh Battery
PMIC.setInputCurrentLimit(2.0);
PMIC.setInputVoltageLimit(3.88);
PMIC.setMinimumSystemVoltage(3.5);
PMIC.setChargeVoltage(4.2);
PMIC.setChargeCurrent(1.0);
DEBUG_PRINTLN("Adjusted load parameters !");
if (!PMIC.enableCharge()) DEBUG_PRINTLN("Error enabling Charge mode");
USBDevice.detach();
if (!Serial) Serial.end();
// Arduino wake-up on the "pin"
LowPower.attachInterruptWakeup(pin, wakeUp, FALLING);
}
void loop() {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= 1000) {
previousMillis = currentMillis;
PMIC_Status = PMIC.chargeStatus();
PMIC_PowerGood = PMIC.isPowerGood();
}
if (PMIC_PowerGood) {
if (PMIC_Status == PRE_CHARGING || PMIC_Status == FAST_CHARGING) {
// Battery is charging
}
else if (PMIC_Status == CHARGE_TERMINATION_DONE) {
// Battery is full
} else {
// Error Charging
}
}
}
// MKR Deep Sleep Function
void Veille() {
DEBUG_PRINTLN("Mise en veille...");
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, LOW);
//
USBDevice.detach();
Serial.end();
LoRa.end();
ADC->CTRLA.bit.ENABLE = 0;
while (ADC->STATUS.bit.SYNCBUSY);
//
digitalWrite(LORA_IRQ_DUMB, LOW);
digitalWrite(LORA_BOOT0, LOW);
digitalWrite(LORA_RESET, LOW);
pinMode(LORA_IRQ_DUMB, INPUT);
pinMode(LORA_BOOT0, INPUT);
pinMode(LORA_RESET, INPUT);
//
LowPower.deepSleep();
}
void wakeUp() {
// Restart LoRa
if (!LoRa.begin(868E6)) DEBUG_PRINTLN("LoRa startup failed!");
else DEBUG_PRINTLN("LoRa startup OK !");
// Reset if you want
//NVIC_SystemReset();
}