🤖Have you ever tried Chat.M5Stack.com before asking??😎
    M5Stack Community
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Register
    • Login

    Battery level

    M5 Stick/StickC
    5
    7
    12.3k
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • C
      cepics
      last edited by

      Hi, does this sketch work with M5Stick too?

      
      // https://github.com/m5stack/M5Stack/issues/74
      
      
      #include <M5Stack.h>
      
      int8_t getBatteryLevel()
      {
        Wire.beginTransmission(0x75);
        Wire.write(0x78);
        if (Wire.endTransmission(false) == 0
         && Wire.requestFrom(0x75, 1)) {
          switch (Wire.read() & 0xF0) {
          case 0xE0: return 25;
          case 0xC0: return 50;
          case 0x80: return 75;
          case 0x00: return 100;
          default: return 0;
          }
        }
        return -1;
      }
      
      void setup() {
          M5.begin();
          Wire.begin();
        }
      
      void loop() {
        delay(1000);
        M5.Lcd.clear();
        M5.Lcd.setTextColor(GREEN);
        M5.Lcd.setCursor(0, 20);
        M5.Lcd.setTextSize(2);
        M5.Lcd.print("Battery Level: ");
        M5.Lcd.print(getBatteryLevel());
        M5.Lcd.print("%");
      }
      
      

      tnks a lot?

      1 Reply Last reply Reply Quote 0
      • world101W
        world101
        last edited by

        I didn’t test it, but probably not likely to work. The M5Stack and m5stickC use different power management chips (IP5306_i2c vs. AXP192). This sketch might help you get some power info from the m5stickC though.

        https://github.com/m5stack/M5StickC/blob/master/examples/Basics/AXP192/AXP192.ino

        1 Reply Last reply Reply Quote 0
        • m5-docsM
          m5-docs
          last edited by

          You can try this code for testing battery voltage level.

          #include <M5Stack.h>
          #include <U8x8lib.h>
          
          int8_t getBatteryLevel()
          {
            Wire.beginTransmission(0x75);
            Wire.write(0x78);
            if (Wire.endTransmission(false) == 0
             && Wire.requestFrom(0x75, 1)) {
              switch (Wire.read() & 0xF0) {
              case 0xE0: return 25;
              case 0xC0: return 50;
              case 0x80: return 75;
              case 0x00: return 100;
              default: return 0;
              }
            }
            return -1;
          }
          
          void setup() {
              M5.begin();
              Wire.begin();
            }
          
          void loop() {
            delay(200);
            Serial.print("Battery Level: ");
            Serial.print(getBatteryLevel());
            Serial.println("%");
          }
          
          

          M5Stack documentation URL

          https://docs.m5stack.com

          C 1 Reply Last reply Reply Quote 0
          • C
            cepics @m5-docs
            last edited by

            @m5-docs coool it works!!

            but, when I plug the usb cable the serial monitor say 100%,
            though the battery level is less..

            is there a software way to know if the usb cable is connected? (to make power cable symbol appear on display)

            1 Reply Last reply Reply Quote 0
            • lukasmaximusL
              lukasmaximus
              last edited by

              Theres a pretty nice comprehensive blog post on the battery and output of the stick c but in japanese https://lang-ship.com/blog/?p=523. He posts his arduino code though on how to read different information about the battery. I tested and it works pretty well.

              #include <M5StickC.h>
               
              void setup() {
                // put your setup code here, to run once:
                M5.begin();
                M5.Lcd.fillScreen(BLACK);
                 
                M5.Axp.EnableCoulombcounter();
              }
              double vbat = 0.0;
              int discharge,charge;
              double temp = 0.0;
              double bat_p = 0.0;
              double bat_p2 = 0.0;
               
              void loop() {
                M5.Lcd.fillScreen(BLACK);
                vbat      = M5.Axp.GetVbatData() * 1.1 / 1000;
                charge    = M5.Axp.GetIchargeData() / 2;
                discharge = M5.Axp.GetIdischargeData() / 2;
                temp      =  -144.7 + M5.Axp.GetTempData() * 0.1;
                bat_p     =  M5.Axp.GetPowerbatData() * 1.1 * 0.5 /1000;
                 
                M5.Lcd.setCursor(0, 0, 1);
                M5.Lcd.printf("vbat:%.3fV\r\n",vbat);
                M5.Lcd.printf("icharge:%dmA\r\n",charge);
                M5.Lcd.printf("idischg:%dmA\r\n",discharge);
                M5.Lcd.printf("temp:%.1fC\r\n",temp);
                M5.Lcd.printf("pbat:%.3fmW\r\n",bat_p);
                M5.Lcd.printf("CoIn :%d\r\n",M5.Axp.GetCoulombchargeData());
                M5.Lcd.printf("CoOut:%d\r\n",M5.Axp.GetCoulombdischargeData());
                M5.Lcd.printf("CoD:%.2fmAh\r\n",M5.Axp.GetCoulombData());
                M5.Lcd.printf("Vin:%.3fmV\r\n",M5.Axp.GetVinData() * 1.7);
                M5.Lcd.printf("Iin:%.3fmA\r\n",M5.Axp.GetIinData() * 0.625);
                M5.Lcd.printf("VBin:%.3fmV\r\n",GetVBinData() * 1.7);
                M5.Lcd.printf("IBin:%.3fmA\r\n",GetIBinData() * 0.375);
                M5.Lcd.printf("VAPS:%.3f\r\n", GetVapsData()*1.4/1000);
                delay(1000);
              }
               
              uint16_t GetVBinData(void){
               
                  uint16_t vin = 0;
               
                  Wire1.beginTransmission(0x34);
                  Wire1.write(0x5a);
                  Wire1.endTransmission();
                  Wire1.requestFrom(0x34, 1);
                  uint8_t buf = Wire1.read();
               
                  Wire1.beginTransmission(0x34);
                  Wire1.write(0x5b);
                  Wire1.endTransmission();
                  Wire1.requestFrom(0x34, 1);
                  uint8_t buf2 = Wire1.read();
               
                  vin = ((buf << 4) + buf2); // V
                  return vin;
               
              }
               
              uint16_t GetIBinData(void){
               
                  uint16_t iin = 0;
               
                  Wire1.beginTransmission(0x34);
                  Wire1.write(0x5c);
                  Wire1.endTransmission();
                  Wire1.requestFrom(0x34, 1);
                  uint8_t buf = Wire1.read();
               
                  Wire1.beginTransmission(0x34);
                  Wire1.write(0x5d);
                  Wire1.endTransmission();
                  Wire1.requestFrom(0x34, 1);
                  uint8_t buf2 = Wire1.read();
               
                  iin = ((buf << 4) + buf2); // V
                  return iin;
               
              }
               
              uint16_t GetVapsData(void){
               
                  uint16_t vaps = 0;
                  Wire1.beginTransmission(0x34);
                  Wire1.write(0x7E);
                  Wire1.endTransmission();
                  Wire1.requestFrom(0x34, 1);
                  uint8_t buf = Wire1.read();
               
                  Wire1.beginTransmission(0x34);
                  Wire1.write(0x7F);
                  Wire1.endTransmission();
                  Wire1.requestFrom(0x34, 1);
                  uint8_t buf2 = Wire1.read();
                   
                  vaps = ((uint16_t)(buf << 4) + buf2);
                  return vaps;
               
              }
              1 Reply Last reply Reply Quote 1
              • C
                cepics
                last edited by

                does it should work on GRAY M5Stick too ?

                tnks

                ajb2k3A 1 Reply Last reply Reply Quote 0
                • ajb2k3A
                  ajb2k3 @cepics
                  last edited by

                  @cepics said in Battery level:

                  does it should work on GRAY M5Stick too ?

                  tnks

                  I don't think so but i could be mistaken.

                  UIFlow, so easy an adult can learn it!
                  If I don't know it, be patient!
                  I've ether not learned it or am too drunk to remember it!
                  Author of the WIP UIFlow Handbook!
                  M5Black, Go, Stick, Core2, and so much more it cant be fit in here!

                  1 Reply Last reply Reply Quote 0
                  • First post
                    Last post