Skip to content

Instantly share code, notes, and snippets.

@juanpabloaj
Created January 10, 2021 15:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save juanpabloaj/a0a2bf750d0ff3c7dc9db73986b5d14f to your computer and use it in GitHub Desktop.
Save juanpabloaj/a0a2bf750d0ff3c7dc9db73986b5d14f to your computer and use it in GitHub Desktop.
battery_level.ino
#include <M5StickCPlus.h>
// the setup routine runs once when M5StickC starts up
void setup() {
// initialize the M5StickC object
M5.begin();
// Lcd display
M5.Lcd.fillScreen(WHITE);
delay(500);
M5.Lcd.fillScreen(RED);
delay(500);
M5.Lcd.fillScreen(GREEN);
delay(500);
M5.Lcd.fillScreen(BLUE);
delay(500);
M5.Lcd.fillScreen(BLACK);
delay(500);
}
// the loop routine runs over and over again forever
void loop(){
// text print
M5.Lcd.fillScreen(BLACK);
M5.Lcd.setCursor(0, 10);
M5.Lcd.setTextColor(WHITE);
M5.Lcd.setRotation(3);
M5.Lcd.setTextSize(4);
M5.Lcd.printf("%.0f%%", getBatteryLevel());
// wake up after 60 seconds
esp_sleep_enable_timer_wakeup(60000000);
// start deep sleep. Display goes out!
esp_deep_sleep_start();
}
double getBatteryLevel(void)
{
uint16_t vbatData = M5.Axp.GetVbatData();
double vbat = vbatData * 1.1 / 1000;
return 100.0 * ((vbat - 3.0) / (4.07 - 3.0));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment