|
| 1 | +/****************************************************************************** |
| 2 | +Example1_Simple |
| 3 | +By: Paul Clark |
| 4 | +Date: October 23rd 2020 |
| 5 | +
|
| 6 | +Based extensively on: |
| 7 | +MAX17043_Simple_Serial.cpp |
| 8 | +SparkFun MAX17043 Example Code |
| 9 | +Jim Lindblom @ SparkFun Electronics |
| 10 | +Original Creation Date: June 22, 2015 |
| 11 | +
|
| 12 | +This file demonstrates the simple API of the SparkFun MAX17043 Arduino library. |
| 13 | +
|
| 14 | +This example will print the gauge's voltage and state-of-charge (SOC) readings |
| 15 | +to Serial (115200 baud) |
| 16 | +
|
| 17 | +This code is released under the MIT license. |
| 18 | +
|
| 19 | +Distributed as-is; no warranty is given. |
| 20 | +******************************************************************************/ |
| 21 | + |
| 22 | +#include <SparkFun_MAX1704x_Fuel_Gauge_Arduino_Library.h> // Click here to get the library: http://librarymanager/All#SparkFun_MAX1704x_Fuel_Gauge_Arduino_Library |
| 23 | + |
| 24 | +SFE_MAX1704X lipo; |
| 25 | + |
| 26 | +double voltage = 0; // Variable to keep track of LiPo voltage |
| 27 | +double soc = 0; // Variable to keep track of LiPo state-of-charge (SOC) |
| 28 | +bool alert; // Variable to keep track of whether alert has been triggered |
| 29 | + |
| 30 | +void setup() |
| 31 | +{ |
| 32 | + Serial.begin(115200); // Start serial, to output debug data |
| 33 | + |
| 34 | + // Set up the MAX17043 LiPo fuel gauge: |
| 35 | + lipo.begin(); // Initialize the MAX17043 LiPo fuel gauge |
| 36 | + |
| 37 | + // Quick start restarts the MAX17043 in hopes of getting a more accurate |
| 38 | + // guess for the SOC. |
| 39 | + lipo.quickStart(); |
| 40 | + |
| 41 | + // We can set an interrupt to alert when the battery SoC gets too low. |
| 42 | + // We can alert at anywhere between 1% - 32%: |
| 43 | + lipo.setThreshold(20); // Set alert threshold to 20%. |
| 44 | +} |
| 45 | + |
| 46 | +void loop() |
| 47 | +{ |
| 48 | + // lipo.getVoltage() returns a voltage value (e.g. 3.93) |
| 49 | + voltage = lipo.getVoltage(); |
| 50 | + // lipo.getSOC() returns the estimated state of charge (e.g. 79%) |
| 51 | + soc = lipo.getSOC(); |
| 52 | + // lipo.getAlert() returns a 0 or 1 (0=alert not triggered) |
| 53 | + alert = lipo.getAlert(); |
| 54 | + |
| 55 | + // Print the variables: |
| 56 | + Serial.print("Voltage: "); |
| 57 | + Serial.print(voltage); // Print the battery voltage |
| 58 | + Serial.println(" V"); |
| 59 | + |
| 60 | + Serial.print("Alert: "); |
| 61 | + Serial.println(alert); |
| 62 | + |
| 63 | + Serial.print("Percentage: "); |
| 64 | + Serial.print(soc); // Print the battery state of charge |
| 65 | + Serial.println(" %"); |
| 66 | + Serial.println(); |
| 67 | + |
| 68 | + delay(500); |
| 69 | +} |
0 commit comments