Skip to content

Commit 3da096c

Browse files
committed
added batt level indicator to example 8
-utilizing the on-board MAX17048 Fuel Guage IC on the ESP32 Thing Plus C. -Printing battery state of charge (SOC) to terminal -drawing a battery level indicator on the bottom right of the OLED - with 3 blocks to show 33/66/100 % batt levels.
1 parent 890529b commit 3da096c

File tree

1 file changed

+110
-0
lines changed

1 file changed

+110
-0
lines changed

examples/Example_08_Demo_Oled/Example_08_Demo_Oled.ino

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,12 @@ int height;
8484
// writeStaticDisplayItems() function
8585
int xPosPM25;
8686

87+
// Fuel Guage Specifics
88+
#include <SparkFun_MAX1704x_Fuel_Gauge_Arduino_Library.h> // Click here to get the library: http://librarymanager/All#SparkFun_MAX1704x_Fuel_Gauge_Arduino_Library
89+
SFE_MAX1704X lipo(MAX1704X_MAX17048); // Create a MAX17048
90+
double soc = 0; // Variable to keep track of LiPo state-of-charge (SOC)
91+
92+
8793
// Some Dev boards have their QWIIC connector on Wire or Wire1
8894
// This #ifdef will help this sketch work across more products
8995

@@ -98,6 +104,8 @@ void setup()
98104
pinMode(StatLedPin, OUTPUT);
99105
digitalWrite(StatLedPin, LOW);
100106

107+
lipo.disableDebugging(); // disable debugging for the MAX1704X fuel gauge
108+
101109
Serial.begin(115200);
102110

103111
while (!Serial)
@@ -156,6 +164,20 @@ void setup()
156164
Serial.println("Error setting BMV080 mode");
157165
writeCenteredStringToDisplay("BMV080 Mode Error");
158166
}
167+
168+
// Set up the MAX17048 LiPo fuel gauge:
169+
if (lipo.begin() == false) // Connect to the MAX17043 using the default wire port
170+
{
171+
Serial.println(F("MAX17048 not detected. Please check wiring. Freezing."));
172+
writeCenteredStringToDisplay("MAX17048 Failure");
173+
while (1)
174+
;
175+
}
176+
Serial.println("MAX17048 found!");
177+
178+
// Quick start restarts the MAX17043 in hopes of getting a more accurate
179+
// guess for the SOC.
180+
lipo.quickStart();
159181
}
160182

161183
void loop()
@@ -177,6 +199,12 @@ void loop()
177199
Serial.print(pm25Value);
178200
writeStaticDisplayItems();
179201
writeValuesToDisplay();
202+
// lipo.getSOC() returns the estimated state of charge (e.g. 79%)
203+
soc = lipo.getSOC();
204+
Serial.print("\tBatt: ");
205+
Serial.print(soc); // Print the battery state of charge
206+
Serial.print(" %");
207+
writeBatteryLevelToDisplay();
180208
myOLED.display(); // actually command the display to show the scene
181209
}
182210

@@ -310,4 +338,86 @@ void toggleHeartbeat()
310338
}
311339

312340
myOLED.display();
341+
}
342+
343+
// Write the battery level to the display
344+
// Create a rectangular battery level indicator on the bottom right side of the screen
345+
// fill in the rectangle with a percentage of the battery level
346+
// each section of the rectangle represents 25% of the battery level
347+
void writeBatteryLevelToDisplay()
348+
{
349+
// calculate the x position of the battery level indicator
350+
// this is the right side of the screen minus 14 pixels
351+
int xPosBatteryLevelIndicator = (myOLED.getWidth()- 14);
352+
353+
// draw the battery level indicator outline
354+
// this rectangle is an outline of the battery level indicator
355+
// it looks like a small battery
356+
// it is 6 pixels tall and 12 pixels wide
357+
// the inside of the rectangle is not filled in
358+
// the rectangle is drawn in white
359+
// the inside of the rectangle will be filled in with the battery level
360+
myOLED.rectangle(xPosBatteryLevelIndicator, myOLED.getHeight() - 6, 12, 6, 1);
361+
362+
// draw the end shape of the battery level indicator
363+
// this makes it look like a traditional battery level indicator
364+
// like the end of a AA battery
365+
// draw a filled in rectangle that will be the end shape of the battery level indicator
366+
// it is 2 pixels tall and 2 pixels wide
367+
// it is drawn in white
368+
myOLED.rectangle(xPosBatteryLevelIndicator + 12, myOLED.getHeight() - 4, 2, 2, 1);
369+
370+
// draw the inner sections of the battery level indicator
371+
// There are 3 blocks inside the battery level indicator
372+
// each block represents 33% of the battery level
373+
// the left most block represents 0-33% of the battery level
374+
// the middle block represents 34-66% of the battery level
375+
// the right most block represents 67-100% of the battery level
376+
// the blocks are filled in with white
377+
// the blocks are 2 pixels tall and 2 pixels wide
378+
// there is a 1 pixel gap between each block
379+
380+
// calculate the y position of the battery level indicator blocks
381+
// this is the bottom of the screen minus 4 pixels
382+
int yPosBatteryLevelBlocks = myOLED.getHeight() - 4;
383+
384+
// calculate the x position of the left most block
385+
// this is the right side of the screen minus 12 pixels
386+
int xPosLeftBlock = (myOLED.getWidth() - 12);
387+
388+
// calculate the x position of the middle block
389+
// this is the right side of the screen minus 9 pixels
390+
int xPosMiddleBlock = (myOLED.getWidth() - 9);
391+
392+
// calculate the x position of the right most block
393+
// this is the right side of the screen minus 6 pixels
394+
int xPosRightBlock = (myOLED.getWidth() - 6);
395+
396+
// write all 3 battery block indicators as black rectangles
397+
myOLED.rectangle(xPosLeftBlock, yPosBatteryLevelBlocks, 2, 2, 0);
398+
myOLED.rectangle(xPosMiddleBlock, yPosBatteryLevelBlocks, 2, 2, 0);
399+
myOLED.rectangle(xPosRightBlock, yPosBatteryLevelBlocks, 2, 2, 0);
400+
401+
// if the battery level is at least 33%
402+
// fill in the left most block
403+
if (soc > 32)
404+
{
405+
myOLED.rectangle(xPosLeftBlock, yPosBatteryLevelBlocks, 2, 2, 1);
406+
}
407+
408+
// if the battery level is between 34% and 66%
409+
// fill in the middle block
410+
if (soc > 33)
411+
{
412+
myOLED.rectangle(xPosMiddleBlock, yPosBatteryLevelBlocks, 2, 2, 1);
413+
}
414+
415+
// if the battery level is greater than 66%
416+
// fill in the right most block
417+
if (soc > 66)
418+
{
419+
myOLED.rectangle(xPosRightBlock, yPosBatteryLevelBlocks, 2, 2, 1);
420+
}
421+
422+
313423
}

0 commit comments

Comments
 (0)