Skip to main content

Overview

Battery_charge This guide will walk you through configuring the GLYPH-C3 board to measure the voltage and charge percentage of a Li-ion or Li-Polymer(LiPo) battery. You will learn how to set up the board, connect the battery, and display the voltage readings on the Serial Monitor.

Voltage Measurement

Battery voltage measurement is the process of determining the electrical potential difference between the positive and negative terminals of a battery. It provides a quantitative value in volts (V), which indicates the charge level and health of the battery. The voltage range of a battery depends on its chemistry (Li-ion, LiPo, NiMH, Lead-Acid, etc.). The correct minimum (empty) and maximum (full) voltage values must be set to measure the charge percentage accurately. You need to update the Battery object with the correct min and max voltages. Monitor Battery Health – Helps determine if the battery is functioning properly and whether it needs replacement. Prevent Overcharging & Deep Discharging – Ensures the battery is used within safe voltage limits to prolong its lifespan. Estimate Remaining Charge – Provides an approximate percentage of battery capacity remaining. Optimize Performance – Ensures the battery supplies the correct voltage for efficient device operation. Safety – Prevents excessive discharge or overvoltage conditions that may cause damage or overheating.

Step 1: Hardware Required

  1. Glyph Board
  2. Li-ion or Li-Polymer(LiPo) battery

Step 2: Circuit Diagram

Circuit Diagram with Lithium Ion Battery:

pcbcupid_liion_battery_circuitdiagram

Circuit Diagram with Lithium Polymer Battery:

pcbcupid_lipolymer_battery_circuitdiagram

Step 3: Connect the Battery

  • First Short the MSR Pin at the back of the Glyph C3 board
  • Solder the Positive and Negative leads of the battery to the leads on the back of Glyph C3 which says BAT+ and BAT-
  • Connect the positive terminal of the battery to the analog input pin on the board.
  • Connect the negative terminal of the battery to the ground pin on the board.
pcbcupid-smowcode-window-setup
pcbcupid-smowcode-window-setup


Step 4: Code Setup

  1. Open Arduino IDE.
  2. Install the Battery Sense library by going to Sketch > Include Library > Manage Libraries and searching for Battery Sense
  3. Copy and paste the following code into the Arduino IDE:

ADC Calibration and Real-World Accuracy

One important thing to understand is that ESP32 ADC readings are not always perfectly accurate. In real hardware, ADC readings can vary slightly due to:
  • ADC non-linearity
  • Resistor tolerances
  • Board variations
  • Noise
Because of this, a calibration factor is often used to fine tune the measured voltage. Example:
 ⁠ This calibration factor compensates for ADC measurement error and helps produce more accurate battery readings. This is a very common practice in embedded systems and battery-powered devices.

Why Future Revisions May Use Dedicated Fuel Gauge ICs

While ADC-based battery measurement works well, it still depends on calibration and voltage estimation. Future revisions of the GLYPH boards may use dedicated LiPo fuel gauge ICs over I2C for:
  • Improved Accuracy
  • Better Battery Estimation
  • More Stable Readings
These dedicated chips are specifically designed for battery monitoring applications. Here is the sample code with ADC Calibration

Step 5: Upload the Code

  1. Connect the Board
  • Connect your GLYPH board to your computer
  1. Select the Board and Port Do the following settings in your Arduino IDE, Do the following settings in your Arduino IDE,
    • Tools > Board > esp32 > Pcbcupid GLYPH C3
For the Pcbcupid GLYPH C3 to appear under Tools > Board > esp32, the esp32 board version installed in the Arduino IDE should be greater than or equal to 3.1.0.
  • Tools > Port and select the port connected to your GLYPH.
  • Tools > USB CDC on Boot > Enabled
If USB CDC on BOOT not enabled, you won’t be seeing any serial data on Arduino IDE.
  1. Upload the Code
  • Click the upload button (➡️ icon) or use the shortcut CTRL + U in Arduino IDE to upload the code to the board. Note: Ensure that the voltage applied to the analog pin does not exceed the maximum allowable voltage (usually 3.3V for the ESP32).

Step 6: Observe the Output

  1. See the LED Light up - After soldering the battery leads to the BAT+ & BAT- terminals and connecting your Glyph board to your PC, you can see the onboard led of your Glyph board light up like this:
pcbcupid_bh1750adafruit pcbcupid_bh1750adafruit
  1. Open Serial Monitor
    • In Arduino IDE, go toTools > Serial Monitor or pressCtrl+Shift+M to open the Serial Monitor.
  2. View Battery Readings
    • You should see the current battery voltage & battery charge percentage every second on the Serial Monitor.
Battery_charge