KRITSADA icon

Battery Measure Lead Acid Voltage

KRITSADA | PRO | 02/24/21 11:18:57 PM UTC | 0 ⭐ | 1422 👁️ | Never ⏰ | []
C |

4.29 KB

|

None

|

0 👍

/

0 👎

//Ratios between voltage dividers
// R# = (R1+R2)/R2
float R1 = 3.128;
float R2 = 6.255;
float R3 = 9.383;
float R4 = 12.511;
float R5 = 15.638;
float R6 = 18.766;
float R7 = 21.894;
float R8 = 25.021;
 
// % Variables
int P8 = 0;
int P7 = 0;
int P6 = 0;
int P5 = 0;
int P4 = 0;
int P3 = 0;
int P2 = 0;
int P1 = 0;
 
long readVcc() {
  // Read 1.1V reference against AVcc
  // set the reference to Vcc and the measurement to the internal 1.1V reference
#if defined(__AVR_ATmega32U4__) || defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
  ADMUX = _BV(REFS0) | _BV(MUX4) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
#elif defined (__AVR_ATtiny24__) || defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__)
  ADMUX = _BV(MUX5) | _BV(MUX0) ;
#else
  ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
#endif
 
  delay(2); // Wait for Vref to settle
  ADCSRA |= _BV(ADSC); // Start conversion
  while (bit_is_set(ADCSRA, ADSC)); // measuring
 
  uint8_t low  = ADCL; // must read ADCL first - it then locks ADCH
  uint8_t high = ADCH; // unlocks both
 
  long result = (high << 8) | low;
 
  result = 1125300L / result; // Calculate Vcc (in mV); 1125300 = 1.1*1023*1000
  return result; // Vcc in millivolts
}
 
void setup() {
  // initialize serial port
  Serial.begin(9600);
}
 
void loop() {
 
  
  //Battery Voltage
  float B1Voltage = 0;
  float B2Voltage = 0;
  float B3Voltage = 0;
  float B4Voltage = 0;
  float B5Voltage = 0;
  float B6Voltage = 0;
  float B7Voltage = 0;
  float B8Voltage = 0;
 
 
  //converted signal in 5v domain
  float RealBat1V = 0;
  float RealBat2V = 0;
  float RealBat3V = 0;
  float RealBat4V = 0;
  float RealBat5V = 0;
  float RealBat6V = 0;
  float RealBat7V = 0;
  float RealBat8V = 0;
 
 
  //Analog channel raw value
  float VolBat1 = 0;
  float VolBat2 = 0;
  float VolBat3 = 0;
  float VolBat4 = 0;
  float VolBat5 = 0;
  float VolBat6 = 0;
  float VolBat7 = 0;
  float VolBat8 = 0;
 
  float vccValue = readVcc() / 1000.0; //Arduino refence voltage
 
 
  // Reading battery voltages
  VolBat8 = analogRead(A8);
  VolBat7 = analogRead(A7);
  VolBat6 = analogRead(A6);
  VolBat5 = analogRead(A5);
  VolBat4 = analogRead(A4);
  VolBat3 = analogRead(A3);
  VolBat2 = analogRead(A2);
  VolBat1 = analogRead(A1);
 
 
  //Converting raw value in 5v domian
  RealBat1V = VolBat1 * vccValue / 1024.0;
  RealBat2V = VolBat2 * vccValue / 1024.0;
  RealBat3V = VolBat3 * vccValue / 1024.0;
  RealBat4V = VolBat4 * vccValue / 1024.0;
  RealBat5V = VolBat5 * vccValue / 1024.0;
  RealBat6V = VolBat6 * vccValue / 1024.0;
  RealBat7V = VolBat7 * vccValue / 1024.0;
  RealBat8V = VolBat8 * vccValue / 1024.0;
 
 
  //Calculating actual voltages
  B8Voltage = RealBat4V * R8;
  B7Voltage = RealBat3V * R7 - B8Voltage;
  B6Voltage = RealBat2V * R6 - B8Voltage - B7Voltage;
  B5Voltage = RealBat1V * R5 - B8Voltage - B7Voltage - B6Voltage;
  B4Voltage = RealBat4V * R4 - B8Voltage - B7Voltage - B6Voltage - B5Voltage;
  B3Voltage = RealBat3V * R3 - B8Voltage - B7Voltage - B6Voltage - B5Voltage - B4Voltage;
  B2Voltage = RealBat2V * R2 - B8Voltage - B7Voltage - B6Voltage - B5Voltage - B4Voltage - B3Voltage;
  B1Voltage = RealBat1V * R1 - B8Voltage - B7Voltage - B6Voltage - B5Voltage - B4Voltage - B3Voltage - B2Voltage;
 
 
  //Convert to %
  // map(value, fromLow, fromHigh, toLow, toHigh)
  P8 = map(B8Voltage, 0, 12, 0, 100);
  P7 = map(B7Voltage, 0, 12, 0, 100);
  P6 = map(B6Voltage, 0, 12, 0, 100);
  P5 = map(B5Voltage, 0, 12, 0, 100);
  P4 = map(B4Voltage, 0, 12, 0, 100);
  P3 = map(B3Voltage, 0, 12, 0, 100);
  P2 = map(B2Voltage, 0, 12, 0, 100);
  P1 = map(B1Voltage, 0, 12, 0, 100);
 
  
  //Print voltages on serial monitor
  Serial.print("Battery-1 Voltage=");
  Serial.println(B1Voltage);
  Serial.print("Battery-2 Voltage=");
  Serial.println(B2Voltage);
  Serial.print("Battery-3 Voltage=");
  Serial.println(B3Voltage);
  Serial.print("Battery-4 Voltage=");
  Serial.println(B4Voltage);
  Serial.print("Battery-5 Voltage=");
  Serial.println(B5Voltage);
  Serial.print("Battery-6 Voltage=");
  Serial.println(B6Voltage);
  Serial.print("Battery-7 Voltage=");
  Serial.println(B7Voltage);
  Serial.print("Battery-8 Voltage=");
  Serial.println(B8Voltage);
 
  delay(2000); //2 Seconds delay and then start again
}

Comments