Arduino Resetting when switching between two batteries power source

I have a system that it is connected to a server by using a wifi. im currently using arduino uno wifi rev 2, it is powered by either of the two batteries. and there is a website that has two buttons “Battery 1” and “Battery 2” when i click the button 1 it will switch the relay and connects to the battery and vice versa.

now the problem that i’m encountering is that when it was switch once, (i think) the arduino resets the whole code. but i didnt see the relay being shut down nor the arduino.

This is my circuit diagram

I guess you switch batteries when one runs low?

With some diodes you could switch in A before switching out B.

Idahowalker:
I guess you switch batteries when one runs low?

yes, you’re right

Idahowalker:
With some diodes you could switch in A before switching out B.

yes. but it will take some time to create the circuit. I’m planning to use a relay to do the switching. but unfortunately there’s a problem when switching occurs

mmm. it seems the imgur picture cant post.

here i uploaded the circuit diagram.

With the relay it’s a timing issue. If you switch at the same exact time, there may be a disconnect to power. I would let them overlap slightly Try a couple mS. It is possible to do since you are using 2 relays.

wolframore: With the relay it's a timing issue. If you switch at the same exact time, there may be a disconnect to power. I would let them overlap slightly Try a couple mS. It is possible to do since you are using 2 relays.

yes your right im switching at the same exact time. what do you mean about overlap? would it be shorted the circuit?

This is the code

if(Battery_using == "Battery_1"){
  digitalWrite(relayPin1, HIGH);
  digitalWrite(relayPin2, HIGH);
}else if (Battery_using == "Battery_2"){
  digitalWrite(relayPin1, LOW);
  digitalWrite(relayPin2, LOW);

}

did you see my circuit diagram

did you see my circuit diagram?

No all I saw was a piece of Fritzing crap showing a physical layout, no schematic at all.

The problem is that the relay contacts are break before make and so when they switch over there is no power for a few milliseconds. This causes a reset.

You could try a 220uF or larger capacitor across the Arduino’s supply to stop it being unpowered.

since the batteries are in parallel,you need to turn both on, wait couple mS then turn off bad one. Make sure it's a small window or you will lose power to the bad battery.

Why does the code looks like you're turning both on (HIGH) then turning both pins off?

Grumpy_Mike: No all I saw was a piece of Fritzing crap showing a physical layout, no schematic at all.

The problem is that the relay contacts are break before make and so when they switch over there is no power for a few milliseconds. This causes a reset.

You could try a 220uF or larger capacitor across the Arduino’s supply to stop it being unpowered.

im sorry, my bad. it was a physical layout, i'll find the schematic layout. You've said i could try a capacitor, but in DC, the capacitor is a open connection.

wolframore: Why does the code looks like you're turning both on (HIGH) then turning both pins off?

initially, the system will use the battery 1, battery 1 is connected to the normally closed in relay 1 while the battery 2 is connected to the normally open in relay 2. when the switching occurs. the relay 1 will now be open, hence battery 1 will be disconnected, while the relay 2 will be now closed then the battery 2 will be connected to the system.

And that’s why we ask for schematics. You realize that energizing relays wastes battery power also? Better to use solidstate or latching relays.

You've said i could try a capacitor, but in DC, the capacitor is a open connection.

Across the power supply of the Arduino, NOT in series with it.

It will act as a temporary reservoir for the power while the batteries are disconnected.

Let me know if that works

When the battery has ran low why do a switch over to a new battery. Is it so that you can charge the drained battery?

Have you entered into your favorite search engine the following words "battery switchover circuit'?

The LTC44XX chip family may do the thing.

Idahowalker:
When the battery has ran low why do a switch over to a new battery. Is it so that you can charge the drained battery?

Have you entered into your favorite search engine the following words "battery switchover circuit’?

The LTC44XX chip family may do the thing.

yes you’re right.
not yet but im currently looking into it. but i’ve search relay but not in low batteries. sometimes they’re talking about high voltages batteries.

this is the schematic. sorry its late.

Yeah that won’t work. You are cutting off power as it switches. So you either need to have a huge capacitor that can supply power during the switchover or you will need to use two relays and overlap it.

wolframore: Yeah that won’t work. You are cutting off power as it switches. So you either need to have a huge capacitor that can supply power during the switchover or you will need to use two relays and overlap it.

yeah it seems like. but i didn't see the arduino's light being turning off. anyway Grumpy_mike suggested 220uF capacitor, what volts should i use?

what volts should i use?

Any voltage 20% or more higher than the voltage of the batteries you want to switch.

The Arduino is now working! it doesn't scramble the codes anymore. As you guys suggested I used a capacitor across the circuit before the power enters Arduino. I used the 1000uf 10v capacitor instead. Thank you for your help :)

anyway, does anyone know the duration/time of switching of the relays/springs?