Read the battery voltage
What this example shows
Reading the voltage of a connected Li-ion or Li-Po battery with readBattery() and showing it on the display every 10 seconds.
Example code
Trimmed from the pinned example: only the file header comment is removed. The sketch includes battSymbol.h, the battery picture, from the same example folder, so open the source link below and keep both files together.
#if !defined(ARDUINO_INKPLATE10) && !defined(ARDUINO_INKPLATE10V2)
#error \
"Wrong board selection for this example, please select e-radionica Inkplate10 or Soldered Inkplate10 in the boards menu."
#endif
#include "Inkplate.h" // Include Inkplate library to the sketch
#include "battSymbol.h" // Include .h file that contains byte array for battery symbol.
// It is in same folder as this sketch. You can even open it (read it) by clicking on battSymbol.h tab in Arduino IDE
Inkplate display(INKPLATE_1BIT); // Create an object on Inkplate library and also set library into 1-bit mode (BW)
void setup()
{
display.begin(); // Init Inkplate library (you should call this function ONLY ONCE)
display.setTextSize(2); // Scale text to be two times bigger then original (5x7 px)
display.setTextColor(BLACK, WHITE); // Set text color to black and background color to white
}
void loop()
{
float voltage = display.readBattery(); // Read battery voltage
display.clearDisplay(); // Clear everything in frame buffer of e-paper display
display.image.draw(battSymbol, 100, 100, 106, 45, BLACK); // Draw battery symbol at position X=100 Y=100
display.setCursor(210, 120);
display.print(voltage, 2); // Print battery voltage
display.print('V');
display.display(); // Send everything to display (refresh the screen)
delay(10000); // Wait 10 seconds before new measurement
}
Functions used
Inkplate(uint8_t mode)returns NoneCreates the display object. `INKPLATE_1BIT` is black and white and supports partial updates. `INKPLATE_3BIT` gives 8 levels of grey but only full updates.
ReturnsConstructor; no return value.
Parameters
| Type | Name | Description |
|---|---|---|
uint8_t | mode | INKPLATE_1BIT or INKPLATE_3BIT. |
display.begin()returns voidInitializes the library and the display. Call it once, at the start of `setup()`.
ReturnsNothing.
display.setTextSize(uint8_t s)returns voidScales text by an integer factor. The base font is 5 x 7 pixels.
ReturnsNothing.
Parameters
| Type | Name | Description |
|---|---|---|
uint8_t | s | Scale factor. |
display.setTextColor(uint16_t c, uint16_t bg)returns voidSets the text color and the background color behind the text.
ReturnsNothing.
Parameters
| Type | Name | Description |
|---|---|---|
uint16_t | c | Text color, for example BLACK. |
uint16_t | bg | Background color, for example WHITE. |
display.readBattery()returns doubleReads the battery voltage. The function switches on the battery measurement path, takes an ADC reading through a voltage divider that halves the voltage, and switches the path off again.
ReturnsBattery voltage in volts.
display.clearDisplay()returns voidClears the frame buffer in memory. It does not change what the panel shows until you call `display()`.
ReturnsNothing.
display.image.draw(const uint8_t *buf, int x, int y, int16_t w, int16_t h, uint8_t c = 1, uint8_t bg = 0xFF)returns boolDraws a one-bit-per-pixel bitmap array from memory.
Returns`true` if the bitmap drew.
Parameters
| Type | Name | Description |
|---|---|---|
const uint8_t * | buf | The bitmap array. |
int | x | Left position in pixels. |
int | y | Top position in pixels. |
int16_t | w | Bitmap width in pixels. |
int16_t | h | Bitmap height in pixels. |
uint8_t | c | Color of the set pixels, for example BLACK. Defaults to 1. |
uint8_t | bg | Background value. Defaults to 0xFF. |
display.setCursor(int16_t x, int16_t y)returns voidSets where the next `print()` starts, in pixels from the top left corner.
ReturnsNothing.
Parameters
| Type | Name | Description |
|---|---|---|
int16_t | x | Horizontal position in pixels. |
int16_t | y | Vertical position in pixels. |
display.print(value)returns size_tPrints text or a number at the cursor. The `print` functions come from the Arduino `Print` class that `Adafruit_GFX` builds on. For a number, an optional second argument sets the decimal places of a float or the number base of an integer.
ReturnsNumber of bytes printed.
Parameters
| Type | Name | Description |
|---|---|---|
text, number or char | value | What to print. |
int | second argument (optional) | Decimal places for a float, or the base for an integer, for example DEC. |
display.display(bool _leaveOn = 0)returns voidDoes a full refresh: sends the frame buffer to the panel. If `_leaveOn` is true, the panel supply stays on after the refresh, which saves time on the next update.
ReturnsNothing.
Parameters
| Type | Name | Description |
|---|---|---|
bool | _leaveOn | Keeps the panel supply on after the update when true. Defaults to 0. |
delay(unsigned long ms)returns voidArduino function that pauses the sketch.
ReturnsNothing.
Parameters
| Type | Name | Description |
|---|---|---|
unsigned long | ms | Time to wait in milliseconds. |
What to expect
The display shows a battery symbol with the measured voltage next to it, with two decimals and a V. The reading is refreshed every 10 seconds. The example header says the reading depends on battery condition and load.
Check the polarity of the battery JST connector before you plug a battery in. The library README warns that some batteries sold online have reversed polarity, which can damage the Inkplate board. A pouch battery from soldered.com and an Inkplate with a built-in battery are safe.
The library README lists standard Li-ion or Li-Po batteries with a 3.7 V nominal voltage as supported, and says the connector is a 2.00 mm pitch JST connector. The example header names a 3.6 to 4.2 V battery. With USB connected at the same time, the onboard charger charges the battery at 400 mA. The README also says a CR2032 cell is only for RTC backup and cannot power the board.