Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //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
- #define R1 100 //Resistor 1 value in kΩ - Used to Measure Combined Battery 1 & Battery 2 Voltage
- #define R2 10 //Resistor 2 value in kΩ - Used to Measure Combined Battery 1 & Battery 2 Voltage
- #define R3 100 //Resistor 3 value in kΩ - Used to Measure Battery 2 Voltage
- #define R4 10 //Resistor 4 value in kΩ - Used to Measure Battery 2 Voltage
- #define ADC_REFERENCE 1100
- #define volt1P 32
- #define volt2P 33
- float volt1, volt2; //Battery 1 & 2 Combined Voltage = volt1. Battery 2 Voltage = volt2
- float volt1S, volt2S; //Voltage 1 and 2 Scaled to Account for Voltage Dividers
- float batt1V; //Battery 1 Voltage after Battery 2 Voltage has been subtracted out using KVL
- void setup() {
- Serial.begin(115200);
- pinMode(volt1P, INPUT);
- pinMode(volt2P, INPUT);
- }
- void loop() {
- volt1 = analogRead(volt1p)*(ADC_REFERENCE/4096);
- volt2 = analogRead(volt2p)*(ADC_REFERENCE/4096);
- volt1S = (volt1*(R1+R2))/R2; //Volt 1 Scaled to the Appropriate Value using the VDR
- volt2S = (volt2*(R3+R4))/R4; //Volt 2 Scaled to the Appropriate Value using the VDR
- batt1V = volt1S - volt2S; //Battery 1 Voltage
- Serial.println("Battery 1 Voltage: ");
- Serial.print(batt1V);
- Serial.println("Battery 2 Voltage: ";
- Serial.print(volt2S);
- }
- //References
- //Reference 1 https://en.ovcharov.me/2020/02/29/how-to-measure-battery-level-with-esp32-microcontroller/
- //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