|
| 1 | +/****************************************************************************** |
| 2 | +Example4: test all the things on the MAX17048 |
| 3 | +By: Paul Clark, SparkFun Electronics |
| 4 | +Date: October 23rd 2020 |
| 5 | +
|
| 6 | +This example is an everything-but-the-kitchen-sink test of the MAX17048. |
| 7 | +
|
| 8 | +This code is released under the MIT license. |
| 9 | +
|
| 10 | +Distributed as-is; no warranty is given. |
| 11 | +******************************************************************************/ |
| 12 | + |
| 13 | +#include <Wire.h> // Needed for I2C |
| 14 | + |
| 15 | +#include <SparkFun_MAX1704x_Fuel_Gauge_Arduino_Library.h> // Click here to get the library: http://librarymanager/All#SparkFun_MAX1704x_Fuel_Gauge_Arduino_Library |
| 16 | + |
| 17 | +SFE_MAX1704X lipo(MAX1704X_MAX17048); // Create a MAX17048 |
| 18 | + |
| 19 | +void setup() |
| 20 | +{ |
| 21 | + Serial.begin(115200); // Start serial, to output debug data |
| 22 | + while (!Serial) |
| 23 | + ; //Wait for user to open terminal |
| 24 | + Serial.println(F("MAX17048 Example")); |
| 25 | + |
| 26 | + Wire.begin(); |
| 27 | + |
| 28 | + lipo.enableDebugging(); // Uncomment this line to enable helpful debug messages on Serial |
| 29 | + |
| 30 | + // Set up the MAX17048 LiPo fuel gauge: |
| 31 | + if (lipo.begin() == false) // Connect to the MAX17048 using the default wire port |
| 32 | + { |
| 33 | + Serial.println(F("MAX17048 not detected. Please check wiring. Freezing.")); |
| 34 | + while (1) |
| 35 | + ; |
| 36 | + } |
| 37 | + |
| 38 | + // Just because we can, let's reset the MAX17048 |
| 39 | + Serial.println(F("Resetting the MAX17048...")); |
| 40 | + delay(1000); // Give it time to get its act back together |
| 41 | + |
| 42 | + // Read and print the reset indicator |
| 43 | + Serial.print(F("Reset Indicator was: ")); |
| 44 | + bool RI = lipo.isReset(true); // Read the RI flag and clear it automatically if it is set |
| 45 | + Serial.println(RI); // Print the RI |
| 46 | + // If RI was set, check it is now clear |
| 47 | + if (RI) |
| 48 | + { |
| 49 | + Serial.print(F("Reset Indicator is now: ")); |
| 50 | + RI = lipo.isReset(); // Read the RI flag |
| 51 | + Serial.println(RI); // Print the RI |
| 52 | + } |
| 53 | + |
| 54 | + // To quick-start or not to quick-start? That is the question! |
| 55 | + // Read the following and then decide if you do want to quick-start the fuel gauge. |
| 56 | + // "Most systems should not use quick-start because the ICs handle most startup problems transparently, |
| 57 | + // such as intermittent battery-terminal connection during insertion. If battery voltage stabilizes |
| 58 | + // faster than 17ms then do not use quick-start. The quick-start command restarts fuel-gauge calculations |
| 59 | + // in the same manner as initial power-up of the IC. If the system power-up sequence is so noisy that the |
| 60 | + // initial estimate of SOC has unacceptable error, the system microcontroller might be able to reduce the |
| 61 | + // error by using quick-start." |
| 62 | + // If you still want to try a quick-start then uncomment the next line: |
| 63 | + //lipo.quickStart(); |
| 64 | + |
| 65 | + // Read and print the device ID |
| 66 | + Serial.print(F("Device ID: 0x")); |
| 67 | + uint8_t id = lipo.getID(); // Read the device ID |
| 68 | + if (id < 0x10) Serial.print(F("0")); // Print the leading zero if required |
| 69 | + Serial.println(id, HEX); // Print the ID as hexadecimal |
| 70 | + |
| 71 | + // Read and print the device version |
| 72 | + Serial.print(F("Device version: 0x")); |
| 73 | + uint8_t ver = lipo.getVersion(); // Read the device version |
| 74 | + if (ver < 0x10) Serial.print(F("0")); // Print the leading zero if required |
| 75 | + Serial.println(ver, HEX); // Print the version as hexadecimal |
| 76 | + |
| 77 | + // Read and print the battery threshold |
| 78 | + Serial.print(F("Battery empty threshold is currently: ")); |
| 79 | + Serial.print(lipo.getThreshold()); |
| 80 | + Serial.println(F("%")); |
| 81 | + |
| 82 | + // We can set an interrupt to alert when the battery SoC gets too low. |
| 83 | + // We can alert at anywhere between 1% and 32%: |
| 84 | + lipo.setThreshold(20); // Set alert threshold to 20%. |
| 85 | + |
| 86 | + // Read and print the battery empty threshold |
| 87 | + Serial.print(F("Battery empty threshold is now: ")); |
| 88 | + Serial.print(lipo.getThreshold()); |
| 89 | + Serial.println(F("%")); |
| 90 | + |
| 91 | + // Read and print the high voltage threshold |
| 92 | + Serial.print(F("High voltage threshold is currently: ")); |
| 93 | + float highVoltage = ((float)lipo.getVALRTMax()) * 0.02; // 1 LSb is 20mV. Convert to Volts. |
| 94 | + Serial.print(highVoltage, 2); |
| 95 | + Serial.println(F("V")); |
| 96 | + |
| 97 | + // Set the high voltage threshold |
| 98 | + lipo.setVALRTMax((float)4.1); // Set high voltage threshold (Volts) |
| 99 | + |
| 100 | + // Read and print the high voltage threshold |
| 101 | + Serial.print(F("High voltage threshold is now: ")); |
| 102 | + highVoltage = ((float)lipo.getVALRTMax()) * 0.02; // 1 LSb is 20mV. Convert to Volts. |
| 103 | + Serial.print(highVoltage, 2); |
| 104 | + Serial.println(F("V")); |
| 105 | + |
| 106 | + // Read and print the low voltage threshold |
| 107 | + Serial.print(F("Low voltage threshold is currently: ")); |
| 108 | + float lowVoltage = ((float)lipo.getVALRTMin()) * 0.02; // 1 LSb is 20mV. Convert to Volts. |
| 109 | + Serial.print(lowVoltage, 2); |
| 110 | + Serial.println(F("V")); |
| 111 | + |
| 112 | + // Set the low voltage threshold |
| 113 | + lipo.setVALRTMin((float)3.9); // Set low voltage threshold (Volts) |
| 114 | + |
| 115 | + // Read and print the low voltage threshold |
| 116 | + Serial.print(F("Low voltage threshold is now: ")); |
| 117 | + lowVoltage = ((float)lipo.getVALRTMin()) * 0.02; // 1 LSb is 20mV. Convert to Volts. |
| 118 | + Serial.print(lowVoltage, 2); |
| 119 | + Serial.println(F("V")); |
| 120 | + |
| 121 | + // Enable the State Of Change alert |
| 122 | + Serial.print(F("Enabling the 1% State Of Change alert: ")); |
| 123 | + if (lipo.enableSOCAlert()) |
| 124 | + { |
| 125 | + Serial.println(F("success.")); |
| 126 | + } |
| 127 | + else |
| 128 | + { |
| 129 | + Serial.println(F("FAILED!")); |
| 130 | + } |
| 131 | + |
| 132 | + // Read and print the HIBRT Active Threshold |
| 133 | + Serial.print(F("Hibernate active threshold is: ")); |
| 134 | + float actThr = ((float)lipo.getHIBRTActThr()) * 0.00125; // 1 LSb is 1.25mV. Convert to Volts. |
| 135 | + Serial.print(actThr, 5); |
| 136 | + Serial.println(F("V")); |
| 137 | + |
| 138 | + // Read and print the HIBRT Hibernate Threshold |
| 139 | + Serial.print(F("Hibernate hibernate threshold is: ")); |
| 140 | + float hibThr = ((float)lipo.getHIBRTHibThr()) * 0.208; // 1 LSb is 0.208%/hr. Convert to %/hr. |
| 141 | + Serial.print(hibThr, 3); |
| 142 | + Serial.println(F("%/h")); |
| 143 | +} |
| 144 | + |
| 145 | +void loop() |
| 146 | +{ |
| 147 | + // Print the variables: |
| 148 | + Serial.print("Voltage: "); |
| 149 | + Serial.print(lipo.getVoltage()); // Print the battery voltage |
| 150 | + Serial.print("V"); |
| 151 | + |
| 152 | + Serial.print(" Percentage: "); |
| 153 | + Serial.print(lipo.getSOC(), 2); // Print the battery state of charge with 2 decimal places |
| 154 | + Serial.print("%"); |
| 155 | + |
| 156 | + Serial.print(" Change Rate: "); |
| 157 | + Serial.print(lipo.getChangeRate(), 2); // Print the battery change rate with 2 decimal places |
| 158 | + Serial.print("%/hr"); |
| 159 | + |
| 160 | + Serial.print(" Alert: "); |
| 161 | + Serial.print(lipo.getAlert()); // Print the generic alert flag |
| 162 | + |
| 163 | + Serial.print(" Voltage High Alert: "); |
| 164 | + Serial.println(lipo.isVoltageHigh()); // Print the alert flag |
| 165 | + |
| 166 | + Serial.print(" Voltage Low Alert: "); |
| 167 | + Serial.println(lipo.isVoltageLow()); // Print the alert flag |
| 168 | + |
| 169 | + Serial.print(" Empty Alert: "); |
| 170 | + Serial.print(lipo.isLow()); // Print the alert flag |
| 171 | + |
| 172 | + Serial.print(" SOC 1% Change Alert: "); |
| 173 | + Serial.print(lipo.isChange()); // Print the alert flag |
| 174 | + |
| 175 | + Serial.print(" Hibernating: "); |
| 176 | + Serial.print(lipo.isHibernating()); // Print the alert flag |
| 177 | + |
| 178 | + Serial.println(); |
| 179 | + |
| 180 | + delay(500); |
| 181 | +} |
0 commit comments