Skip to content

Instantly share code, notes, and snippets.

@4DA
Created April 10, 2021 11:28
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save 4DA/f4fffd6538ba0878477740f928da9047 to your computer and use it in GitHub Desktop.
Save 4DA/f4fffd6538ba0878477740f928da9047 to your computer and use it in GitHub Desktop.
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()
{
Serial.begin(9600);
pinMode(blue, OUTPUT);
pinMode(vbatPin, INPUT);
}
void loop()
{
delay(1000);
// 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
; https://docs.platformio.org/page/projectconf.html
[env:ttgo-lora32-v21]
platform = espressif32
board = ttgo-lora32-v21
framework = arduino
lib_deps =
adafruit/Adafruit GFX Library@^1.10.7
SPI
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