Skip to main content

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.

Select Board
Inkplate10_Read_Battery_Voltage.ino
#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 None

Creates 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

TypeNameDescription
uint8_tmodeINKPLATE_1BIT or INKPLATE_3BIT.
display.begin()returns void

Initializes the library and the display. Call it once, at the start of `setup()`.

ReturnsNothing.

display.setTextSize(uint8_t s)returns void

Scales text by an integer factor. The base font is 5 x 7 pixels.

ReturnsNothing.

Parameters

TypeNameDescription
uint8_tsScale factor.
display.setTextColor(uint16_t c, uint16_t bg)returns void

Sets the text color and the background color behind the text.

ReturnsNothing.

Parameters

TypeNameDescription
uint16_tcText color, for example BLACK.
uint16_tbgBackground color, for example WHITE.
display.readBattery()returns double

Reads 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 void

Clears 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 bool

Draws a one-bit-per-pixel bitmap array from memory.

Returns`true` if the bitmap drew.

Parameters

TypeNameDescription
const uint8_t *bufThe bitmap array.
intxLeft position in pixels.
intyTop position in pixels.
int16_twBitmap width in pixels.
int16_thBitmap height in pixels.
uint8_tcColor of the set pixels, for example BLACK. Defaults to 1.
uint8_tbgBackground value. Defaults to 0xFF.
display.setCursor(int16_t x, int16_t y)returns void

Sets where the next `print()` starts, in pixels from the top left corner.

ReturnsNothing.

Parameters

TypeNameDescription
int16_txHorizontal position in pixels.
int16_tyVertical position in pixels.
display.print(value)returns size_t

Prints 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

TypeNameDescription
text, number or charvalueWhat to print.
intsecond argument (optional)Decimal places for a float, or the base for an integer, for example DEC.
display.display(bool _leaveOn = 0)returns void

Does 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

TypeNameDescription
bool_leaveOnKeeps the panel supply on after the update when true. Defaults to 0.
delay(unsigned long ms)returns void

Arduino function that pauses the sketch.

ReturnsNothing.

Parameters

TypeNameDescription
unsigned longmsTime 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.