02 - Battery Level Indicator

![](https://cdn.shopify.com/s/files/1/0331/9994/7908/files/Pasted_image_20260117090156.png?v=1768608461) On the expansion board, you can enable battery voltage monitoring by **shorting (bridging/soldering)** the **battery monitor pad** (often labeled as a jumper or test point). - This connects the battery voltage to **GPIO IO2** through a **voltage divider** made of **two equal-value resistors**. - The divided voltage appears on **IO2**, which is an analog-capable pin (ADC). This feature lets you monitor the current battery level in your portable projects — great for low-battery warnings or percentage display! ``` cpp #include <U8g2lib.h> #include <Wire.h> #define SCK 6 #define SDA 5 U8G2_SSD1306_72X40_ER_F_SW_I2C u8g2(U8G2_R0, SCK, SDA); // ADC pin for voltage divider const int adcPin = 2; // Calibration settings const float refVoltage = 3.3;  // Measure your ESP32's 3.3V pin and adjust if needed const float calibrationFactor = 0.911;  // Based on 3.68V actual / 4.04V measured const float dividerRatio = 2.0;  // For equal 470kΩ resistors; adjust if measured differently const int numSamples = 10;  // Number of ADC samples to average void setup() {   Serial.begin(115200);   u8g2.begin();   // Set ADC attenuation to 11dB (0–3.3V range)   analogSetAttenuation(ADC_11db); } void loop() {   // Average ADC readings to reduce noise   long sum = 0;   for (int i = 0; i < numSamples; i++) {     sum += analogRead(adcPin);     delay(10);  // Short delay between samples   }   int adcValue = sum / numSamples;   // Calculate voltages   float vOut = adcValue * (refVoltage / 4095.0);  // Voltage at ADC pin   float vBattery = (vOut * dividerRatio) * calibrationFactor;  // Apply divider and calibration   // Format voltage string (e.g., "3.68")   char buffer[10];   dtostrf(vBattery, 4, 2, buffer);   // Display on OLED   u8g2.clearBuffer();   u8g2.setFont(u8g2_font_ncenB08_tr);   u8g2.drawStr(0, 20, "Battery:");   u8g2.drawStr(50, 20, buffer);   u8g2.drawStr(80, 20, "V");   u8g2.sendBuffer();   // Serial debugging   Serial.print("ADC Avg: ");   Serial.print(adcValue);   Serial.print(" | Vout: ");   Serial.print(vOut);   Serial.print("V | Vbattery (calib): ");   Serial.print(vBattery);   Serial.println("V");![](https://cdn.shopify.com/s/files/1/0331/9994/7908/files/Pasted_image_20260117090137.png?v=1768608465)     delay(1000); } ```