View Single Post
 
Old 08-04-2018, 06:28 PM
axxelxavier axxelxavier is offline
Member
 
Join Date: Dec 2011
Posts: 70
The Arduino code for Battery swapper

Some final mentions:
- for the sake of simplicity, you can use a power bank, so you don`t need components from power section;
- the setup is incomplete, because does not contain also the booster, as seen in schematic, but this can be easily added;
- pay attention to wiring the relay board, some instruction are in coding file (.ino);
- for voltage sensor, You can use something like this, or you can do your own board, like myself (I used high precision resistors: 2 x 2K (single 4k, if you find), and 1 K resistor for each board));
- all was mounted on a bigger test board, using 0,5 and 1 mm copper wire for soldering;
- connection of the sensitive boards (arduino, the future LCD Nokia screen) was made with male - to female connectors, so the components can be exchanged or used for other projects.
Feel free to ask anything.

L.E.
It seems like I can't upload the coding file, so here is the code (copy the code in a notepad and save it with the .ino extension):

//2BGS_Battery_swapper V_1, first developed by axxelxavier (Teodor), from energeticforum.com.
//Based on miscelaneous codes from web, among my personal contribution (mharmon12 for digitalWrite HIGH & LOW inverted; more to come :-) )
//More contributors are welcomed, please do not remove this ”history status”.

int Relay1 = 5; //pin 5 will be connected to the relay1 (numbering the relays from copper traces, on PCB layout)
int Relay2 = 4; //pin 4 will be connected to the relay2
int Relay3 = 3; //pin 3 will be connected to the relay3
int Relay4 = 2; //pin 2 will be connected to the relay4
#define ON 0 //invert phase of digital writing for correct operation
#define OFF 1 //the same

void setup() {//Instruction for 4 Relay board = External „+” voltage goes to JD-VCC and „-” to Ground. VCC from relay board goes to 5 volt Arduino Pin.
pinMode(Relay1, OUTPUT); //define digital pin 5 from Relay 1 as output
pinMode(Relay2, OUTPUT); //define digital pin 4 from Relay 2 as output
pinMode(Relay3, OUTPUT); //define digital pin 3 from Relay 3 as output
pinMode(Relay4, OUTPUT); //define digital pin 2 from Relay 4 as output
Serial.begin(9600);//initialize serial print //good for debugging
}

void loop() {
int Batt1 = analogRead(A0);// read the input of Battery 1
int Batt2 = analogRead(A1);// the same for Battery 2
float voltage1 = Batt1 * (5.0 / 1023.0)*4.75; //last number is to convert voltage to real value, use it to fine tune your readings. It must be changed for each battery.
float voltage2 = Batt2 * (5.0 / 1023.0)*4.75; //my voltage divisors use 1% precision resistors, so the results are similar, but feel free to test each voltage divisor and tune up for best performance
Serial.print("Battery 1: ");Serial.println(voltage1);
delay(1000);
Serial.print("Battery 2: ");Serial.println(voltage2);
delay(1000);
if (voltage1<voltage2){ //Relay 1 and 3 from Battery 1 will close, Battery 2 is now active
digitalWrite(Relay1, OFF);
digitalWrite(Relay3, OFF);
digitalWrite(Relay2, ON);
digitalWrite(Relay4, ON);
Serial.println("Battery 1 < Battery 2");
}else{ //Relay 2 and 4 from Battery 2 will close, Battery 1 is now active
digitalWrite(Relay1, ON);
digitalWrite(Relay3, ON);
digitalWrite(Relay2, OFF);
digitalWrite(Relay4, OFF);
Serial.println("Battery 2 < Battery 1");
}
}
//the code it is still incomplete, must be implemented in the future:
//1. some safety features
//2. when voltage1 = voltage2, all relays should shut down
__________________
 

Last edited by axxelxavier; 08-04-2018 at 06:32 PM.
Reply With Quote