LilyGO / TTGO-T8-ESP32

esp32-i2s-sdcard-wav-player
99 stars 26 forks source link

Battery measurement #13

Open eried opened 4 years ago

eried commented 4 years ago

Hi!

I just wanted to add that it would be useful to have the following info in the repo:

1) PIN 35 measures the VBAT (or USB) with a resistor divider bundled in the board.

    #define PIN_VBAT 35
    int v = analogRead(PIN_VBAT); // This returns the battery level

2) VCC does not provide any power if we use the battery (I do not know why, but it would be useful to change this in the pcb for next revision and provide USB/VBAT)

paolovr70 commented 4 years ago

Eried I have T8 1.7.1 board. Analog read finds a value in PIN35 around 2430. This level is present both with BATTERY connected and also with NO BATTERY. Are you sure that it correspond to V.bat?

eried commented 4 years ago

Yes, in that case is USB voltage

paolovr70 commented 4 years ago

But what is the scale of 0-4095.measurement? Because lithium battery is 3.0-4.2v. Board and pins volt are maximum 3.3v and 0-4095 usually are for 0-3.3v. How.did you manage that?

eried commented 4 years ago

Well, I didnt exactly need the voltage value, just the reference (because the analogRead changes for every analog input in reference to VBAT, so the values are all different if you are on battery power vs USB... futhermore they decrease once the battery wears down... so it is a big issue if you are calibrating something based on the analog inputs)

So what I did is to map the range: https://github.com/eried/pidflight-lap-firmware/blob/master/pidflight_lap/rx5808.ino#L37

For a simple mapping I would use:

#define PIN_VBAT 35
double v = analogRead(PIN_VBAT)  / 4096 / 2 / 4.2 ; // This returns the voltage (max range / voltage divider / max battery voltage )
// voltage divider = in the PCB of the T8 you can see there is 2 equal resistors, hence 50% division
// max battery voltage = this is the output of the lipo charging circuitry before it gets to the 3.3 regulator... this circuit is before the ON/OFFswitch)

For a more accurate reading, I would calibrate a mapping with few known voltages and an polinomial interpolation in excel.

eried commented 4 years ago

Also, consider that while reading the serial monitor via USB you will always get the same VBAT. Create a sketch with a simple bluetooth serial and try with different batteries.