# Battery Managment system

hi im trying to balance 2 lithium ion batterys using an PWM from arduino to drive the gate of 2 mosfet's, i want both batterys to to discharge equally i have 2 hall effect current sesors on the inputs of the arudino i want to print this data on a lcd shield and vary the pwm accrodingly to which ever battery has the lowest current reading, im very new to programming and would really like some help.

this is formula for the mosfet
y = 0.0032x +0.702

x = y-0.702/0.0032

x is the frequency of the pwm

this is formula to work out the current from the hall effect current sensor

y = -0.0014x + 3.2841

x = y-3.2841/-0.0014

xis the current read by the current sensor

y = 0.0032x +0.702
then
x = (y - 0.702) / 0.0032

These braces must not be omitted. Same for the second pair of equations.

hi as i say im very new to ardunio and really dont have a clue what im doing so far this what ive got and its not very good, im lost at how to put the equations into code. the first equation is to work out current in amps for a hall effect current sensor and the second is for the frequency of the gate voltage to allow more curretn to pass drain to source. with changeing the pwm im tryin to equal the to current sensor to discharge my batterys equally. do this make sense? i also want to print input mAmps to an lcd shield

so far i think ive manage to assign inputs and output but then i got lost and confussed about how to do the next bit

include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 10, 5, 4, 3, 2);

const int currentsensor1Pin = 0; // select the input pin form the first current sensor
const int currentsensor2Pin = 1; // select the input pin from the second current sensor
int MOSFET PWM 1Pin = 11; // select the pin for the PWM
int MOSFET PWM 2Pin = 12; // select the pin for the PWM

int val = 0; //varriable to store the data from input 1
int val = 1; //varriable to store the data from input 2

void setup()
{
pinMode(currentsesor1Pin, INPUT);
pinMode(currentsesor2Pin, INPUT);
pinMode(MOSFET PWM 1Pin 12, OUTPUT); //declare the MOSFET PWM 1Pin as an output
pinMode(MOSFET PWM 2Pin 13, OUTPUT); //declare the MOSFET PWM 2Pin as an output
}

void loop()
{

if (val1 < val2); // this is where I haven’t got a clue what to do, I want current sensor 1 to = current sensor 2
{
analogWrite(PWM, x)// variable pwm ??

else
if val1 > val2)

analogWrite(PWM, x)//variable pwm ??

thanks

Please use code tags when posting code. It's the button with the hash symbol (#) in the editor.

``````int MOSFET PWM 1Pin = 11; // select the pin for the PWM
``````

Variables must not include spaces. This code won't compile.

What do you want to drive with your two batteries? Not every device is happy when it gets power only for a some part of the time (PWM is not a real analog signal but it's switching the power on and off very fast and the relation between the time switched off and the time switched on controls the amount of power a device gets). It's OK for most DC motors or for a heating but driving some logic chips with it may produce unexpected behavior. Also, usually not the frequency of a PWM signal is regulated but the percentage of the time the signal is in the on state.
Can you draw a schematics of the circuit you intend to control with your sketch?

You still don't use code tags. Edit your post and include code tags!

ahhh ive found it now sorry

``````#include <LiquidCrystal.h>

LiquidCrystal lcd = LiquidCrystal(6, 7, 10, 5, 4, 3, 2);

const int currentsensor1Pin = 0;  // select the input pin form the first current sensor
const int currentsensor2Pin = 1;  // select the input pin from the second current sensor
const int THRESHOLD = 0; // Threshold current to shut off batteries
int MOSFETPIN_1 = 11; // select the pin for the PWM
int MOSFETPIN_2 = 12; // select the pin for the PWM

void setup()
{
pinMode(currentsensor1Pin, INPUT);
pinMode(currentsensor2Pin, INPUT);
pinMode(12, OUTPUT); //declare the MOSFET PWM 1Pin as an output
pinMode(13, OUTPUT); //declare the MOSFET PWM 2Pin as an output
}

void loop()
{

double current1 = ((s1*5.0/1024) - 0.702)/0.0032;
double current2 =  ((s2*5.0/1024) - 0.702)/0.0032;

// LCD.Position(16x2);
lcd.clear();
lcd.print( current1, DEC);
lcd.setCursor(0,1);
lcd.print( current2, DEC);

int cycle;

if (current1 < current2)
{
cycle = (int)51*(current2-0.702)/0.0032;
analogWrite(MOSFETPIN_1, cycle);
}
else if (current1 > current2)
{
cycle = (int)51*(current1-0.702)/0.0032;
analogWrite(MOSFETPIN_2, cycle);
}

if (current1 == THRESHOLD)
digitalWrite(MOSFETPIN_1, LOW);
if (current2 == THRESHOLD)
digitalWrite(MOSFETPIN_2, LOW);

}
``````

Does that code work for you? I have doubts. You're measuring the current of a wire controlled by a PWM signal. This means it just depends on when the analogRead() is done if you have a almost 100% (1023) reading or more or less zero.

ive not actually test to see if it works yet as the lab closed before i had chance to test it but im measuring current using a hall effect current sensor and the output PWM is going to IRL540 MOSFET(VCR), im trying to use the pwm to drive the gate and let a certain amount of curretn pass through the MOSFET to discharge 2 batterys equally across a resistive load, does this make sensce?

im trying to use the pwm to drive the gate and let a certain amount of curretn pass through the MOSFET to discharge 2 batterys equally across a resistive load, does this make sensce?

In my opinion it doesn't make sense but I'm absolutely no specialist for batteries and battery management. But my gut instinct tells me it's not the right way to do it with PWM. You know that there are times when your load does get absolutely no power? What kind of load do you have?

its a purely resistive load, ( a resistor 5k6ohm wire bound high watt) ha