Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Be sure you have a voltage divider 4:1 to measure the battery voltages. Battery--[4k]----[1k]--GND
- * |
- * [Arduino]
- Sens:
- Battery 1 is (thru voltage divider) on Pin A0
- Battery 2 is (thru voltage divider) on Pin A1
- Relais for Batteries:
- On Digital 2: Relais to connect Battery 1 to load
- On Digital 3: Relais to connect Battery 1 to charger
- On Digital 4: Relais to connect Battery 1 to load
- On Digital 5: Relais to connect Battery 1 to charger
- */
- /* Constants and Inits */
- unsigned int rereads = 0; // we want to make a multiple-read on the battery to be sure it is not only a spike...
- float voltagesum = 0; // The sum of 100x reading of battery in float
- int b1l = 2; // Definitions for pins
- int b1c = 3; // Definitions for pins
- int b2l = 4; // Definitions for pins
- int b2c = 5; // Definitions for pins
- /* Setup */
- float batteryempty = 11.8; // Empty battery according to https://www.tobesure.com/2018/11/06/what-is-the-voltage-of-a-car-battery-harrisburg-pa/
- void setup() {
- // put your setup code here, to run once:
- pinMode(A0, INPUT); // sets the analog A0 to input --> Sens for battery 1
- pinMode(A1, INPUT); // sets the analog A1 to input --> Sens for battery 2
- pinMode(b1l, OUTPUT); // sets the digital 2 to output --> Control of relais for battery 1, Dischargepath
- pinMode(b1c, OUTPUT); // sets the digital 3 to output --> Control of relais for battery 2, Dischargepath
- pinMode(b2l, OUTPUT); // sets the digital 4 to output --> Control of relais for battery 1, Chargepath
- pinMode(b2c, OUTPUT); // sets the digital 5 to output --> Control of relais for battery 2, Chargepath
- digitalWrite(b2l, LOW); // Battery 1 OFF for charge
- digitalWrite(b1l, HIGH); // Battery 1 ON for discharge
- digitalWrite(b2c, HIGH); // Battery 2 ON for charge
- digitalWrite(b1c, LOW); // Battery 2 OFF for discharge
- }
- void loop() {
- float bat1voltage = 5 * analogRead(A0);
- if (bat1voltage <= batteryempty) { // Battery 1 triggered a low indicator!
- for (int i = 0; i <= 100; i++) { // Measure over 2sec every < 20ms
- voltagesum = voltagesum + analogRead(A0);
- delay(17); // Allow 1ms for analog read... have it a bit faster than 1per20ms to avoid powerline-frequencies
- }
- bat1voltage = 1.0 * voltagesum / 100; // * 1.0 to force float
- voltagesum = 0;
- // Now we have an average of the battery-voltage over around 2secs.
- if (bat1voltage <= batteryempty) {
- digitalWrite(b1c, HIGH); // Battery 1 ON for charge
- digitalWrite(b1l, LOW); // Battery 1 OFF for load/discharge
- digitalWrite(b2c, LOW); // Battery 2 OFF for charge
- digitalWrite(b2l, HIGH); // Battery 2 ON for load/discharge
- continue; // Skips rest of mainloop and starts a new iteration
- }
- }
- float bat2voltage = 5 * analogRead(A1);
- if (bat2voltage <= batteryempty) { // Battery 2 triggered a low indicator!
- for (int i = 0; i <= 100; i++) { // Measure over 2sec every < 20ms
- voltagesum = voltagesum + analogRead(A0);
- delay(17); // Allow 1ms for analog read... have it a bit faster than 1per20ms to avoid powerline-frequencies
- }
- bat2voltage = 1.0 * voltagesum / 100; // * 1.0 to force float
- voltagesum = 0;
- // Now we have an average of the battery-voltage over around 2secs.
- if (bat2voltage <= batteryempty) {
- digitalWrite(b1c, LOW); // Battery 1 OFF for charge
- digitalWrite(b1l, HIGH); // Battery 1 ON for load/discharge
- digitalWrite(b2c, HIGH); // Battery 2 ON for charge
- digitalWrite(b2l, LOW); // Battery 2 OFF for load/discharge
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement