Battery messure

Hi

I have this schematic:
image

Battery measures to 3.88V when no load.

On serial I read 4.68V

Battery V sent to HA: 4.68

Code I use to calculate is:

PIN on ESP32 WROOM is VN

float analogPin = 39;  // Voltmeter VN=39 with the actual analog pin number you are using
void loop()float rawValue = analogRead(analogPin);  // Read the raw analog value
float volt = map(rawValue, 0, 1023, 0, 2100) / 1000.0;   

Any tip to get?

What is this number? How did you choose it?

1 Like

Hi

this is my note, not shure where I got it :
4200 mV volt over 1k + 1k ohm resistor gives volt/2=2100. Convert the raw value to voltage (0-1.85V)

UPDATE
I use a LIPI one cell battery 3.7V, vharge max 4.2V

4200 is 4.2V and I use a solar charger that charges to 4.2V. Maybe I should use a different number than 4200? like 3700

I did some AI, this gives me my original code.
I charge batt to 4.2 V with a solar + bms

But my readings, as mention, are different, actual batt is 3.88, reading is 4.68?
After voltage divider I messure 1.92v (2x 1.92 = 3.84)

If your voltage divider, consisting of two 1 kΩ resistors, gives a maximum voltage of 2100 mV (2.1V), and you want to use the map() function to scale an analog reading to this range, you should use the following parameters:

  • fromLow is the minimum value of your analog reading (usually 0).
  • fromHigh is the maximum value of your analog reading (usually 1023 for a 10-bit ADC).
  • toLow is 0 (as you want to map to 0V).
  • toHigh is 2100 (as this is the maximum voltage you can measure).

So, the map() function would look like this:

mappedValue = map(analogReading, 0, 1023, 0, 2100);

Here, analogReading is the analog value you want to scale within the range of 0 to 2100 based on your voltage divider's output. The map() function will linearly map this value to the new range. If analogReading is 0, the result will be 0 mV, and if analogReading is 1023, the result will be 2100 mV (2.1V), and values in between will be linearly interpolated between these two endpoints.

Any more you can help me with?

To start with, by default the ESP32 ADC output ranges from 0 to 4095 for input from 0 to 3.3V, so there is really nothing correct about your scaling or use of the map function.

The ADC is also nonlinear, and not very useful for making accurate measurements.

There are some tutorials on line on how to use the ESP32 ADC, so spend some time studying them.

I did some AI

Does this mean you asked chatGPT for advice? Good luck with that.

1 Like

Ahhh... sure, thanks

Does this mean you asked chatGPT for advice? Good luck with that.

Yes, AI is a good tool for me, but as you indicate, not a perfect tool. So me, and probably you/all, need to use it with care.

My code was previously for an ESP8266 board, so that explains the 1023.

I try update my code with this new information.

M

Hi, @Modesty

Have you Googled;

esp32 adc battery measure voltage

Doing some Google searches rather than chatGPT gospel, may help.

Thanks... Tom... :grinning: :+1: :coffee: :australia:

I I have searched for the solution according to @jremington

For your info, I use:
Batt charged 4.2V
Voltage divider 1k + 1k, gives 4.2*1/(1+1)= 2,1

wire.h
VN pin GPIO39 for battery ADC

I tried this code after reading a many approches i found on google

volt = (float)(analogRead(analogPin)) / 4095 x 2 x 3.3 x 1.1;

Reference voltage of the ESP32 3.3V and then finally, multiply that again by the ADC Reference Voltage of 1100mV.

The ESP32 reference voltage and ADC Reference Voltage of 1100mV (1.1 in formula) I struggle to understand or find

Real batt measured is 4.02
On serial volt is 4.26

Any more tip to get from you?

M

Given the nonlinearity of the ESP32 ADC, that is about what you could expect. Careful re-calibration of the ADC in the voltage range of interest should produce a more accurate result.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.