Skip to content

Commit c316d06

Browse files
committed
nicla-system: Add sketch that shows how to charge the battery.
1 parent 9bce6e8 commit c316d06

File tree

1 file changed

+79
-0
lines changed

1 file changed

+79
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
* This example shows how to use the Nicla Sense ME library to charge a battery.
3+
*
4+
* The LED colors 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 battery to the board.
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+
A safe default charging current value that works for most common LiPo batteries is 0.5C,
32+
which means charging at a rate equal to half of the battery's capacity.
33+
For example, a 200mAh battery could be charged at 100mA (0.1A).
34+
*/
35+
nicla::enableCharging(100);
36+
nicla::leds.setColor(blue);
37+
}
38+
39+
void loop(){
40+
41+
static auto updateTimestamp = millis();
42+
43+
if (millis() - updateTimestamp >= printInterval) {
44+
updateTimestamp = millis();
45+
46+
float currentVoltage = nicla::getCurrentBatteryVoltage();
47+
if(currentVoltage != voltage){
48+
voltage = currentVoltage;
49+
Serial.print("\nVoltage: ");
50+
Serial.println(voltage);
51+
} else {
52+
Serial.print(".");
53+
}
54+
55+
auto operatingStatus = nicla::getOperatingStatus();
56+
57+
switch(operatingStatus) {
58+
case OperatingStatus::Charging:
59+
nicla::leds.setColor(255,100,0); // Yellow
60+
break;
61+
case OperatingStatus::ChargingComplete:
62+
nicla::leds.setColor(green);
63+
64+
// This will stop further charging until enableCharging() is called again.
65+
nicla::disableCharging();
66+
break;
67+
case OperatingStatus::Error:
68+
nicla::leds.setColor(red);
69+
break;
70+
case OperatingStatus::Ready:
71+
nicla::leds.setColor(blue);
72+
break;
73+
default:
74+
nicla::leds.setColor(off);
75+
break;
76+
}
77+
78+
}
79+
}

0 commit comments

Comments
 (0)