batterie-lipo.ino
Battery life under control: periodic reading of the LiPo voltage on the MKR GSM 1400 J6 sense line, with alert thresholds. The 2G modem draws current peaks, so watching VBAT avoids hard resets mid-transmission.
Hardware
An Arduino MKR GSM 1400 (SAMD21 microcontroller, 2G/GPRS cellular modem, external antenna, powered by a LiPo battery on the J6 connector or over USB). A 3.7 V LiPo on the board battery connector (single cell), or charging over USB; no external sensor.
Wiring
LiPo battery on the board's battery connector (mind the polarity), antenna fitted for realistic readings.
How it works
The SAMD21 measures VBAT through an internal divider; readVbat() from the SAMD core returns the true voltage. The 4.0/3.8/3.5 V thresholds match classic lithium-polymer discharge levels.
Running it
In the Arduino IDE, install the Arduino SAMD Boards package, select the MKR GSM 1400 board, then upload. Network examples assume an active data SIM is inserted, the antenna is screwed on, and 2G coverage still exists in your area.
Variants
Shut the modem down (gsm.shutdown()) below 3.5 V, add an SD-card logger, or plot the discharge curve in a spreadsheet to calibrate real autonomy.
batterie-lipo.ino
// Suivi de la batterie LiPo branchee sur le connecteur J6 du MKR.
#include <MKRGSM.h>
#include <battery_soc.h> // exemple de l'IDE : estimation state-of-charge
GSM gsm;
const long PERIODE = 30000; // lecture toutes les 30 s
unsigned long dernier = 0;
void setup() {
Serial.begin(9600);
while (!Serial);
analogReadResolution(10); // 10 bits pour VBAT sur SAMD21
Serial.println(F("Suivi batterie LiPo (J6)"));
}
void loop() {
if (millis() - dernier < PERIODE) return;
dernier = millis();
int brut = analogRead(VBAT); // diviseur interne 2/3
float vbus = 3.3 * brut / 1023.0 * 1.5;
float vbat = readVbat(); // helper du package SAMD
Serial.print("VBAT brut="); Serial.print(brut);
Serial.print(" ~"); Serial.print(vbus, 2); Serial.print(" V | ");
Serial.print("LiPo "); Serial.print(vbat, 2);
Serial.print(" V -> ");
if (vbat > 4.0) Serial.println(F("chargee"));
else if (vbat > 3.8) Serial.println(F("normale"));
else if (vbat > 3.5) Serial.println(F("faible, recharge!"));
else Serial.println(F("CRITIQUE, debranche le modem"));
}