How to control relay with multiply trigger condition

Hello there,
I am building a UPS project for my router and home automation server. I am using two different battery packs one 2S and one 3S. I am writing the code so that whenever the main power cuts off, the 2S relay must switch on. Additionally, when the 2S battery reaches a critical level, it must stop powering the router.

I want the firmware to handle this logic instead of relying on the 2S BMS. However, I have tried multiple times and failed to make it work. ChatGPT was also of no help. I either need a solution or at least to know whether this is even possible. Use code tags to format code for the forum

int r1 = 3;
int r2 = 4;
int r3 = 5;
int r4 = 6;
int pcs = 8;

int b2s; // 2S battery voltage
int b3s; // 3S battery voltage
int bcs; // Battery charge sensor

int sv = 0; // System state
int bi = 0; // Battery indicator

void setup() {
pinMode(r1, OUTPUT);
pinMode(r2, OUTPUT);
pinMode(r3, OUTPUT);
pinMode(r4, OUTPUT);
pinMode(pcs, INPUT);

Serial.begin(9600);

}

void loop() {
// Read sensors
int powerState = digitalRead(pcs);
b2s = analogRead(A0);
b3s = analogRead(A1);
bcs = analogRead(A2);

// -------- System State Priority Fix --------
if (b2s < 200) {
    digitalWrite(r3, LOW);
    sv = 1;
} else if (b3s < 200) {
    digitalWrite(r3, HIGH);
    bi = 2;
}

if (powerState == LOW || sv == 1) {
    digitalWrite(r1, HIGH);
    digitalWrite(r4, HIGH);
} 
if(powerState == HIGH){
    digitalWrite(r1, LOW);
    digitalWrite(r4, LOW);
}

if (b3s < 680) {
    digitalWrite(r2, LOW);
}

if (b3s > 800 && bcs > 900) {
    digitalWrite(r2, HIGH);
}

}

All that is certainly possible and likely has been done several times.

However, you are making a common mistake of trying to write a program to do all those things in version 1 of your program. That never works!

Begin, one step at a time and when that one step works, move to adding a second step. Testing all steps as you add them

May I suggest you start over by writing a program that will detect when the AC power is off and when it is on. When that works properly, add a second and third step that will detect the state of charge of your batteries.

Only then add the code to switch the output power from AC to the batteries.

3 Likes
  • Always show us a good schematic of your proposed circuit.
    Show us good images of your ‘actual’ wiring.
    Give links to components.

  • In the Arduino IDE, use Ctrl T or CMD T to format your code then copy the complete sketch.
    Use the < CODE / > icon from the ‘posting menu’ to attach the copied sketch.




  • Loss of AC can be as simple as a relay is energized by AC.
    When AC is gone, relay drops, contacts close.

im not able to upload any files as i am new user.

You did in Post #1, and got it almost-right. You just need to fix it.

b1 is never used.
r1 and r4 are constantly on because pcs is undetermined.

What variable is "main power" and what is the signal level of "cut off"? What 2S relay? None mentioned in the code or comments. What is a critical level? What is the signal from a critical level? What is "stop powering"? Is that where the invisible relay exists? How does a critically low battery energize a relay? What happens when the battery runs out of power to hold the relay, or the relay burns up for being energized for a long time? What is the benefit of making a BMS for a BMS? I begin to understand why chatGPT fails.

Draw a picture... and format your code.

Hello r_shav

Welcome to the best Arduino forum ever :slight_smile:

Post a connection and wiring diagram for your project.

The use of described variable names is very helpful when studying the program, as you do not have to remember the cryptic names of the I/O pins.

int r1 = 3;
int r2 = 4;
int r3 = 5;
int r4 = 6;
int pcs = 8;

int b2s; // 2S battery voltage
int b3s; // 3S battery voltage
int bcs; // Battery charge sensor

int sv = 0; // System state     //
int bi = 0; // Battery indicator // both are int variable i was messing around to make the code run   

void setup() {
pinMode(r1, OUTPUT);
pinMode(r2, OUTPUT);
pinMode(r3, OUTPUT);
pinMode(r4, OUTPUT);
pinMode(pcs, INPUT);

Serial.begin(9600);
}

void loop() {
// Read sensors
int powerState = digitalRead(pcs);       // Check for ac mains 
b2s = analogRead(A0);                    // checkes for 2s battery voltage
b3s = analogRead(A1);                    // checkes for 3s battery voltage
bcs = analogRead(A2);                    // checkes for charges state of 3s battery

// -------- System State Priority Fix --------
if (b2s < 200) {                                            // if 2s battery goes below 200 to be considered critical and stop futher discharging.
    digitalWrite(r3, LOW);                                 // now 3s battery will take the load.
    sv = 1;
} else if (b3s < 200) {                                  // if 3s battery goes below 200  to consider critical and all devices connected to it must stop thus turn r3,r4 off.
    digitalWrite(r3, HIGH);
    bi = 2;
}

if (powerState == LOW || sv == 1) {                 // AC mains power on so no need for battery backup 
    digitalWrite(r1, HIGH);
    digitalWrite(r4, HIGH);
} 
if(powerState == HIGH){                         // Ac mains cut-off so need battery backup thus r1 and r4 relay will turn on
    digitalWrite(r1, LOW);
    digitalWrite(r4, LOW);
}

if (b3s < 680) {                           // 3s battery charging threshold when it reaches this voltage r2 must turn on connecting charger to the battery.
    digitalWrite(r2, LOW);
}

if (b3s > 800 && bcs > 900) {          // when 3ss battery voltage is high and when sensor leaad connected to the charger states battery charger r2 must turn off.
    digitalWrite(r2, HIGH);
}
}





//f the question arises about why I'm using two different battery packs—originally, I had purchased new batteries to build a 3S pack. 
//However, later, I found a broken power bank and salvaged its batteries to create a separate 2S pack.
//Additionally, I'm using a 300W CC-CV module for charging, just soldered the wire to one led and checking the voltage to determine charge state.

thank you xfpd for directions.

i am not able to upload images or pdf as i am new user.

Read 10 topics over 10 minutes to unlock uploading images.

schematic.zip (24.7 KB)
thank you again and attached are the schematic and prototyping setup i am using with wokwii.

Hey nice! Share the wokwi link, we take a peek.

a7

have already included json file in schematic.zip

Wokwi - Online ESP32, STM32, Arduino Simulator. link

You should save your code in your wokwi link.

Together, they "work" depending on your definition.

The switch controls the outer LEDs. Combinations of the three potentiometers control the inner LEDs.

1 Like

switch simulate power cutoff sensor
3 pots simulate battery voltages

My comments are not met to detract from your project nor to disparage your attempts.

But you are not building a UPS you are building a SPS. A stand-by-power supply. A UPS supplies the power 100% of the time and will switch to battery as input power when the AC goes away. The battery makes up for the missing AC power. A real UPS is expensive. I know because I had one powering a pick-and-place machine that would stop if there was ever a momentary AC power interruption.

A true UPS is also dangerous because if the AC power is interrupted because a fuse is opened up, the UPS will continue to supply power. It does not know the difference between a commercial power interruption and a circuit breaker opening because of an overload.

That said, your router and home automation server will most likely stop because of the AC power interruption caused by the delay in your UPS. That cannot be avoided by any of the available SPS devices unless your router and server have a power reserve that can carry over the brief interruption.

Have you tried a commercial device with your router and server to find out if your building such a device is worth it?

1 Like

Thank you! To be honest, I didn’t know it’s called SPS. Regarding your point about power interruption during an AC cutoff—it doesn’t actually happen in my setup. I’m already using Rev 1 of my system.

Here’s what happens: when the power cuts off, the relay quickly triggers, connecting the battery to the system and taking the entire load. During this transition, it doesn’t interrupt the WiFi or my server. However, the problem arises when the power resumes. The relay controlling the battery turns off, disconnecting the battery from the system, and both the server and WiFi halt for a second, causing them to reboot—which is not ideal for uninterrupted service.

To counter this, I initially tried using a bank of capacitors, but that didn’t help. The easiest solution was to use a millis() delay of 2 to 5 seconds. This ensures that when AC power resumes, the battery continues to supply power to both the WiFi and server for five more seconds before disconnecting, preventing the devices from rebooting.

I haven’t included millis() in this code yet, as I’m just testing basic relay control. I just need to control one relay using two different trigger conditions, but for the love of god, I still can’t get it to work! I’ve been trying for the last week now.

As I recall this is standard for commercial devices. Many time power restoration takes multiple tries! They reset a breaker only to find there is another branch line with a problem.