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

    M5Stick (gray) usb power from code

    Arduino
    2
    4
    4.9k
    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 all, is it possible to know if the power comes from the usb cable from code on M5Stick (gray)?

      tnks a lot

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

        Hi @cepics I presume your asking for code to check whether the usb is inserted or not, what the current flow over usb, battery charge amount etc. You didn't specify which programming language you wanted the code in. If your using Arduino this code may help you.

        #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;
        double discharge,charge;
        double temp = 0.0;
        double bat_p = 0.0;
        double bat_p2 = 0.0;
         
        void loop() {
          vbat      = M5.Axp.GetVbatData() * 1.1 / 1000;
          charge    = M5.Axp.GetIchargeData() / 2.0 / 1000;
          discharge = M5.Axp.GetIdischargeData() / 2.0 / 1000;
          temp      =  -144.7 + M5.Axp.GetTempData() * 0.1;
          bat_p     =  M5.Axp.GetPowerbatData() * 1.1 * 0.5 /1000 /1000;
           
          M5.Lcd.setCursor(0, 0, 1);
          M5.Lcd.printf("vbat   :%.2fV\r\n",vbat);
          M5.Lcd.printf("icharge:%.2fA\r\n",charge);
          M5.Lcd.printf("idischg:%.2fA\r\n",discharge);
          M5.Lcd.printf("temp   :%3.1fC\r\n",temp);
          M5.Lcd.printf("pbat   :%.2fW\r\n",bat_p);
          M5.Lcd.printf("CoIn   :%5d\r\n",M5.Axp.GetCoulombchargeData());
          M5.Lcd.printf("CoOut  :%5d\r\n",M5.Axp.GetCoulombdischargeData());
          M5.Lcd.printf("CoD    :%.2f\r\n",M5.Axp.GetCoulombData());
          M5.Lcd.printf("          mAh\r\n");
          M5.Lcd.printf("Vin    :%.2fV\r\n",M5.Axp.GetVinData() * 1.7 /1000);
          M5.Lcd.printf("Iin    :%.2fA\r\n",M5.Axp.GetIinData() * 0.625 /1000);
          M5.Lcd.printf("Vusbin :%.2fV\r\n",GetVusbinData() * 1.7 /1000);
          M5.Lcd.printf("Iusbin :%.2fA\r\n",GetIusbinData() * 0.375 /1000);
          M5.Lcd.printf("VAPS   :%.2fV\r\n", GetVapsData()*1.4/1000);
          M5.Lcd.printf("WL     :%5d\r\n",GetWarningLeve());
          delay(1000);
        }
         
        uint16_t GetVusbinData(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 GetIusbinData(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;
         
        }
         
        uint8_t GetWarningLeve(void){
         
            uint16_t vaps = 0;
            Wire1.beginTransmission(0x34);
            Wire1.write(0x47);
            Wire1.endTransmission();
            Wire1.requestFrom(0x34, 1);
            uint8_t buf = Wire1.read();
            return (buf &amp; 0x01);
        }
        1 Reply Last reply Reply Quote 0
        • C
          cepics
          last edited by

          Yes I'm asking exactly this arduino code but for M5Stick GRAY (NOT C)

          tnks a lot..

          1 Reply Last reply Reply Quote 0
          • C
            cepics
            last edited by

            this is my solution:

            
            #include <M5Stack.h>
            #include <U8g2lib.h>
            
            uint8_t bat = M5.Power.getBatteryLevel();
            
            
            U8G2_SH1107_64X128_1_4W_HW_SPI u8g2(U8G2_R1, /* cs=*/ 14, /* dc=*/ 27, /* reset=*/ 33);
            
            void setup(void) {
            
              M5.begin();
            
              Wire.begin();
            
              u8g2.begin();
            
              Serial.begin(9600);
            }
            
            void loop(void) {
              delay(500);
              Batteria();
            }
            
            
            void Batteria() {//
              u8g2.firstPage();
              do {
                if (M5.Power.isCharging()) {
                  // DEBUG Serial.println("Battery is charging");
                  u8g2.setFont(u8g2_font_battery19_tn);
                  u8g2.drawGlyph(2, 20, 48);
                  u8g2.setFont(u8g2_font_unifont_t_symbols);
                  u8g2.drawGlyph(1, 16, 8623); //fulmine
                } else {
                  u8g2.setFont(u8g2_font_battery19_tn);
                  u8g2.drawGlyph(2, 20, 48); // VUOTA
            
                  if (bat > 50) {
                    u8g2.drawGlyph(2, 20, 49); // una tacca
                  }
                  if (bat > 100) {
                    u8g2.drawGlyph(2, 20, 50); // //due tacche
                  }
                  if (bat > 150) {
                    u8g2.drawGlyph(2, 20, 51); // tre tacche
                  }
                  if (bat > 200) {
                    u8g2.drawGlyph(2, 20, 52); // quattro tacche
                  }
                  if (bat > 250) {
                    u8g2.drawGlyph(2, 20, 53); // cinque tacche
                  }
                }
            
                /// FOR DEBUG/////
                //u8g2.setFont(u8g2_font_7Segments_26x42_mn);
                //u8g2.setCursor(20, 52);
                //u8g2.print(bat);
                //Serial.print(bat);
                //Serial.println(" %");
            
              } while ( u8g2.nextPage() );
            }
            

            right now.....

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