Advertisement
JonD1988

BatteryVoltageTesterRev0

Dec 13th, 2022
1,253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //This program goes hand in hand with ESP32RCVehiclePowerCircuitSchematicRev0.  This is designed to test the battery voltage for two 4.2 V LiIon batteries which have been placed in series
  2. #define R1 100 //Resistor 1 value in kΩ - Used to Measure Combined Battery 1 & Battery 2 Voltage
  3. #define R2 10 //Resistor 2 value in kΩ - Used to Measure Combined Battery 1 & Battery 2 Voltage
  4. #define R3 100 //Resistor 3 value in kΩ - Used to Measure Battery 2 Voltage
  5. #define R4 10 //Resistor 4 value in kΩ - Used to Measure Battery 2 Voltage
  6. #define ADC_REFERENCE 1100
  7. #define volt1P 32
  8. #define volt2P 33
  9.  
  10. float volt1, volt2; //Battery 1 & 2 Combined Voltage = volt1. Battery 2 Voltage = volt2
  11. float volt1S, volt2S; //Voltage 1 and 2 Scaled to Account for Voltage Dividers
  12. float batt1V; //Battery 1 Voltage after Battery 2 Voltage has been subtracted out using KVL
  13.  
  14. void setup() {
  15.   Serial.begin(115200);
  16.   pinMode(volt1P, INPUT);
  17.   pinMode(volt2P, INPUT);
  18. }
  19.  
  20. void loop() {
  21.   volt1 = analogRead(volt1p)*(ADC_REFERENCE/4096);
  22.   volt2 = analogRead(volt2p)*(ADC_REFERENCE/4096);
  23.   volt1S = (volt1*(R1+R2))/R2; //Volt 1 Scaled to the Appropriate Value using the VDR
  24.   volt2S = (volt2*(R3+R4))/R4; //Volt 2 Scaled to the Appropriate Value using the VDR
  25.   batt1V = volt1S - volt2S; //Battery 1 Voltage
  26.   Serial.println("Battery 1 Voltage: ");
  27.   Serial.print(batt1V);
  28.   Serial.println("Battery 2 Voltage: ";
  29.   Serial.print(volt2S);
  30. }
  31. //References
  32. //Reference 1 https://en.ovcharov.me/2020/02/29/how-to-measure-battery-level-with-esp32-microcontroller/
  33. //Reference 2 https://docs.espressif.com/projects/esp-idf/en/v4.4/esp32/api-reference/peripherals/adc.html
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement