Skip to content

Commit 0b37daa

Browse files
authored
Merge pull request #680 from sebromero/sebromero/pmic-fix
[AE-74] Revamp Battery API for Nicla Sense ME
2 parents fb8531f + 1ca6fc3 commit 0b37daa

File tree

11 files changed

+1455
-142
lines changed

11 files changed

+1455
-142
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*
2+
* This example shows how to use the Nicla Sense ME library to charge a battery.
3+
*
4+
* The LED color will change depending on the battery's operating status:
5+
* - Blue: Ready
6+
* - Yellow: Charging
7+
* - Green: Charging complete
8+
* - Red: Error
9+
*
10+
* Instructions:
11+
* 1. Connect a single cell (3.7V nominal) LiPo/Li-Ion battery to the board via battery connector (J4) or the pin header (J3).
12+
* 2. Configure the charge current in the setup() function.
13+
* 3. Upload this sketch to your Nicla Sense ME board.
14+
*
15+
* Initial author: Sebastian Romero @sebromero
16+
*/
17+
18+
#include "Nicla_System.h"
19+
20+
constexpr auto printInterval { 10000ul };
21+
float voltage = -1.0f;
22+
23+
void setup(){
24+
Serial.begin(115200);
25+
for (const auto timeout = millis() + 2500; millis() < timeout && !Serial; delay(250));
26+
nicla::begin(); // Initialise library
27+
nicla::leds.begin(); // Start I2C connection to LED driver
28+
nicla::setBatteryNTCEnabled(true); // Set to false if your battery doesn't have a NTC.
29+
30+
/*
31+
Set the maximum charging time to 9 hours. This helps to prevent overcharging.
32+
Set this to a lower value (e.g. 3h) if your battery will be done with charging sooner.
33+
To get an estimation of the charging time, you can use the following formula:
34+
Charging time (in hours) = (Battery capacity in mAh) / (0.8 * Charging current in mA)
35+
This formula takes into account that the charging process is approximately 80% efficient (hence the 0.8 factor).
36+
This is just a rough estimate, and actual charging time may vary depending on factors like the charger, battery quality, and charging conditions.
37+
*/
38+
nicla::configureChargingSafetyTimer(ChargingSafetyTimerOption::NineHours);
39+
40+
/*
41+
A safe default charging current value that works for most common LiPo batteries is 0.5C,
42+
which means charging at a rate equal to half of the battery's capacity.
43+
For example, a 200mAh battery could be charged at 100mA (0.1A).
44+
*/
45+
nicla::enableCharging(100);
46+
47+
nicla::leds.setColor(blue);
48+
}
49+
50+
void loop(){
51+
52+
static auto updateTimestamp = millis();
53+
54+
if (millis() - updateTimestamp >= printInterval) {
55+
updateTimestamp = millis();
56+
57+
float currentVoltage = nicla::getCurrentBatteryVoltage();
58+
if(currentVoltage != voltage){
59+
voltage = currentVoltage;
60+
Serial.print("\nVoltage: ");
61+
Serial.println(voltage);
62+
} else {
63+
Serial.print(".");
64+
}
65+
66+
auto operatingStatus = nicla::getOperatingStatus();
67+
68+
switch(operatingStatus) {
69+
case OperatingStatus::Charging:
70+
nicla::leds.setColor(255,100,0); // Yellow
71+
break;
72+
case OperatingStatus::ChargingComplete:
73+
nicla::leds.setColor(green);
74+
75+
// This will stop further charging until enableCharging() is called again.
76+
nicla::disableCharging();
77+
break;
78+
case OperatingStatus::Error:
79+
nicla::leds.setColor(red);
80+
break;
81+
case OperatingStatus::Ready:
82+
nicla::leds.setColor(blue);
83+
break;
84+
default:
85+
nicla::leds.setColor(off);
86+
break;
87+
}
88+
89+
}
90+
}

0 commit comments

Comments
 (0)