-
-
Notifications
You must be signed in to change notification settings - Fork 213
[AE-74] Revamp Battery API for Nicla Sense ME #680
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 47 commits
Commits
Show all changes
48 commits
Select commit
Hold shift + click to select a range
965fb69
nicla-system: Rework battery API.
sebromero d8fc798
nicla-sense-pmic: Use function to toggle Hi-Z mode.
sebromero 20ee494
nicla-system: Add battery monitor web app.
sebromero 864d9a3
nicla-system: Add battery check sketch for Nicla Sense.
sebromero bd0ee3e
nicla-system: Add function for toggling battery NTC.
sebromero a47ec88
nicla-system: Refactor names.
sebromero 254eecb
nicla-system: Remove duplicate functionality.
sebromero 66bc141
nicla-system: Add API for operating system.
sebromero 3b0af63
nicla-system: Rename function.
sebromero edaf109
nicla-system: Use getFaultsRegister to query faults register.
sebromero 03cbd92
nicla-system: Add documentation.
sebromero 1c55bb5
nicla-system: Remove battery status function.
sebromero c29e363
nicla-system: Replace defines with enum for battery level.
sebromero 9f7b9a9
nicla-system: Add getter for fast charge register.
sebromero f14d519
nicla-system: Replace battery temperature with enum.
sebromero 6e8f454
nicla-system: Add API to set regulated battery voltage.
sebromero d2c93a0
nicla-system: Move function to read LDO register to pmic driver.
sebromero 56b67be
nicla-system: Catch error case.
sebromero d69d3ad
nicla-system: Rename function name.
sebromero 444a74d
nicla-system: Improve efficiency of voltage calculation.
sebromero 0708980
nicla-system: Fix incorrect INT disable.
sebromero e6ff5c5
nicla-system: Add documentation to charging function.
sebromero 6d8c08a
nicla-system: Rename charging function.
sebromero 9f1b6cc
nicla-system: Avoid High-Z mode when powered from VIN.
sebromero ad89fb1
nicla-system: Add function to check if board runs on battery.
sebromero 6ab5b66
nicla-system: Add documentation to LDO functions.
sebromero 6286728
nicla-system: Add function to disable charging.
sebromero abbc54f
nicla-system: Disable charging while taking a battery reading.
sebromero 2871417
nicla-system: Display charging and power status.
sebromero de371ef
nicla-system: Make BLE updates more robust.
sebromero 4dee246
nicla-system: Add documentation to the web app.
sebromero 48fb48e
nicla-system: Change order of setup instructions.
sebromero cea8fa2
nicla-system: Add function for backwards compatibility.
sebromero b16d5ea
nicla-system: Give PMIC more time to wake up.
sebromero b3d7f82
nicla-system: Add error messages to sketch.
sebromero f7845c2
nicla-system: Fix incorrect battery voltage reading.
sebromero d47a6db
nicla-system: Add documentation about charging rate.
sebromero a0b13de
nicla-system: Remove NTC toggling from charge function.
sebromero f5e6718
nicla-system: Add more documentation.
sebromero d2271c1
nicla-system: Simplify the watchdog reset machanism.
sebromero 14117b6
nicla-system: Remove unused code.
sebromero d399ee3
nicla-system: Increase safety charge time from 3 to 9 hours.
sebromero 9bce6e8
nicla-system: Add more documentation.
sebromero c316d06
nicla-system: Add sketch that shows how to charge the battery.
sebromero 21a7c56
nicla-system: Add new measurement of charge current to docs.
sebromero e51a021
nicla-system: Add option to configure safety timer.
sebromero 5fff648
nicla-system: Make the safety timer configuration explicit.
sebromero 1ca6fc3
nicla-system: Apply suggestions from Ali's code review.
sebromero File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
90 changes: 90 additions & 0 deletions
90
...System/examples/NiclaSenseME_BatteryChargingSimple/NiclaSenseME_BatteryChargingSimple.ino
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/* | ||
* This example shows how to use the Nicla Sense ME library to charge a battery. | ||
* | ||
* The LED colors will change depending on the battery's operating status: | ||
* - Blue: Ready | ||
* - Yellow: Charging | ||
* - Green: Charging complete | ||
* - Red: Error | ||
* | ||
* Instructions: | ||
* 1. Connect a battery to the board. | ||
sebromero marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* 2. Configure the charge current in the setup() function. | ||
* 3. Upload this sketch to your Nicla Sense ME board. | ||
* | ||
* Initial author: Sebastian Romero @sebromero | ||
*/ | ||
|
||
#include "Nicla_System.h" | ||
|
||
constexpr auto printInterval { 10000ul }; | ||
float voltage = -1.0f; | ||
|
||
void setup(){ | ||
Serial.begin(115200); | ||
for (const auto timeout = millis() + 2500; millis() < timeout && !Serial; delay(250)); | ||
nicla::begin(); // Initialise library | ||
nicla::leds.begin(); // Start I2C connection to LED driver | ||
nicla::setBatteryNTCEnabled(true); // Set to false if your battery doesn't have a NTC. | ||
|
||
/* | ||
Set the maximum charging time to 9 hours. This helps to prevent overcharging. | ||
Set this to a lower value (e.g. 3h) if your battery will be done with charging sooner. | ||
To get an estimation of the charging time, you can use the following formula: | ||
Charging time (in hours) = (Battery capacity in mAh) / (0.8 * Charging current in mA) | ||
This formula takes into account that the charging process is approximately 80% efficient (hence the 0.8 factor). | ||
This is just a rough estimate, and actual charging time may vary depending on factors like the charger, battery quality, and charging conditions. | ||
*/ | ||
nicla::configureChargingSafetyTimer(ChargingSafetyTimerOption::NineHours); | ||
|
||
/* | ||
A safe default charging current value that works for most common LiPo batteries is 0.5C, | ||
which means charging at a rate equal to half of the battery's capacity. | ||
For example, a 200mAh battery could be charged at 100mA (0.1A). | ||
*/ | ||
nicla::enableCharging(100); | ||
sebromero marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
nicla::leds.setColor(blue); | ||
} | ||
|
||
void loop(){ | ||
|
||
static auto updateTimestamp = millis(); | ||
|
||
if (millis() - updateTimestamp >= printInterval) { | ||
updateTimestamp = millis(); | ||
|
||
float currentVoltage = nicla::getCurrentBatteryVoltage(); | ||
if(currentVoltage != voltage){ | ||
voltage = currentVoltage; | ||
Serial.print("\nVoltage: "); | ||
Serial.println(voltage); | ||
} else { | ||
Serial.print("."); | ||
} | ||
|
||
auto operatingStatus = nicla::getOperatingStatus(); | ||
|
||
switch(operatingStatus) { | ||
case OperatingStatus::Charging: | ||
nicla::leds.setColor(255,100,0); // Yellow | ||
break; | ||
case OperatingStatus::ChargingComplete: | ||
nicla::leds.setColor(green); | ||
|
||
// This will stop further charging until enableCharging() is called again. | ||
nicla::disableCharging(); | ||
break; | ||
case OperatingStatus::Error: | ||
nicla::leds.setColor(red); | ||
break; | ||
case OperatingStatus::Ready: | ||
nicla::leds.setColor(blue); | ||
break; | ||
default: | ||
nicla::leds.setColor(off); | ||
break; | ||
} | ||
|
||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.