examples(bq27441): Add practical usage examples. #73#188
Conversation
There was a problem hiding this comment.
Pull request overview
Adds two BQ27441 fuel-gauge example sketches addressing issue #73: one prints periodic battery telemetry to serial, the other triggers visual/audible alerts on low charge. Both wire the gauge to the STeaMi internal I2C bus.
Changes:
- New
BatteryStatusexample printing voltage, SOC, remaining capacity and SOH every 3 s. - New
LowBatteryAlertexample blinkingLED_REDbelow 20 % SOC and beepingSPEAKERbelow 5 % SOC.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 6 comments.
| File | Description |
|---|---|
| lib/bq27441/examples/BatteryStatus/BatteryStatus.cpp | Baseline telemetry sketch for the BQ27441 |
| lib/bq27441/examples/LowBatteryAlert/LowBatteryAlert.cpp | Low-SOC alert sketch using LED + buzzer |
Notes: Both files use a .cpp extension and PascalCase folders, which conflict with the example layout required by CONTRIBUTING.md (lower snake-case folders and matching .ino sketch file) — the PR description itself uses battery_status / low_battery_alert. Also, neither sketch prints a diagnostic when sensor.begin() fails, diverging from the established sibling examples.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| @@ -0,0 +1,44 @@ | |||
| // SPDX-License-Identifier: GPL-3.0-or-later | |||
There was a problem hiding this comment.
fix in commit : 598d855
Rename example files to snake_case.
| @@ -0,0 +1,57 @@ | |||
| // SPDX-License-Identifier: GPL-3.0-or-later | |||
There was a problem hiding this comment.
fix in commit : 598d855
Rename example files to snake_case.
| if (!sensor.begin()) { | ||
| while (true) { | ||
| delay(1000); | ||
| } | ||
| } |
There was a problem hiding this comment.
fix in commit : 8131689
Print an error message before the infinite loop so the user knows the sensor was not detected instead of silently blocking.
| if (!sensor.begin()) { | ||
| while (true) { | ||
| delay(1000); | ||
| } | ||
| } |
There was a problem hiding this comment.
fix in commit : 4e8401b
Print an error message before the infinite loop so the user knows the sensor was not detected instead of silently blocking.
| void loop() { | ||
| if (sensor.stateOfCharge() <= 20) { | ||
| blink(); | ||
| delay(200); | ||
| } | ||
| if (sensor.stateOfCharge() <= 5) { | ||
| buzzAlert(); | ||
| delay(1000); | ||
| } else { | ||
| delay(1000); | ||
| } |
There was a problem hiding this comment.
fix in commit : 4e8401b
Cache stateOfCharge() once at the top of loop() to avoid two I2C transactions per iteration — both reads could straddle a threshold and produce inconsistent results.
| Serial.print("mV \n"); | ||
| Serial.print("State of Charge: "); | ||
| Serial.print(sensor.stateOfCharge()); | ||
| Serial.print("% \n"); | ||
| Serial.print("Remaining Capacity: "); | ||
| Serial.print(sensor.capacityRemaining()); | ||
| Serial.print("mAh \n"); | ||
| Serial.print("State of Health: "); | ||
| Serial.print(sensor.stateOfHealth()); | ||
| Serial.println("% \n"); |
There was a problem hiding this comment.
fix in commit : 8131689
Replace Serial.print(...) + "\n" with Serial.println(...) to follow Arduino convention and remove the trailing space before the newline. Also remove the extra blank line produced by the last Serial.println("% \n").
Summary
Add two example sketches for the BQ27441 fuel gauge driver.
close #73
Changes
Add
battery_status— prints voltage (mV), state of charge (%),remaining capacity (mAh) and state of health (%) to the serial
monitor every 3 seconds.
Add
low_battery_alert— blinks the red LED when the battery dropsbelow 20 % and sounds the on-board buzzer when it falls below 5 %.
Checklist
make lintpasses (clang-format)make buildpasses (PlatformIO)make test-nativepasses (if native tests exist)make test-hardwarepasses on a connected STeaMi (if hardware tests exist)