Skip to content

Instantly share code, notes, and snippets.

Created Apr 10, 2021
What would you like to do?
Read battery voltage on TTGO Lora32
#include <Arduino.h>
#include <Wire.h> // Only needed for Arduino 1.6.5 and earlier
const uint8_t blue = 23;
const uint8_t vbatPin = 35;
float VBAT; // battery voltage from ESP32 ADC read
void setup()
pinMode(blue, OUTPUT);
pinMode(vbatPin, INPUT);
void loop()
// Battery Voltage
VBAT = (float)(analogRead(vbatPin)) / 4095*2*3.3*1.1;
The ADC value is a 12-bit number, so the maximum value is 4095 (counting from 0).
To convert the ADC integer value to a real voltage you’ll need to divide it by the maximum value of 4095,
then double it (note above that Adafruit halves the voltage), then multiply that by the reference voltage of the ESP32 which
is 3.3V and then vinally, multiply that again by the ADC Reference Voltage of 1100mV.
Serial.println("Vbat = "); Serial.print(VBAT); Serial.println(" Volts\n");
; PlatformIO Project Configuration File
; Build options: build flags, source filter
; Upload options: custom upload port, speed and extra flags
; Library options: dependencies, extra library storages
; Advanced options: extra scripting
; Please visit documentation for the other options and examples
platform = espressif32
board = ttgo-lora32-v21
framework = arduino
lib_deps =
adafruit/Adafruit GFX Library@^1.10.7
adafruit/Adafruit BusIO@^1.7.3
adafruit/Adafruit SSD1306@^2.4.4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment