onehorse wrote:
Code: Select all
float VBAT = (127.0f/100.0f) * 3.30f * float(analogRead(34)) / 4096.0f; // LiPo battery
Serial.print("Battery Voltage = "); Serial.print(VBAT, 2); Serial.println(" V");
The ADC is 12-bit which is why the 3.3/4096 (ref voltage/max counts) conversion factor is in there. When the analog read returns 0, the battery voltage is 0, and when it returns 4095, the battery voltage is 4.2 V. The response of the ESP32 ADCs is apparently non-linear so you will have to calibrate the response and correct for maximum accuracy.
hi
wounder me to use better
ADC 12 Bit = 0b 1111 1111 1111 =
4095
3.3 / 4095 = 8,0586080586080586080586080586081e-4
if return 4095 (max RAW) :
4095 * 8,0586080586080586080586080586081e-4 = 3.3
if return 0:
0 * 8,0586080586080586080586080586081e-4 = 0
if return 2:
2 * 8,0586080586080586080586080586081e-4 = 0,00161172161172161172161172161172
---
3.3 / 4096 = 0,0008056640625
if return 4095 (max RAW):
4095 * 0,0008056640625 = 3,2991943359375 ( missing one part )
if return 0:
0 * 0,0008056640625 = 0
if return 2:
2 * 0,0008056640625 = 0,001611328125
btw:
4.2 Cell
4.2 / 4096 = 0,001025390625
Max RAW Return 4095 :
4095 * 0,001025390625 = 4,198974609375 ( missing one part )
4.2 Cell
4.2 / 4095 = 0,00102564102564102564102564102564
Max RAW Return 4095 :
4095 * 0,001025390625 = 4,2
Code: Select all
float VBAT = (127.0f/100.0f) * 3.30f * float(analogRead(34)) / 4095.0f; // LiPo battery
Serial.print("Battery Voltage = "); Serial.print(VBAT, 2); Serial.println(" V");
best wishes
rudi