Skip to content

Instantly share code, notes, and snippets.

@jenschr
Created August 10, 2019 14:03
Show Gist options
  • Save jenschr/dfc765cb9404beb6333a8ea30d2e78a1 to your computer and use it in GitHub Desktop.
Save jenschr/dfc765cb9404beb6333a8ea30d2e78a1 to your computer and use it in GitHub Desktop.
Battery monitor example for the Banggood version of TTGO LoRa32
const uint8_t blue = 2;
const uint8_t vbatPin = 35;
#include <Wire.h> // Only needed for Arduino 1.6.5 and earlier
#include "SSD1306.h" // alias for `#include "SSD1306Wire.h"`
float VBAT; // battery voltage from ESP32 ADC read
SSD1306 display(0x3c, 4, 15);
void setup()
{
Serial.begin(115200);
pinMode(blue, OUTPUT);
pinMode(vbatPin, INPUT);
pinMode (16, OUTPUT);
digitalWrite (16, LOW); // set GPIO16 low to reset OLED
delay (50);
digitalWrite (16, HIGH); // while OLED is running, GPIO16 must go high
display.init();
display.flipScreenVertically();
display.setFont(ArialMT_Plain_10);
}
char string[25];
void drawFontFaceDemo(float Vbat) {
// Font Demo1
// create more fonts at http://oleddisplay.squix.ch/
display.setTextAlignment(TEXT_ALIGN_LEFT);
display.setFont(ArialMT_Plain_10);
display.drawString(0, 0, "Battery");
display.setFont(ArialMT_Plain_16);
display.drawString(0, 10, "Monitoring");
display.setFont(ArialMT_Plain_24);
itoa(Vbat,string,10);
sprintf(string,"%7.5f",Vbat);
display.drawString(0, 26, string);
}
void loop()
{
digitalWrite(blue, 1);
delay(100);
digitalWrite(blue, 0);
delay(100);
digitalWrite(blue, 1);
delay(100);
// 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");
display.clear();
drawFontFaceDemo(VBAT);
display.display();
digitalWrite(blue, 0);
delay(700);
}
@jenschr
Copy link
Author

jenschr commented Sep 8, 2020

Comment any line that says "display"-and then something and it should work? Then again - you wouldn't see the result :-P
The code here isn't too complicated. Read it line by line and see what you'll need to make it work without a screen?

@lblackZl
Copy link

lblackZl commented Sep 8, 2020

I comment out any line. But in Serial Monitor VBAT shows 0.0. :(
Maybe my TTGO Lora32 V1.0 doesnt have a internal divider or battery pin?

Oh shit... After reading line by line...
Maybe I've forgotten pinMode INPUT 😆
Need to check this tomorrow...

@jenschr
Copy link
Author

jenschr commented Sep 8, 2020

Could be, but I'd guess Gihub Gist isn't the place to discuss stuff like this? It's primarily a way to share tiny snippets ;-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment