## ARDUINO BMS

#### INTRODUCTION

Now this might seem like it was a complete waste of time to try and reinvent the wheel with creating my owm custom BMS solution. You can buy them on ebay or alibaba or banggood for super cheap and it seems like a plug and play solution.

One problem I see with this, at least for me, is that I dont completely trust those cheap BMS system. I like to buy something that is reasonable as well as trusting that it will safely protect my cells from unbalancing. Another this is that I like to have flexibility with how I balance my cells or anything else.

Now this is where the custom arduino BMS came into play. I’ve designed this bms to handle a 4S battery pack so upto 16.8V fully charged and 14.8V nominal. The balance discharge current can be adjusted by simply replacing the discharge resistor with any value you wish. Just to add this does not have over current or over voltage protection..yet 🙂 I’m currently working on an add on for this so stay tuned for that future update.

Lets get started with all the cool details for this arduino bms.

#### 3D RENDERING

Here i just wanted to show you the 3D rendering images of the final board. The rendering was created using kicad and its pretty nice actually for an EE cad software.

The heart of this system is the atmega328P running the minicore arduino bootloader[1]. From here you have the 4 balancing passive resistors to the right of the board. The best part of this project is that you can replace these resistors to any value you want to customize the balance current.

The one thing that this does not have is overvoltage and overcurrent protection but that will be a separate circuit later on ;).

Now we can talk about the main features below in the schematic section.

#### SCHEMATICS

Below are images of the full schematics for this project:

The first page of the schematic files is a hierarchy block level of the system to help understand how a lot of these subcircuits are linked together. Below I’ll breakdown all the pages and what exactly they do.

1. Hierarchy Block:

• This is the overview block level that links the subcircuits on the separate pages. I normally like to design my projects this way if I have more than 2 subcircuits because its easier to see how things are working together

2. Power Supply

• This section is just the power supply section to power the arduino. This is a high differential buck converter. The input voltage can go up to 36V and output down to 1.8V with an active quiescent current of only 190uA. Now the most important part is the quiescent current because we are trying to eliminate any wasted current since this is for a battery operated system and we do not want to drain the batteries faster than needed.

3. MCU

• The MCU page has the Atmega328P and all the corresponding IO related to it. Im using A0-A3 to measure the batteries via a resistor divider
• This page also includes the external reference voltage used instead of the built in 1.1V from the atmega328p. Its a 1.25V external voltage reference that has a better tolerance at a range of temperatures.

4. Balancing Circuit

• This page has the balacing circuit which is incharge of discharging the battery cell that has a higher potentional. The heart of this is a bidirectional N-channel mosfet that allows current to flow in both directional either during discharge or during charging.

5. Connectors

• This page has the power pin connectors and the BMS pads that connectors to the lithium cells.

Now that we’ve gone through the schematic lets go into the second most important part and that is the arduino code.

#### ARDUINO CODE

Now probably the second most important part is the arduino code that will control the mosfets triggering the discharge when a preset voltage difference is hit.

/* Revision: A9
* Written by: Steven Guzman
* Date: 7/10/2018
* Description: This is a customizable BMS system for a 4
* cell lithium ion pack.
*/

#include "Wire.h"
#include "avr/wdt.h"

#define OLED_RESET 4

#define NUMFLAKES 10
#define XPOS 0
#define YPOS 1
#define DELTAY 2

//////////////////////////////////////////////
// This variable is used for checking on
// whether the OLED screen is attached or not
//////////////////////////////////////////////
int check = LOW;

//////////////////////////////////////////////
// Sampling number for analog read values.
// Will use this sample size to calculate a
// better estimation of the analog value
//////////////////////////////////////////////
int samples = 100;

//////////////////////////////////////////////
// This is the max difference that the cells
// can achieve in respect to each other
//////////////////////////////////////////////
float tol = 0.03;

//////////////////////////////////////////////
// To temporaly store analog values from the
// cell voltages to average out later with
// the number of samples taken
//////////////////////////////////////////////
float temp_cell[4];

//////////////////////////////////////////////
// Float array to store unconverted cell values
//////////////////////////////////////////////
float cell[4];

//////////////////////////////////////////////
// Variable array to store converted voltage
// values for cell measurement
//////////////////////////////////////////////
float B[4];

//////////////////////////////////////////////
// Variable array to store voltage differences
// between cells
//////////////////////////////////////////////
float diff[4];

/////////////////////////////////////////////////
// Variable used for digital output
// signals
/////////////////////////////////////////////////
int BAT[4];

/////////////////////////////////////////////////
// Variable used for storing the cell pack
/////////////////////////////////////////////////
float PACK = 0.000;

/////////////////////////////////////////////////
// Scaling factor used for converting
// the scaled down analogread values to there
// actual values from the voltage divider
/////////////////////////////////////////////////
float scale0 = 0.25; // 10k/(30k + 10k)
float scale1 = 0.125; // 10k/(69.8k + 10k)
float scale2 = 0.083; // 10k/(110k + 10K)
float scale3 = 0.0625; // 10k/(150k + 10k)

#if (SSD1306_LCDHEIGHT != 32)
#endif

void setup()
{

Serial.begin(9600);
analogReference(EXTERNAL); // Configure reference voltage for
// external 1.25V

//////////////////////////////////////////////
// After switching to external reference you
// need to read a value a couple times before
// the reference has stabalized.
//////////////////////////////////////////////
delay(20);

// Setup up watchdog timer to reset
// after 4 seconds
wdt_enable(WDTO_4S);

///////////////////////////////////////////////
// Variables to set the digital output
// pins 1-4
///////////////////////////////////////////////
BAT[0] = 5;
BAT[1] = 6;
BAT[2] = 7;
BAT[3] = 8;

///////////////////////////////////////////////
// Setting digital pins to output configuration
///////////////////////////////////////////////
pinMode(BAT[0], OUTPUT);
pinMode(BAT[1], OUTPUT);
pinMode(BAT[2], OUTPUT);
pinMode(BAT[3], OUTPUT);

///////////////////////////////////////////////
// Sets the digital outputs to an initial low
// state
///////////////////////////////////////////////

digitalWrite(BAT[0],LOW);
digitalWrite(BAT[1],LOW);
digitalWrite(BAT[2],LOW);
digitalWrite(BAT[3],LOW);

}

void loop()
{

Wire.requestFrom(0x3C, 1); // Pings OLED screen

///////////////////////////////////////////////
// If OLED pings back and check statement
// equals 1, then initiate the OLED screen
// and reset the check value to 0 so it will
// not continue to re-initiate. It needs to
// initiate another request one last time
///////////////////////////////////////////////
if (Wire.available() &amp;amp;amp;amp;amp;amp;&amp;amp;amp;amp;amp;amp; check == HIGH )
{
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
check = 0;
Wire.requestFrom(0x3C, 1);
}
if ( Wire.available() &amp;amp;amp;amp;amp;amp;&amp;amp;amp;amp;amp;amp; check == LOW)
{
// Leave blank
}
else if (!Wire.available())
{
check = 1;
}

///////////////////////////////////////////////
// Sets the temp variable array back to zero
///////////////////////////////////////////////
for (int x = 0; x &amp;amp;amp;amp;amp;lt;4; x++)
{
temp_cell[x] = 0.00;
diff[x] = 0.00;
cell[x] = 0.00;
}
///////////////////////////////////////////////
// For loop used to record battery cell voltage
// information and used later to average out
///////////////////////////////////////////////
delay(100);

////////////////////////////////////////////////
// Using true RMS calculation for computing a
// better average estimate for multiple samples.
////////////////////////////////////////////////
for (int i = 0; i &amp;amp;amp;amp;amp;lt; samples; i++)
{
temp_cell[0] = temp_cell[0] + sq((analogRead(A0)/scale0)* (1.249 / 1024));
delay(1);
temp_cell[1] = temp_cell[1] + sq((analogRead(A1)/scale1) * (1.249 / 1024));
delay(1);
temp_cell[2] = temp_cell[2] + sq((analogRead(A2)/scale2) * (1.249 / 1024));
delay(1);
temp_cell[3] = temp_cell[3] + sq((analogRead(A3)/scale3) * (1.249 / 1024));
delay(1);
}
cell[0] = sqrt(temp_cell[0] / samples);
cell[1] = sqrt(temp_cell[1] / samples);
cell[2] = sqrt(temp_cell[2] / samples);
cell[3] = sqrt(temp_cell[3] / samples);

////////////////////////////////////////////////
// Scale up the cell values and then convert to
// to voltage values
////////////////////////////////////////////////
B[0] = (cell[0]);
B[1] = ((cell[1] - cell[0]));
B[2] = ((cell[2] - cell[1] ));
B[3] = ((cell[3] - cell[2] ));
PACK = B[3] + B[2] + B[1] + B[0];

///////////////////////////////////////////////////////////////////
// Compares each cell voltage to each other and then finds the
// lowest cell value. Then it stores the difference between the
// lowest cell and the remaining cells for comparison
///////////////////////////////////////////////////////////////////
if ( (B[0] &amp;amp;amp;amp;amp;lt;= B[1]) &amp;amp;amp;amp;amp;amp;&amp;amp;amp;amp;amp;amp; (B[0] &amp;amp;amp;amp;amp;lt;= B[2]) &amp;amp;amp;amp;amp;amp;&amp;amp;amp;amp;amp;amp; (B[0] &amp;amp;amp;amp;amp;lt;= B[3]))
{
diff[1] = B[1] - B[0];
diff[2] = B[2] - B[0];
diff[3] = B[3] - B[0];
}
else if ((B[1] &amp;amp;amp;amp;amp;lt;= B[0]) &amp;amp;amp;amp;amp;amp;&amp;amp;amp;amp;amp;amp; (B[1] &amp;amp;amp;amp;amp;lt;= B[2]) &amp;amp;amp;amp;amp;amp;&amp;amp;amp;amp;amp;amp; (B[1] &amp;amp;amp;amp;amp;lt;= B[3]))
{
diff[0] = B[0] - B[1];
diff[2] = B[2] - B[1];
diff[3] = B[3] - B[1];
}
else if ((B[2] &amp;amp;amp;amp;amp;lt;= B[0]) &amp;amp;amp;amp;amp;amp;&amp;amp;amp;amp;amp;amp; (B[2] &amp;amp;amp;amp;amp;lt;= B[1]) &amp;amp;amp;amp;amp;amp;&amp;amp;amp;amp;amp;amp; (B[2] &amp;amp;amp;amp;amp;lt;= B[3]))
{
diff[0] = B[0] - B[2];
diff[1] = B[1] - B[2];
diff[3] = B[3] - B[2];
}
else
{
diff[0] = B[0] - B[3];
diff[1] = B[1] - B[3];
diff[2] = B[2] - B[3];
}

//////////////////////////////////////////////////////////////////
// It enables the balancing protocol for a given cell that has a
// greater voltage difference from the lowest cell and the set
// tolerance.
//////////////////////////////////////////////////////////////////
delay(1);
for ( int x = 0; x tol )
{
digitalWrite(BAT[x], HIGH);
delay(1);
}
else
{
digitalWrite(BAT[x], LOW);
delay(1);
}
delay(1);
}

// text display tests
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0,0);
display.print("V1:");
display.print(B[0]);
display.println("V");
display.print("V2:");
display.print(B[1]);
display.println("V");
display.print("V3:");
display.print(B[2]);
display.println("V");
display.print("V4:");
display.print(B[3]);
display.println("V");

display.setCursor(70,0);
display.print("VP:");
display.print(PACK);
display.print("V");

display.setCursor(70,8);
display.print("Config:");
display.print("4S");
display.display();
delay(1);
display.clearDisplay();
wdt_reset();
}

Lets break down this code in order to understand my thought process behind this.

1. The samples variable is used to create an average sampling of the battery cells to eliminate any noise issues. The averaging technique I used in this project is a little different than most but I will explain later on
2. The next variable is the tol variable. This variable is the maximum voltage different between the lowest cell and the rest of the cells in the 4S pack. Currently its set to a maximum of 30mV difference but this can be set to a minimum of 10mV because of the 8-bit adc on the arduino.
1. Here is the scaling factor for the adc from the cell measurements. We have different values because in order to get the best accuracy measuring in reference to ground, different values were needed for measuring cell 1 which has a max of 4.2V and cell 4 which measures a max of 16.8V.
1. This portion of the code is mainly for the OLED screen. It allows for the screen to be removed and re attached without having to reset the arduino. Before you had to initialize it in the setup but with this loop it is initialized in the main loop when it detects the screen.
1. Now this section is averaging the adc values from all 4 cells to get a best measurement of the cell voltage values. Instead of using the traditional sample 25 and take the average, I’m using a true RMS calculation that will give you a slightly better voltage measurement that just taking the average. It will be at a closer measurement to your multimeter than using average sample.

#### CONCLUSION / ISSUES

Here is the final PCB:

I’m pretty excited to do more testing on this and I will post my technical review later on that includes current consumption, maybe a mini manual on how to use it or change values for your own build.

Again this does not include an over voltage or over current protection circuit but I decided to make that a separate board with its own controller in order to make the board as small as I could and isolate the two circuits as a redundancy. I’m still making some tweaks and might make another revision but all in all I’m very happy about it.

Some current issues that I know off.

1. You might have to calibrate with a known good multimeter. The arduinos ADC has some weird issues with analogPin A2 measuring higher than the rest. I added a calibration value in the code to help with that.

For the Bill of Materials, I’ve embedded the datasheets for some of the major components in case you wondered why the file is larger than it should.

## How-to: Design a Boost Converter

#### Designing a boost converter sounds complicated and intimidating, well that was always my impression when it came to this topic in school.  In reality, the design and testing of a boost converter is a lot easier than meets the eye.

Here I will walk you step by step on designing your first boost converter and how the datasheet is your best friend when designing.  For this tutorial we will be using the L6920DC IC Boost converter from skyworks.[1]

This information was referenced from TI reference report.[2]

First and foremost, download the highlighted datasheet, datasheet-l6920dc. This has all the highlighted paremeters that you will need when designing a boost converter.

Step 1:

You need to decide what are your specifications.  These are the key parameters:

• Vin(min)
• Vin(max)
• Vout
• Iout
• n = efficiency; Most boost converters average around 85 to 90% under medium load and up to 95% on heavy load.  We will use the lowest percentage to be safe.

Example:

• Vin(min): 1.8V
• Vin(max): 2.4V
• Vout: 3.3V
• Iout: 0.4A
• n = 87% or 0.87

Step 2:

With your specifications, next step is to find your DUTY CYCLE:

$\bf \Huge D= 1 - \cfrac{(V_{\text{IN}}*n)}{V_{\text{OUT}}}$

We calculated the duty cycle for both lowest input voltage and highest input voltage.

• Lowest input voltage gives you the highest switching current you will see
• Highest input voltage gives you the highest output current your converter can produce

Example:

$\bf \Huge V_{\text{IN-MIN}}$

$\Huge D = 1 - \cfrac{1.8V*0.85}{3.3V}$ = 0.52

$\bf \Huge V_{\text{IN-MAX}}$

$\Huge D = 1 - \cfrac{2.4V*0.85}{3.3V}$ = 0.36

Step 3:

Next we will estimate the switching current or CURRENT RIPPLE of the Inductor:

ΔIL = $(0.3) * I_\text{OUTmax} * \cfrac{(V_\text{OUT})}{(V_\text{IN})}$

Example:

$\bf \Huge V_{\text{IN-MIN}}$

ΔIL = $\Huge (0.3) * 0.4A * \cfrac{3.3V}{1.8V}$ = 0.22A

$\bf \Huge V_{\text{IN-MAX}}$

ΔIL = $\Huge (0.3) * 0.4A * \cfrac{3.3V}{2.4V}$ = 0.165A

Step 4:

Next we calculate the minimum INDUCTANCE we need:

$\bf \Huge L_\text{MIN} = \cfrac{(V_\text{IN})*(V_\text{OUT} - V_\text{IN})}{\Delta I_\text{L}*f_\text{S}*V_\text{OUT}}$

$\bf \Huge f_\text{S}$ – This is the switching frequency that the converter will operate at.

Example:

$\bf \Huge V_{\text{IN-MIN}}$

$\Huge L_\text{MIN} = \cfrac{(1.8V)*(3.3V - 1.8V)}{0.22A*1MHz*3.3V}$ = 3.72uH

$\bf \Huge V_{\text{IN-MAX}}$

$\Huge L_\text{MIN} = \cfrac{(2.4V)*(3.3V - 2.4V)}{0.165A*1MHz*3.3V}$ = 3.97uH

We would select the highest inductance value to meet our input voltage rage of 1.8V-2.4V

When selecting the inductor, the key parameters you need to look for is low DCR, package size, and max current the inductor can handle.

DCR – Is the resistance in the coil because at the end of the day, an inductor is still a wire. When you keep this value at a minimum, it will increase your effieciency and the ability to provide a higher output power.

In step 7 , you will calculate the maximum current the inductor will see and there you will have all the necessary parameters needed to chose the inductor.

Step 5:

Now that we have our inductor value, we can calculate the actual CURRENT RIPPLE of the Inductor:

ΔIL = $\bf \Huge \cfrac{V_{IN}*D}{f_\text{S}*L}$

Example:

$\bf \Huge V_{\text{IN-MIN}}$

ΔIL = $\Huge \cfrac{1.8V*0.525}{1MHz*3.72uH}$ = 0.19A

$\bf \Huge V_{\text{IN-MAX}}$

ΔIL = $\Huge \cfrac{2.4V*0.36}{1MHz*3.97uH}$ = 0.18A

Step 6:

Next we need to calculate the MAX OUTPUT CURRENT the boost converter can output:

$\bf \Huge I_\text{MAXOUT}$ = $\bf \Huge (I_\text{LIM} - \cfrac{\Delta I_\text{L}}{2}) * (1 - D)$

$I_\text{LIM}$ – This is the current switch limit of the boost converter.

Example:

$\bf \Huge V_{\text{IN-MIN}}$

$\Huge I_\text{MAXOUT}$ = $\bf \Huge (0.8A - \cfrac{0.19A}{2}) * (1 - 0.52)$ = 0.33A

$\bf \Huge V_{\text{IN-MAX}}$

$\Huge I_\text{MAXOUT}$ = $\bf \Huge (0.8A - \cfrac{0.18A}{2}) * (1 - 0.36)$ = 0.45A

Step 7:

Next we will calculate the MAX SWITCHING CURRENT, $I_\text{SW}$ the Inductor will see.  This value cannot exceed the ILIM value of the boost converter:

$\bf \Huge I_\text{SW-MAX} = \cfrac{\Delta I_\text{L}}{2} + \cfrac{I_\text{OUT}}{1 - D}$

Example:

$\bf \Huge V_{\text{IN-MIN}}$

$\Huge I_\text{SW-MAX} = \cfrac{0.19A}{2} + \cfrac{0.4A}{1 - 0.525}$ = 0.94A

$\bf \Huge V_{\text{IN-MAX}}$

$\Huge I_\text{SW-MIN} = \cfrac{0.18A}{2} + \cfrac{0.4A}{1 - 0.36}$ = 0.72A

Note: $\Huge I_\text{SW-MAX}$ value cannot exceed $\Huge I_\text{LIM}$ which can be found in the datasheet.  In this example we see that with a low input voltage, the switching current exceeds the limit in the datasheet.  The boost converter might still be able to output the desired current at that low input voltage because $\Huge I_\text{LIM}$ is the minimum switching current it can handle.  But better to be safe than sorry.

Here you can see the inductor will see a max of 0.94A at its lowest input voltage. Now we can chose the inductor for our design.

For this design I went with,MSS5131-472MLB, a 4.7uH inductor from coilcraft.[3]

Since I chose an inductor that has a higher value than previous calculated, the inductor current ripple and output power will be slightly lower but it will not effect your design negatively.

Step 8:

This step is only if your boost converter has an adjustable output voltage.

(This boost converter is a fixed output and does not require these resistors.  Step 8 values are dummy values but the process )

Here we will find R1 AND R2 values for the feedback network:

$\bf \Huge I_\text{R0.5} >= 100 * I_\text{FB}$

$\bf \Huge I_\text{FB}$ – This is the current that the feedback resistor draws.

$\bf \Huge R_2 = \cfrac{V_\text{FB}}{I_\text{R0.5}}$

$\bf \Huge V_\text{FB}$ – This is the feedback reference voltage

$\bf \Huge R_1 = R_2 * (\cfrac{V_\text{OUT}}{V_\text{FB}}-1)$

Example:

$\Huge I_\text{R0.5} >= 100 * 350nA$ = 35mA

$\Huge R_2 = \cfrac{1.24V}{35mA}$ = 35.4kΩ

$\Huge R_1 = 35.4k \Omega * (\cfrac{3.3V}{1.24V}-1)$ = 58.74kΩ

Step 9:

Next l, we will calculate the INPUT CAPACITOR and OUTPUT CAPACITOR needed to minimize the ripple going in and out of the system:

First, you find your input capacitor:

$\bf \Huge C_\text{IN}$: Typically this value is 4.7uF to 10uF

Next, we need to first to look at these two equations below[6]:

$\bf \Huge \Delta V_\text{OUT}= \cfrac{I_\text{OUT} *T_\text{ONmax}}{ C_\text{OUT}}$

$\bf \Huge T_\text{ONmax}$ – This is the maximum on time of the boost converter.  It is also written as

$\bf \Huge D * T_\text{S}$

Were $\bf \Huge T_\text{S} = \cfrac{1}{f_\text{S}}$

$\bf \Huge \Delta V_\text{OUTesr} = ESR* I_\text{SW-MAX}$

ESR – All capacitors are not ideal capacitors and therefore have what is known as Equivalent Series Resistance. This is an important parameter that you need to consider when choosing the right output capacitor.

Example:

Cin = 10uF

First, we need to choose a voltage ripple that we can live with. Here I chose 50mV, and if we rearrange the first equation, we get:

$\Huge \Delta C_\text{OUTmin}= \cfrac{0.4 *6.25us}{50mV}$ = 50uF

Now we have a couple of options to choose from when it comes to materials for capacitors.

Most common are ceramic and electrolytic capacitors.  Each have there own pro and con.

Ceramic capacitors offer lower ESR for lower ripple but they typically do not have the bulk capacitance.

Electrolytic capacitors have bulk capacitance but generally have a high ESR that adds to ripple.

In this case I decided to go with both, getting the benefit of bulk capacitance and low ESR.

I went with a 1206 package, 10uF ceramic capacitor and a 47uF Electrolytic in parallel. For the electrolytic, they also have an aluminum polymer that has high capacitance with the added benefit of low ESR. I went with a 47uF that has an ESR of 40mΩ.

Now we plug in the values we got back into the equations and we get:Special Note: For ceramic capacitors, you need to be careful of which class and package size you choose because you only see a certain percentage of your nominal value (ex. 1206 10uF X7R will see 73% of 10uF)[4]. Click here for more info. I generally go with 1206 or 1210 with capacitors.

$\Huge \Delta V_\text{OUT}= \cfrac{0.4A * 6.25us}{50uF}$ = 50mV

$\Huge \Delta V_\text{OUTesr} = 40m\Omega*0.94A$ = 37mV

$\Huge \Delta V_\text{OUT}$ = 87mV
Always refer to the datasheet and compare recommended value vs calculated[1]

You’ve now designed your own boost converter regulator.  See it wasnt too hard :).

I will post this project soon that has the schematic and bill of materials, it’ll be under the projects menu bar, stay tuned!!

Feel free to comment below and correct me if anything seems incorrect to you.

References:

1. L6920 Datasheet

2. TI Basic Calculations of a Boost Converter Power Stage

3. Coilcraft Inductor

4. Temperature and Voltage Variation of Ceramic Capacitors

5. Ceramic or electrolytic output capacitors
in DC/DC converters—Why not both?

6. Boost Converter Output Capacitor Selection