SensorLib 0.5.0
Multi-platform sensor driver library for Arduino, PlatformIO, and ESP-IDF
Loading...
Searching...
No Matches
bq27220_gauge.ino
Go to the documentation of this file.
1
30#include <GaugeDrv.hpp>
31
32#ifndef SENSOR_SDA
33#define SENSOR_SDA 2
34#endif
35
36#ifndef SENSOR_SCL
37#define SENSOR_SCL 3
38#endif
39
41
42void setup()
43{
44 Serial.begin(115200);
45 while (!Serial);
46
47 if (!gauge.begin(Wire, SENSOR_SDA, SENSOR_SCL)) {
48 Serial.println("Failed to BQ27220 - check your wiring!");
49 while (1) {
50 delay(1000);
51 }
52 }
53 Serial.println("Init BQ27220 Sensor success!");
54
55
56
70 // uint32_t key = 0x12345678;
71 // gauge.setAccessKey(key)
72
73 uint16_t newDesignCapacity = 3500;
74 uint16_t newFullChargeCapacity = 3500;
75 gauge.setNewCapacity(newDesignCapacity, newFullChargeCapacity);
76
78
79 //* OperationConfig A *//
80 Serial.print("OperationConfigA Values:0x");
81 Serial.println(config.getConfigA(), HEX);
82
83 Serial.print("External Thermistor Selected: ");
84 Serial.println(config.isTempsSet() ? "YES" : "NO");
85
86 Serial.print("BATT_GD Pin Polarity High: ");
87 Serial.println(config.isBatgPolHigh() ? "YES" : "NO");
88
89 Serial.print("BATT_GD Function Enabled: ");
90 Serial.println(config.isBatgEnEnabled() ? "YES" : "NO");
91
92 Serial.print("Can Enter SLEEP State: ");
93 Serial.println(config.canEnterSleep() ? "YES" : "NO");
94
95 Serial.print("slpwakechg Function Enabled: ");
96 Serial.println(config.isSlpwakechgEnabled() ? "YES" : "NO");
97
98 Serial.print("Write Temperature Function Enabled: ");
99 Serial.println(config.isWrtempEnabled() ? "YES" : "NO");
100
101 Serial.print("Battery Insertion Detection Enabled: ");
102 Serial.println(config.isBienableEnabled() ? "YES" : "NO");
103
104 Serial.print("Battery Insertion Pin Pull - Up Enabled: ");
105 Serial.println(config.isBlPupEnEnabled() ? "YES" : "NO");
106
107 Serial.print("Pin Function Code (PFC) Mode: ");
108 Serial.println(config.getPfcCfg());
109
110 Serial.print("Wake - Up Function Enabled: ");
111 Serial.println(config.isWakeEnEnabled() ? "YES" : "NO");
112
113 Serial.print("Wake - Up Threshold 1: ");
114 Serial.println(config.getWkTh1());
115
116 Serial.print("Wake - Up Threshold 0: ");
117 Serial.println(config.getWkTh0());
118
119 //* OperationConfig B *//
120 Serial.print("\nOperationConfigB Values:0x");
121 Serial.println(config.getConfigB(), HEX);
122
123 Serial.print("Default Seal Option Enabled: ");
124 Serial.println(config.isDefaultSealEnabled() ? "YES" : "NO");
125
126 Serial.print("Non - Removable Option Set: ");
127 Serial.println(config.isNonRemovableSet() ? "YES" : "NO");
128
129 Serial.print("INT_BREM Function Enabled: ");
130 Serial.println(config.isIntBremEnabled() ? "YES" : "NO");
131
132 Serial.print("INT_BATL Function Enabled: ");
133 Serial.println(config.isIntBatLEnabled() ? "YES" : "NO");
134
135 Serial.print("INT_STATE Function Enabled: ");
136 Serial.println(config.isIntStateEnabled() ? "YES" : "NO");
137
138 Serial.print("INT_OCV Function Enabled: ");
139 Serial.println(config.isIntOcvEnabled() ? "YES" : "NO");
140
141 Serial.print("INT_OT Function Enabled: ");
142 Serial.println(config.isIntOtEnabled() ? "YES" : "NO");
143
144 Serial.print("INT_POL Function Enabled (High - Level Polarity): ");
145 Serial.println(config.isIntPolHigh() ? "YES" : "NO");
146
147 Serial.print("INT_FOCV Function Enabled: ");
148 Serial.println(config.isIntFocvEnabled() ? "YES" : "NO");
149
150 delay(10000);
151}
152
153
154void loop()
155{
156 uint32_t startMeasTime = millis();
157
158 if (gauge.refresh()) {
159
160 uint32_t endMesTime = millis();
161
162 Serial.print("Polling time: "); Serial.print(endMesTime - startMeasTime); Serial.println(" ms");
163
164 Serial.println("\nStandard query:");
165 Serial.print("\t- AtRate:"); Serial.print(gauge.getAtRate()); Serial.println(" mA");
166 Serial.print("\t- AtRateTimeToEmpty:"); Serial.print(gauge.getAtRateTimeToEmpty()); Serial.println(" minutes");
167 Serial.print("\t- Temperature:"); Serial.print(gauge.getTemperature() ); Serial.println(" ℃");
168 Serial.print("\t- BatteryVoltage:"); Serial.print(gauge.getVoltage()); Serial.println(" mV");
169 Serial.print("\t- InstantaneousCurrent:"); Serial.print(gauge.getCurrent()); Serial.println(" mAh");
170 Serial.print("\t- RemainingCapacity:"); Serial.print(gauge.getRemainingCapacity()); Serial.println(" mAh");
171 Serial.print("\t- FullChargeCapacity:"); Serial.print(gauge.getFullChargeCapacity()); Serial.println(" mAh");
172 Serial.print("\t- DesignCapacity:"); Serial.print(gauge.getDesignCapacity()); Serial.println(" mAh");
173 Serial.print("\t- TimeToEmpty:"); Serial.print(gauge.getTimeToEmpty()); Serial.println(" minutes");
174 Serial.print("\t- TimeToFull:"); Serial.print(gauge.getTimeToFull()); Serial.println(" minutes");
175 Serial.print("\t- StandbyCurrent:"); Serial.print(gauge.getStandbyCurrent()); Serial.println(" mA");
176 Serial.print("\t- StandbyTimeToEmpty:"); Serial.print(gauge.getStandbyTimeToEmpty()); Serial.println(" minutes");
177 Serial.print("\t- MaxLoadCurrent:"); Serial.print(gauge.getMaxLoadCurrent()); Serial.println(" mA");
178 Serial.print("\t- MaxLoadTimeToEmpty:"); Serial.print(gauge.getMaxLoadTimeToEmpty()); Serial.println(" minute");
179 Serial.print("\t- RawCoulombCount:"); Serial.print(gauge.getRawCoulombCount()); Serial.println(" mAh");
180 Serial.print("\t- AveragePower:"); Serial.print(gauge.getAveragePower()); Serial.println(" mW");
181 Serial.print("\t- InternalTemperature:"); Serial.print(gauge.getInternalTemperature()); Serial.println(" ℃");
182 Serial.print("\t- CycleCount:"); Serial.println(gauge.getCycleCount());
183 Serial.print("\t- StateOfCharge:"); Serial.print(gauge.getStateOfCharge()); Serial.println(" %");
184 Serial.print("\t- StateOfHealth:"); Serial.print(gauge.getStateOfHealth()); Serial.println(" %");
185 Serial.print("\t- RequestChargingVoltage:"); Serial.print(gauge.getRequestChargingVoltage()); Serial.println(" mV");
186 Serial.print("\t- RequestChargingCurrent:"); Serial.print(gauge.getRequestChargingCurrent()); Serial.println(" mA");
187 Serial.print("\t- BTPDischargeSet:"); Serial.print(gauge.getBTPDischargeSet()); Serial.println(" mAh");
188 Serial.print("\t- BTPChargeSet:"); Serial.print(gauge.getBTPChargeSet()); Serial.println(" mAh");
190 BatteryStatus batteryStatus = gauge.getBatteryStatus();
191
192 Serial.println("\nOperation Status:");
193 Serial.print("\t- getIsConfigUpdateMode:"); Serial.println(status.getIsConfigUpdateMode() ? "YES" : "NO");
194 Serial.print("\t- getIsBtpThresholdExceeded:"); Serial.println(status.getIsBtpThresholdExceeded() ? "YES" : "NO");
195 Serial.print("\t- getIsCapacityAccumulationThrottled:"); Serial.println(status.getIsCapacityAccumulationThrottled() ? "YES" : "NO");
196 Serial.print("\t- getIsInitializationComplete:"); Serial.println(status.getIsInitializationComplete() ? "YES" : "NO");
197 Serial.print("\t- getIsDischargeCycleCompliant:"); Serial.println(status.getIsDischargeCycleCompliant() ? "YES" : "NO");
198 Serial.print("\t- getIsBatteryVoltageBelowEdv2:"); Serial.println(status.getIsBatteryVoltageBelowEdv2() ? "YES" : "NO");
199 Serial.print("\t- getSecurityAccessLevel:"); Serial.println(status.getSecurityAccessLevel());
200 Serial.print("\t- getIsCalibrationModeEnabled:"); Serial.println(status.getIsCalibrationModeEnabled() ? "YES" : "NO");
201
202 Serial.println("\nBattery Status:");
203 if (batteryStatus.isFullDischargeDetected()) {
204 Serial.println("\t- Full discharge detected.");
205 }
206 if (batteryStatus.isOcvMeasurementUpdateComplete()) {
207 Serial.println("\t- OCV measurement update is complete.");
208 }
209 if (batteryStatus.isOcvReadFailedDueToCurrent()) {
210 Serial.println("\t- Status bit indicating that an OCV read failed due to current.");
211 Serial.println("\tThis bit can only be set if a battery is present after receiving an OCV_CMD().");
212 }
213 if (batteryStatus.isInSleepMode()) {
214 Serial.println("\t- The device operates in SLEEP mode");
215 }
216 if (batteryStatus.isOverTemperatureDuringCharging()) {
217 Serial.println("\t- Over-temperature is detected during charging.");
218 }
219 if (batteryStatus.isOverTemperatureDuringDischarge()) {
220 Serial.println("\t- Over-temperature detected during discharge condition.");
221 }
222 if (batteryStatus.isFullChargeDetected()) {
223 Serial.println("\t- Full charge detected.");
224 }
225 if (batteryStatus.isChargeInhibited()) {
226 Serial.println("\t- Charge Inhibit: If set, indicates that charging should not begin because the Temperature() is outside the range");
227 Serial.println("\t[Charge Inhibit Temp Low, Charge Inhibit Temp High]. ");
228 }
229 if (batteryStatus.isChargingTerminationAlarm()) {
230 Serial.println("\t- Termination of charging alarm. This flag is set and cleared based on the selected SOC Flag Config A option.");
231 }
232 if (batteryStatus.isGoodOcvMeasurement()) {
233 Serial.println("\t- A good OCV measurement was made.");
234 }
235 if (batteryStatus.isBatteryInserted()) {
236 Serial.println("\t- Detects inserted battery.");
237 }
238 if (batteryStatus.isBatteryPresent()) {
239 Serial.println("\t- Battery presence detected.");
240 }
241 if (batteryStatus.isDischargeTerminationAlarm()) {
242 Serial.println("\t- Termination discharge alarm. This flag is set and cleared according to the selected SOC Flag Config A option.");
243 }
244 if (batteryStatus.isSystemShutdownRequired()) {
245 Serial.println("\t- System shutdown bit indicating that the system should be shut down. True when set. If set, the SOC_INT pin toggles once.");
246 }
247 if (batteryStatus.isInDischargeMode()) {
248 Serial.println("\t- When set, the device is in DISCHARGE mode; when cleared, the device is in CHARGING or RELAXATION mode.");
249 }
250 Serial.println("===============================================");
251
252 }
253 delay(3000);
254}
255
256
257
@license MIT License
void setup()
GaugeBQ27220 gauge
#define SENSOR_SCL
#define SENSOR_SDA
void loop()
bool isInSleepMode() const
Check if the device is in sleep mode.
bool isBatteryPresent() const
Check if a battery is present.
bool isChargeInhibited() const
Check if charging is inhibited.
bool isInDischargeMode() const
Check if the device is in discharge mode.
bool isOverTemperatureDuringCharging() const
Check if over-temperature is detected during charging.
bool isDischargeTerminationAlarm() const
Check if the discharge termination alarm is triggered.
bool isFullChargeDetected() const
Check if full charge is detected.
bool isSystemShutdownRequired() const
Check if system shutdown is required.
bool isGoodOcvMeasurement() const
Check if a good OCV measurement was made.
bool isOcvReadFailedDueToCurrent() const
Check if the OCV read failed due to current.
bool isChargingTerminationAlarm() const
Check if the charging termination alarm is triggered.
bool isOverTemperatureDuringDischarge() const
Check if over-temperature is detected during discharge.
bool isFullDischargeDetected() const
Check if full discharge is detected.
bool isOcvMeasurementUpdateComplete() const
Check if the OCV measurement update is complete.
bool isBatteryInserted() const
Check if a battery is inserted.
uint16_t getMaxLoadTimeToEmpty() const
Get the estimated time until the battery is empty at maximum load current discharge rate.
uint16_t getTimeToFull() override
Get the estimated time until the battery is fully charged.
uint16_t getBTPDischargeSet() const
Get the BTP discharge set value.
uint16_t getStandbyTimeToEmpty() const
Get the estimated time until the battery is empty at standby discharge rate.
uint16_t getAtRateTimeToEmpty() const
Get the AtRateTimeToEmpty value.
uint16_t getFullChargeCapacity() const
Get the full charge capacity of the battery.
uint16_t getStateOfHealth() const
Get the state of health value.
uint16_t getStateOfCharge() override
Get the state of charge value.
uint16_t getRawCoulombCount() const
Get the raw coulomb count value.
OperationConfig getOperationConfig()
Retrieve the operation configuration of the device.
int16_t getAveragePower() const
Get the average power value.
BatteryStatus getBatteryStatus() const
Get the battery status.
uint16_t getRemainingCapacity() const
Get the remaining battery capacity.
uint16_t getCycleCount() const
Get the cycle count value.
uint16_t getRequestChargingCurrent() const
Get the requested charging current value.
bool refresh() override
Refresh the battery data by reading from the registers.
bool begin(SensorCommCustom::CustomCallback callback, SensorCommCustomHal::CustomHalCallback hal_callback)
float getTemperature() override
Get the temperature value.
uint16_t getAtRate() const
Get the AtRate value.
uint16_t getDesignCapacity() const
Get the design capacity value.
FuelGaugeOperationStatus getOperationStatus() const
Get the operation status.
uint16_t getRequestChargingVoltage() const
Get the requested charging voltage value.
float getCurrent() override
Get the instantaneous current value.
float getInternalTemperature() const
Get the internal temperature value.
int16_t getMaxLoadCurrent() const
Get the maximum load current value.
uint16_t getVoltage() override
Get the battery voltage value.
uint16_t getTimeToEmpty() override
Get the estimated time until the battery is empty.
bool setNewCapacity(uint16_t newDesignCapacity, uint16_t newFullChargeCapacity)
Set the new design capacity and full charge capacity of the battery.
uint16_t getBTPChargeSet() const
Get the BTP charge set value.
int16_t getStandbyCurrent() const
Get the standby current value.
bool isNonRemovableSet() const
Check if the non - removable option is set.
bool isIntBremEnabled() const
Check if the INT_BREM function is enabled.
bool isIntStateEnabled() const
Check if the INT_STATE function is enabled.
bool isTempsSet() const
Check if the external thermistor is selected for temperature measurement.
uint16_t getConfigB() const
Get the value in configuration register B.
bool canEnterSleep() const
Check if the fuel gauge can enter the SLEEP state.
bool isWakeEnEnabled() const
Check if the wake - up function is enabled.
bool isDefaultSealEnabled() const
Check if the default seal option is enabled (sealed after POR).
bool isIntBatLEnabled() const
Check if the INT_BATL function is enabled.
bool isWrtempEnabled() const
Check if the write temperature function is enabled.
int getPfcCfg() const
Get the Pin Function Code (PFC) mode.
bool isBlPupEnEnabled() const
Check if the battery insertion pin pull - up is enabled.
bool isIntFocvEnabled() const
Check if the INT_FOCV function is enabled.
uint16_t getConfigA() const
Get the value in configuration register A.
bool isIntPolHigh() const
Check if the INT_POL function is enabled (high - level polarity).
bool isBienableEnabled() const
Check if battery insertion detection is enabled.
int getWkTh0() const
Get the Wake - up threshold 0 value.
bool isBatgPolHigh() const
Check the BATT_GD pin polarity.
bool isBatgEnEnabled() const
Check if the BATT_GD function is enabled.
bool isIntOtEnabled() const
Check if the INT_OT function is enabled.
int getWkTh1() const
Get the Wake - up threshold 1 value.
bool isIntOcvEnabled() const
Check if the INT_OCV function is enabled.
bool isSlpwakechgEnabled() const
Check if the slpwakechg function is enabled.