M5Stack Basic accu level

Place code snippets and demo code here
Post Reply
Jan Volk
Posts: 71
Joined: Wed Mar 03, 2021 1:35 pm
Been thanked: 23 times

M5Stack Basic accu level

Post by Jan Volk »

The M5Stack Basic contains a rechargeable battery which can be read via the i2c bus.
The standard battery is not that powerful, so an extra module with a battery of 750mAh has been installed.
The problem is that the battery status is not directly visible on the screen. I came across some routines here on the forum to read out the IP5306. Adjusted it a bit and found out that there is another level to read the battery level.
There is 100%, 75%, 50%, 25% and also a 3.2V level in the registers.
By now attaching a 10% level to the 3.2V it is a lot clearer and then also displaying the loading and full level on the top line and the time and ip address it is complete.
If you want to read it even more accurately, you can determine the percentage very precisely with an i2c adc ADS1115 and mount the module on a separate BUS module.
If the IP address does not fit exactly on the top line, the line can be shortened somewhat.

I also found out that a separate I2C RTC ZS-042 module and an RTC DS3231 RPI module affect the IP5306 on / off push button, making the M5Stack difficult to turn on or off. Disconnecting the RTC module was the solution. But a good result with the modification of the RPI module where I removed the battery. (design error) Then a separate normal CR2032 cell with holder placed and only the GND connected to the board. Then the pin14 (Vbat) soldered loose from the board and directly to the plus 3V cell.
Note only connect GND, SCL, SDA.

Battery Loading Charge
4.2V F100% Bit=Full (Register=&h71)
4.1V L100% Bit=4.2V (Register=&h22)
4.0V L75 % 100%
3.9V L75 % 75 %
3.8V L50 % 75 %
3.7V L50 % 50 %
3.6V L25 % 25 % If battery is charging Start. (Register=&h70)
3.5V 25 %
3.4V 25 %
3.3V 25 %
3.2V 10 % Bit=3.2V for safe BAS.RTCMEM$ (Register=&h78)
3.1V 10 %
3.0V 0 % Bit=3.0V Shutdown. (Register=&h78)

Below the routine that can be included in a program by default.

BLUETOOTH.DELETE ' version 1.43.3
i2c.setup 21, 22
ip5306_addr = &h75
ret_value = -1
COL_BACKGROUND = tft.color(blue)
COL_TEXT = tft.color(white)
TFT.BRIGHTNESS 40 ' Brightness 0 - 255 0 = dark
TFT.INIT 1
TFT.FILL blue
TFT.TEXT.COL COL_TEXT, COL_BACKGROUND

timer0 5000, rule
wait
rule:
' TFT M5Stack Basic first rule
tft.text.size 2
tft.text.pos 3, 1: tft.print time$
tft.text.pos 66, 1: tft.print ""
tft.text.pos 70, 1: tft.print "IP"; word$(ip$, 1)
ip5306_battery_level ip5306_addr, ret_value
tft.text.pos 295, 1: tft.print "%"
tft.text.pos 271, 1: tft.print str$(ret_value)
ip5306_isCharging ip5306_addr, ret_value ' L = charging Loading
if ret_value = 1 then
tft.text.pos 260, 1: tft.print "L"
end if
ip5306_isChargeFull ip5306_addr, ret_value ' F = charging Full
if ret_value = 1 then
tft.text.pos 260, 1: tft.print "F"
end if
return

' ** IP5306 lipo battery controller via i2c status registers **
sub ip5306_battery_level(addr, ret) ' Return percentage of battery charge
value = i2c.readregbyte(addr, &h78)
if value = &h00 then ret = 100
if value = &h80 then ret = 75
if value = &hC0 then ret = 50
if value = &hE0 then ret = 25
if value = &hF0 then ret = 10
' Register = &h78 7=75% 6=50% 5=25% 4=3.2V 1=3.0V(Shutdown)
end sub

sub ip5306_isCharging(addr, ret) ' Return 1 if battery is charging, 0 if not
value = i2c.readregbyte(addr, &h70)
ret = value >> 7
end sub

sub ip5306_isChargeFull(addr, ret) ' Return 1 if battery if full charged, 0 if not
value = i2c.readregbyte(addr, &h71)
ret = value >> 7
end sub
end
Jan Volk
Posts: 71
Joined: Wed Mar 03, 2021 1:35 pm
Been thanked: 23 times

Re: M5Stack Basic accu level

Post by Jan Volk »

BLUETOOTH.DELETE ' version 1.43.3
i2c.setup 21, 22
ip5306_addr = &h75
ret_value = -1
COL_BACKGROUND = tft.color(blue)
COL_TEXT = tft.color(white)
TFT.BRIGHTNESS 40 ' Brightness 0 - 255 0 = dark
TFT.INIT 1
TFT.FILL blue
TFT.TEXT.COL COL_TEXT, COL_BACKGROUND

timer0 5000, rule
wait
rule:
' TFT M5Stack Basic first rule
tft.text.size 2
tft.text.pos 3, 1: tft.print time$
tft.text.pos 66, 1: tft.print " "
tft.text.pos 70, 1: tft.print "IP"; word$(ip$, 1)
ip5306_battery_level ip5306_addr, ret_value
tft.text.pos 295, 3 : tft.print " %"
tft.text.pos 271, 1: tft.print str$(ret_value)
ip5306_isCharging ip5306_addr, ret_value ' L = charging Loading
if ret_value = 1 then
tft.text.pos 260, 1: tft.print "L"
end if
ip5306_isChargeFull ip5306_addr, ret_value ' F = charging Full
if ret_value = 1 then
tft.text.pos 260, 1: tft.print "F"
end if
return

' ** IP5306 lipo battery controller via i2c status registers **
sub ip5306_battery_level(addr, ret) ' Return percentage of battery charge
value = i2c.readregbyte(addr, &h78)
if value = &h00 then ret = 100
if value = &h80 then ret = 75
if value = &hC0 then ret = 50
if value = &hE0 then ret = 25
if value = &hF0 then ret = 10
' Register = &h78 7=75% 6=50% 5=25% 4=3.2V 1=3.0V(Shutdown)
end sub

sub ip5306_isCharging(addr, ret) ' Return 1 if battery is charging, 0 if not
value = i2c.readregbyte(addr, &h70)
ret = value >> 7
end sub

sub ip5306_isChargeFull(addr, ret) ' Return 1 if battery if full charged, 0 if not
value = i2c.readregbyte(addr, &h71)
ret = value >> 7
end sub
end
Top
User avatar
cicciocb
Site Admin
Posts: 1900
Joined: Mon Feb 03, 2020 1:15 pm
Location: Toulouse
Has thanked: 407 times
Been thanked: 1271 times
Contact:

Re: M5Stack Basic accu level

Post by cicciocb »

Thanks Jan for your contribution.
Do you think that it worth to include these routines directly into the ESP32 code ?

cicciocb
Jan Volk
Posts: 71
Joined: Wed Mar 03, 2021 1:35 pm
Been thanked: 23 times

Re: M5Stack Basic accu level

Post by Jan Volk »

This would be nice and call this from a program so that the M5Stack is no longer a black box regarding the battery level and place a heavier battery module. What a party.

Thanks for your nice basic code cicciocb.
Stuart
Posts: 126
Joined: Fri Feb 19, 2021 7:46 pm
Has thanked: 5 times
Been thanked: 20 times

Re: M5Stack Basic accu level

Post by Stuart »

Wow, it "just worked" thanks Jan
Post Reply