|
| 1 | +/****************************************************************************** |
| 2 | +Example3_MAX17044 |
| 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 how to talk to the MAX17044 using 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 <Wire.h> // Needed for I2C |
| 23 | + |
| 24 | +#include <SparkFun_MAX1704x_Fuel_Gauge_Arduino_Library.h> // Click here to get the library: http://librarymanager/All#SparkFun_MAX1704x_Fuel_Gauge_Arduino_Library |
| 25 | + |
| 26 | +SFE_MAX1704X lipo(10); // The MAX17044 has a full-scale voltage of 10V. Specify it here. (int) |
| 27 | + |
| 28 | +double voltage = 0; // Variable to keep track of LiPo voltage |
| 29 | +double soc = 0; // Variable to keep track of LiPo state-of-charge (SOC) |
| 30 | +bool alert; // Variable to keep track of whether alert has been triggered |
| 31 | + |
| 32 | +void setup() |
| 33 | +{ |
| 34 | + Serial.begin(115200); // Start serial, to output debug data |
| 35 | + while (!Serial) |
| 36 | + ; //Wait for user to open terminal |
| 37 | + Serial.println(F("MAX17044 Example")); |
| 38 | + |
| 39 | + Wire.begin(); |
| 40 | + |
| 41 | + lipo.enableDebugging(); // Uncomment this line to enable helpful debug messages on Serial |
| 42 | + |
| 43 | + // Set up the MAX17044 LiPo fuel gauge: |
| 44 | + if (lipo.begin() == false) // Connect to the MAX17044 using the default wire port |
| 45 | + { |
| 46 | + Serial.println(F("MAX17044 not detected. Please check wiring. Freezing.")); |
| 47 | + while (1) |
| 48 | + ; |
| 49 | + } |
| 50 | + |
| 51 | + // Quick start restarts the MAX17044 in hopes of getting a more accurate |
| 52 | + // guess for the SOC. |
| 53 | + lipo.quickStart(); |
| 54 | + |
| 55 | + // We can set an interrupt to alert when the battery SoC gets too low. |
| 56 | + // We can alert at anywhere between 1% - 32%: |
| 57 | + lipo.setThreshold(20); // Set alert threshold to 20%. |
| 58 | +} |
| 59 | + |
| 60 | +void loop() |
| 61 | +{ |
| 62 | + // lipo.getVoltage() returns a voltage value (e.g. 7.86) |
| 63 | + voltage = lipo.getVoltage(); |
| 64 | + // lipo.getSOC() returns the estimated state of charge (e.g. 79%) |
| 65 | + soc = lipo.getSOC(); |
| 66 | + // lipo.getAlert() returns a 0 or 1 (0=alert not triggered) |
| 67 | + alert = lipo.getAlert(); |
| 68 | + |
| 69 | + // Print the variables: |
| 70 | + Serial.print("Voltage: "); |
| 71 | + Serial.print(voltage); // Print the battery voltage |
| 72 | + Serial.println(" V"); |
| 73 | + |
| 74 | + Serial.print("Alert: "); |
| 75 | + Serial.println(alert); |
| 76 | + |
| 77 | + Serial.print("Percentage: "); |
| 78 | + Serial.print(soc); // Print the battery state of charge |
| 79 | + Serial.println(" %"); |
| 80 | + Serial.println(); |
| 81 | + |
| 82 | + delay(500); |
| 83 | +} |
0 commit comments