# DIY Lithium Battery Balancer and monitoring (BMS)

**URL:** <https://community.openenergymonitor.org/t/diy-lithium-battery-balancer-and-monitoring-bms/5594>\
**Category:** Hardware\
**Tags:** powerwall, bms, diybms, battery\
**Created:** [8 November 2017 13:02 UTC](https://community.openenergymonitor.org/t/diy-lithium-battery-balancer-and-monitoring-bms/5594 "2017-11-08T13:02:53Z")\
**Posts on this page:** 20\
**Page:** 13

<div class="post-metadata">

**Author:** ![Jman\_brosef](https://community.openenergymonitor.org/user_avatar/community.openenergymonitor.org/jman_brosef/32/11339_2.png) [@Jman\_brosef](https://community.openenergymonitor.org/u/Jman_brosef)\
**Post date:** [22 April 2018 07:55 UTC](https://community.openenergymonitor.org/t/diy-lithium-battery-balancer-and-monitoring-bms/5594/243 "2018-04-22T07:55:38Z")

</div>

I am still trying to get my mppt charge controller to connect to my pi and then get granfana working

---

<div class="post-metadata">

**Author:** ![Colin\_Hickey](https://community.openenergymonitor.org/user_avatar/community.openenergymonitor.org/colin_hickey/32/9434_2.png) [@Colin\_Hickey](https://community.openenergymonitor.org/u/Colin_Hickey)\
**Post date:** [22 April 2018 07:59 UTC](https://community.openenergymonitor.org/t/diy-lithium-battery-balancer-and-monitoring-bms/5594/244 "2018-04-22T07:59:05Z")

</div>

So are you just monitoring the values in the built in web interface? I’d be interested to see if Stuart’s code still gets the eroneous values, i’ll have to switch back and check. I’ve not touched that side so you should get the same issues with his code as well i think. BTW you can do the battery monitoring in grafana independent of the charge controller monitoring.

I’m taking some solace in the fact that your experiencing the same at least.

---

<div class="post-metadata">

**Author:** ![stuart](https://community.openenergymonitor.org/user_avatar/community.openenergymonitor.org/stuart/32/16661_2.png) [@stuart](https://community.openenergymonitor.org/u/stuart)\
**Post date:** [22 April 2018 08:03 UTC](https://community.openenergymonitor.org/t/diy-lithium-battery-balancer-and-monitoring-bms/5594/245 "2018-04-22T08:03:21Z")

</div>

I suspect a 65535 reading is generated when the controller didn’t get a reply from one of the modules, but could also be a software bug

---

<div class="post-metadata">

**Author:** ![Colin\_Hickey](https://community.openenergymonitor.org/user_avatar/community.openenergymonitor.org/colin_hickey/32/9434_2.png) [@Colin\_Hickey](https://community.openenergymonitor.org/u/Colin_Hickey)\
**Post date:** [22 April 2018 08:07 UTC](https://community.openenergymonitor.org/t/diy-lithium-battery-balancer-and-monitoring-bms/5594/246 "2018-04-22T08:07:18Z")

</div>

Are you seeing them on your setup also stuart? Just curious if it’s something on my side as i had issues with the pull ups initially and without the setclockstrechlimit change i get no readings at all as the modules can’t be found/communicated with.

Hope you enjoyed Portugal 🙂

---

<div class="post-metadata">

**Author:** ![stuart](https://community.openenergymonitor.org/user_avatar/community.openenergymonitor.org/stuart/32/16661_2.png) [@stuart](https://community.openenergymonitor.org/u/stuart)\
**Post date:** [22 April 2018 08:44 UTC](https://community.openenergymonitor.org/t/diy-lithium-battery-balancer-and-monitoring-bms/5594/247 "2018-04-22T08:44:24Z")

</div>

I’ve not setup my test rig since I’ve been pack from holiday.

However I didn’t need to change the stretch limit on my setup to make it work!

I2C does have cable length limits which is why CAN BUS protocol are better for this sort of thing, so keep the cables as short as possible.

---

<div class="post-metadata">

**Author:** ![Colin\_Hickey](https://community.openenergymonitor.org/user_avatar/community.openenergymonitor.org/colin_hickey/32/9434_2.png) [@Colin\_Hickey](https://community.openenergymonitor.org/u/Colin_Hickey)\
**Post date:** [22 April 2018 09:36 UTC](https://community.openenergymonitor.org/t/diy-lithium-battery-balancer-and-monitoring-bms/5594/248 "2018-04-22T09:36:56Z")

</div>

I guess as your using a nodeMCU and i’m using a wemos D1 and with potentially different cable lengths (32cm here) that must be what is causing the differences. I’ve just modified my code to use a strechlimit of 2000 and it’s working rock solid now with no false values so I’ve hooked it up to my powerwall and getting some nice monitoring 🙂

---

<div class="post-metadata">

**Author:** ![Colin\_Hickey](https://community.openenergymonitor.org/user_avatar/community.openenergymonitor.org/colin_hickey/32/9434_2.png) [@Colin\_Hickey](https://community.openenergymonitor.org/u/Colin_Hickey)\
**Post date:** [22 April 2018 15:05 UTC](https://community.openenergymonitor.org/t/diy-lithium-battery-balancer-and-monitoring-bms/5594/249 "2018-04-22T15:05:33Z")

</div>

Since my changes this morning it’s been really stable and giving reliable readings 🙂

 ![image](https://community.openenergymonitor.org/uploads/default/original/2X/2/2ba43f91f9f781c9e671161e70743d5fe860037e.png)

---

<div class="post-metadata">

**Author:** ![Jman\_brosef](https://community.openenergymonitor.org/user_avatar/community.openenergymonitor.org/jman_brosef/32/11339_2.png) [@Jman\_brosef](https://community.openenergymonitor.org/u/Jman_brosef)\
**Post date:** [22 April 2018 15:31 UTC](https://community.openenergymonitor.org/t/diy-lithium-battery-balancer-and-monitoring-bms/5594/250 "2018-04-22T15:31:17Z")

</div>

Hey Collin. I am using your variant of the code. I changed the 1500 to 2000 and I’m also not getting the false values. woooooooooooooooooohooooooooooooooooooooooooo. I can finally sleep at night. I am using a nodeMcu with 2 2.2k pullup resistors.

---

<div class="post-metadata">

**Author:** ![Jman\_brosef](https://community.openenergymonitor.org/user_avatar/community.openenergymonitor.org/jman_brosef/32/11339_2.png) [@Jman\_brosef](https://community.openenergymonitor.org/u/Jman_brosef)\
**Post date:** [22 April 2018 19:58 UTC](https://community.openenergymonitor.org/t/diy-lithium-battery-balancer-and-monitoring-bms/5594/251 "2018-04-22T19:58:43Z")

</div>

maybe we could use this tutorial to convert the temperature readings into centigrade.

> **[Thermistor](https://learn.adafruit.com/thermistor/using-a-thermistor)**
>
> A thermistor is a thermal resistor - a resistor that changes its resistance with temperature. Technically, all resistors are thermistors - their resistance changes slightly with temperature - but the change is usually very very small and difficult to...

---

<div class="post-metadata">

**Author:** ![stuart](https://community.openenergymonitor.org/user_avatar/community.openenergymonitor.org/stuart/32/16661_2.png) [@stuart](https://community.openenergymonitor.org/u/stuart)\
**Post date:** [23 April 2018 07:39 UTC](https://community.openenergymonitor.org/t/diy-lithium-battery-balancer-and-monitoring-bms/5594/252 "2018-04-23T07:39:09Z")

</div>

You would need to use the more complex formula

> <https://electronics.stackexchange.com/questions/8754/how-to-measure-temperature-using-a-ntc-thermistor>

Steinhart–Hart equation

---

<div class="post-metadata">

**Author:** ![stuart](https://community.openenergymonitor.org/user_avatar/community.openenergymonitor.org/stuart/32/16661_2.png) [@stuart](https://community.openenergymonitor.org/u/stuart)\
**Post date:** [23 April 2018 07:41 UTC](https://community.openenergymonitor.org/t/diy-lithium-battery-balancer-and-monitoring-bms/5594/253 "2018-04-23T07:41:03Z")

</div>

> [@Jman\_brosef](#):
>
> I changed the 1500 to 2000 and I’m also not getting the false values.

Just a thought - on the ATTINY chips are you both using 8Mhz internal clocks ?

You need to manually program the fuses before you program the chip.

If you are running at 1Mhz it may explain why you need such a slow clockstretch value.

---

<div class="post-metadata">

**Author:** ![Colin\_Hickey](https://community.openenergymonitor.org/user_avatar/community.openenergymonitor.org/colin_hickey/32/9434_2.png) [@Colin\_Hickey](https://community.openenergymonitor.org/u/Colin_Hickey)\
**Post date:** [23 April 2018 09:26 UTC](https://community.openenergymonitor.org/t/diy-lithium-battery-balancer-and-monitoring-bms/5594/254 "2018-04-23T09:26:45Z")

</div>

Hi Stuart, that would make perfect sense. I see it’s mentioned at the top of the module code as well, i totally missed it.

---

<div class="post-metadata">

**Author:** ![Colin\_Hickey](https://community.openenergymonitor.org/user_avatar/community.openenergymonitor.org/colin_hickey/32/9434_2.png) [@Colin\_Hickey](https://community.openenergymonitor.org/u/Colin_Hickey)\
**Post date:** [23 April 2018 12:04 UTC](https://community.openenergymonitor.org/t/diy-lithium-battery-balancer-and-monitoring-bms/5594/255 "2018-04-23T12:04:27Z")

</div>

> [@stuart](#):
>
> Steinhart–Hart

I’ve done some code today based on the Steinhart–Hart equation. I’ll pop it on my github a little later when i get a chance to test it properly.

---

<div class="post-metadata">

**Author:** ![JoeyPoortman](https://community.openenergymonitor.org/letter_avatar_proxy/v4/letter/j/a5b964/32.png) [@JoeyPoortman](https://community.openenergymonitor.org/u/JoeyPoortman)\
**Post date:** [23 April 2018 15:18 UTC](https://community.openenergymonitor.org/t/diy-lithium-battery-balancer-and-monitoring-bms/5594/256 "2018-04-23T15:18:04Z")

</div>

I2C performs well with bigger cables though! Got a 15m setup on 2.5mm2 solid copper wire and it has almost no voltage drop over that 15m!

---

<div class="post-metadata">

**Author:** ![Colin\_Hickey](https://community.openenergymonitor.org/user_avatar/community.openenergymonitor.org/colin_hickey/32/9434_2.png) [@Colin\_Hickey](https://community.openenergymonitor.org/u/Colin_Hickey)\
**Post date:** [23 April 2018 17:26 UTC](https://community.openenergymonitor.org/t/diy-lithium-battery-balancer-and-monitoring-bms/5594/257 "2018-04-23T17:26:29Z")

</div>

The issue was down to the fuses not being set correctly on the attiny85’s on the modules. I’ve tested tonight and after setting the fuses correctly I’ve modified the stretch limit back to 1000 and it worked straight away.

---

<div class="post-metadata">

**Author:** ![Colin\_Hickey](https://community.openenergymonitor.org/user_avatar/community.openenergymonitor.org/colin_hickey/32/9434_2.png) [@Colin\_Hickey](https://community.openenergymonitor.org/u/Colin_Hickey)\
**Post date:** [23 April 2018 17:31 UTC](https://community.openenergymonitor.org/t/diy-lithium-battery-balancer-and-monitoring-bms/5594/258 "2018-04-23T17:31:14Z")

</div>

Hi Jman, If your using my fork of the code the readings are in Celsius now. I’ve also modified the grafana upload code so the temps are uploaded to grafana as well as the bypass status as well. Stuart this was the function for the Steinhart-Hart equation. Not as elegant as any of your code but you can get the idea. The values for RT1 and RT2 are from the NTC thermistor datasheet and set for a 10 Thermistor/20k resistor values

float Vin=3.3; // [V]  
float Rt=10000; // Resistor t [ohm]  
float R0=20000; // value of rct in T0 [ohm]  
float T1=273.15; // [K] in datasheet 0º C  
float T2=373.15; // [K] in datasheet 100° C  
float RT1=35563; // [ohms] resistence in T1  
float RT2=549.4; // [ohms] resistence in T2  
float beta=0.0; // initial parameters [K]  
float Rinf=0.0; // initial parameters [ohm]  
float T0=298.15; // use T0 in Kelvin [K]  
float Vout=0.0; // Vout in A0  
float Rout=0.0; // Rout in A0  
float TempK=0.0; // variable output  
float TempC=0.0; // variable output

float tempconvert(float rawtemp) {  
Vout=Vin\*((float)(rawtemp)/1024.0); // calc for ntc  
Rout=(Rt_Vout_(Vin-Vout));

TempK=(beta/log(Rout/Rinf)); // calc for temperature  
TempC=TempK-273.15;

return TempC;  
}

---

<div class="post-metadata">

**Author:** ![Jman\_brosef](https://community.openenergymonitor.org/user_avatar/community.openenergymonitor.org/jman_brosef/32/11339_2.png) [@Jman\_brosef](https://community.openenergymonitor.org/u/Jman_brosef)\
**Post date:** [23 April 2018 17:51 UTC](https://community.openenergymonitor.org/t/diy-lithium-battery-balancer-and-monitoring-bms/5594/259 "2018-04-23T17:51:01Z")

</div>

Thanks Collin. How did you burn the fuses. Did you use the usbasp?

---

<div class="post-metadata">

**Author:** ![Colin\_Hickey](https://community.openenergymonitor.org/user_avatar/community.openenergymonitor.org/colin_hickey/32/9434_2.png) [@Colin\_Hickey](https://community.openenergymonitor.org/u/Colin_Hickey)\
**Post date:** [23 April 2018 17:54 UTC](https://community.openenergymonitor.org/t/diy-lithium-battery-balancer-and-monitoring-bms/5594/260 "2018-04-23T17:54:21Z")

</div>

Yes, stuart has put in the command for doing it with avrdude but i did it through the IDE by selecting the attiny85 as you would prior to uploading, setting the correct speed etc and selecting usbasp as the programmer and then chose burn bootloader. After that i had to re-upload the code, you will need to reprovision the modules as well.

---

<div class="post-metadata">

**Author:** ![Jman\_brosef](https://community.openenergymonitor.org/user_avatar/community.openenergymonitor.org/jman_brosef/32/11339_2.png) [@Jman\_brosef](https://community.openenergymonitor.org/u/Jman_brosef)\
**Post date:** [23 April 2018 23:42 UTC](https://community.openenergymonitor.org/t/diy-lithium-battery-balancer-and-monitoring-bms/5594/261 "2018-04-23T23:42:19Z")

</div>

Thanks. Uploaded the new code. My temperatures are not showing correctly.

 ![Capture](https://community.openenergymonitor.org/uploads/default/original/2X/9/9ac6f61e757cfd307453227178a55bf9d219ebce.PNG)

---

<div class="post-metadata">

**Author:** ![Colin\_Hickey](https://community.openenergymonitor.org/user_avatar/community.openenergymonitor.org/colin_hickey/32/9434_2.png) [@Colin\_Hickey](https://community.openenergymonitor.org/u/Colin_Hickey)\
**Post date:** [24 April 2018 13:09 UTC](https://community.openenergymonitor.org/t/diy-lithium-battery-balancer-and-monitoring-bms/5594/262 "2018-04-24T13:09:36Z")

</div>

If you click onto the modules view what values are you seeing?

Mine is logging great in the modules view and grafana as well, should add main view works well also. I’ve set the legend so it’s from -25 to 100 degrees on the scale now.

 ![image](https://community.openenergymonitor.org/uploads/default/original/2X/3/355ac80a8733db4b4b4235699054eaf76a3a4823.png)

 ![image](https://community.openenergymonitor.org/uploads/default/original/2X/1/14d799f1570cef427f6e9647c42a92d6bd6a3465.png)

 ![image](https://community.openenergymonitor.org/uploads/default/original/2X/9/995ac79afcaec5fe38520458428e1be64f94c188.png)

[Previous page](https://community.openenergymonitor.org/t/diy-lithium-battery-balancer-and-monitoring-bms/5594.md?page=12)

[Next page](https://community.openenergymonitor.org/t/diy-lithium-battery-balancer-and-monitoring-bms/5594.md?page=14)
