Heltec battery power example not working

Hello,

I used this example to read the battery status of my Lora V2:

But it reads always ~3100mV:
With only USB connected
With no USB and a full LiPo
With no USB and an nearly empty LiPo
With USB and a full/empty LiPo

What am I doing wrong?

image
It should be 37.

Yes, I saw that and I had already changed that to adcAttachPin(37).
Same results. Always 3000 to 3100mv.
Any other suggestions?

I don’t know when you bought the node. If you are an old node, then the pins should be 13 pins.

it was a new device and it apparently was pin 37.
But the example is wrong. The culprit is this line:
float XS = 0.00225;
The multiplier is not correct.
In this thread user “wombat” wrotes about a 320K/100K voltage divider:

But 320/100=3.2
So if I use
float batteryVoltage=3.2*analogRead(37);
it seems that I get correct values:
Around 4.1V with USB connected, 3.8V with LiPo connected.

Here is something that is working for me with the LoRa32 board. I’m using a divider of 338 which is giving me more accurate results than 320:

float getBatteryVoltage(int nbMeasurements) {
    float voltageDivider = 338;
    // int readMin = 1080; // -> 338 * 3.2 // If you want to draw a progress bar, this is 0%
    // int readMax = 1420; // -> 338 * 4.2 // If you want to draw a progress bar, this is 100%

    // Take x measurements and average
    int readValue = 0;
    for (int i = 0; i < nbMeasurements; i++) {
        readValue += analogRead(37);
        delay(100);
    }
    
    float voltage = (readValue/nbMeasurements)/voltageDivider;
    return voltage;
}