How to make a condition to disconnect a load

Hello guys,

i am trying to make a DIY battery system management. I am at the part of the code where i am trying to disconnect the battery when the voltage of one of the cells is below 3 volts which is ok but i want to disconnect the load at 3 volts a cell and stay disconnected till all cells are 3.2 volts and more. I do not know how to work with conditions and statements. Part of the code i want to make to work is below.

tempVal is cells voltage. If anyone of them is below 3 volts, the arduino disconnects the load but i do not want to have it disconnected forever so i want to connect the load right after all cells reach voltage 3.2+V.

I dont think this code is correct. it disconnects the load and connects it right after disconecting because the cells voltage raise when i disconnect the load so it disconnects and connects the load all the time. I want to add some time delay or better wait till all voltages are above 3.2V.

  if (tempVal1<3.0 || tempVal2<3.0 || tempVal3<3.0)[color=#222222][/color]
    digitalWrite(11, LOW);[color=#222222][/color]
    digitalWrite(11, HIGH);[color=#222222][/color]

Looking forward to your advises!


I think you need to post all of the code with a schematic … and, if was me, I would write it more logically with only one step per line so it is easier to see what is going on.
Given that I might be misunderstanding the issue !!

Look at maybe using “case” which is nice and easy to read ,

Also use more logical variable names such as :

“BatCellVolts1” etc which reflect what you are doing . I would then have a global variable “HystVolts” and make that be the voltage difference between turning on and off , so you can see If the voltage is BatCellVolts or BatCellVolts+ HystVolts.
Maybe read your voltages into a small array , then step through it checking each array cell against your conditions for on/off

Use <= or >= not just > or <

Finally bear in mind , as you say, the voltage may rise when you disconnect the load anyway due to voltage drop across the internal Cell resistance. This will depend on the load , so set your HystVolts accordingly.

Depending on the battery type the difference between charged and discharged voltage may also be small . You really need to get your multimeter out and check what is happening to the voltages , then attempt to code.

float V1;

float V2;

float V3;

void setup() {


 pinMode(2, OUTPUT);

 pinMode(3, OUTPUT);

 pinMode(4, OUTPUT);

 pinMode(5, OUTPUT);

 pinMode(6, OUTPUT);

 pinMode(7, OUTPUT);  

 pinMode(8, OUTPUT);

 pinMode(9, OUTPUT);

 pinMode(10, OUTPUT);

 pinMode(11, OUTPUT);

 pinMode(12, OUTPUT);

 pinMode(A0, INPUT);

 pinMode(A1, INPUT);

 pinMode(A2, INPUT);


void loop() {


digitalWrite(2, HIGH);

digitalWrite(3, HIGH);

tempVal1 = (analogRead(A0)*5.05)/1024;

digitalWrite(2, LOW);

digitalWrite(3, LOW);


Serial.print (V1);

Serial.print ("     ");

digitalWrite(4, HIGH);

digitalWrite(5, HIGH);

tempVal2 = (analogRead(A1)*5.05)/1024;

digitalWrite(4, LOW);

digitalWrite(5, LOW);


Serial.print (V2);

Serial.print ("     ");

digitalWrite(6, HIGH);

digitalWrite(7, HIGH);

tempVal3 = (analogRead(A2)*5.05)/1024;

digitalWrite(6, LOW);

digitalWrite(7, LOW);


Serial.print (V3);

Serial.println ("     ");

  if (V1 > 4.0)


    digitalWrite(10, HIGH);




    digitalWrite(10, LOW);


  if (V2 > 4.0)


    digitalWrite(9, HIGH);




    digitalWrite(9, LOW);


  if (V3 > 4.0)


    digitalWrite(8, HIGH);




    digitalWrite(8, LOW);


   if (V1<3.0 || V2<3.0 || V3<3.0)


    digitalWrite(11, LOW);




    digitalWrite(11, HIGH);



  if (V1>=4 && V2>=4 && V3>=4)


    digitalWrite(12, LOW);




    digitalWrite(12, HIGH);



there is a code. Every cells voltage is read by arduino individually via optocouplers. I know that if i disconnect the lode the voltage raises up a little (depending on the load) but when arduino disconnects the load at 3.0V, battery voltage raise up a little so arduino connects a load again, disconnects again after a second and so on. I need to add some time delay or another statement if one of the cells is =<3V, disconnect the load and connect the load when all the cells are 3.2 volts and more. What type of code to use please?

the other part of a code is about balancing … it works ok and it is not matter of discussion.


You should look at your code after you post it. All that color crap makes the code hard to read.

Good point why do you need to keep having the color statement ?

If before setup you set up your outputs , it again becomes more logical, for example ...

int PowerSwitch =3;

Then in setup you can write PinMode( PowerSwitch , OUTPUT);

And later it is logical when you might write ... digitalWrite ( PowerSwitch , HIGH) ; it is easy to understand .

This link might help too

Search for hysteresis, it's what you need in your code. The ON voltage needs to be higher than the OFF voltage.

It seems the hysteresis is exactly what I need. Cheers lads! :slight_smile:

Can anybody please point a direction how to write a code for hysteresis in my case? Iam kinda lost.

I want to turn off the load if one of the cells voltage is below 3.0V and turn back on when it is above 3.2V.
From 4.0 to 3.0 .. load is connected
At 3.0 and below .. load is disconnected
At 3.2 and above .. load is connected again.

Thank you.

I’ve not going to write the actual full code but here is something to think about:

If (load connected) {
  if(voltage <= 3.0) {
    disconnect load;
} else {
  if (voltage >= 3.1) {
  connect load;