Read the battery voltage

Hi Seeedstudio-Team,

I ask the question directly to you because I have not found an answer or solution so far.
I want to monitor the input voltage again and again, and when the voltage reaches, for example, the 2.80 volts, the power led (pbPwLed) should flash.

But it only works if I use the USB voltage in addition to the 2x AA battery.
That means, if both voltages are present, the battery voltage is read out correctly (currently approx. 2.90 volts), but if I remove the USB voltage, then approx. 3.2 volts are measured.
Am I doing something wrong?

Any help would be appreciated. Thanks!

This is my code:

void ManageBattery() {
  batvalue = analogRead(PIN_VBAT);
  voltage = (batvalue * 3.3) / 1024;
  if (voltage < 2.80) {
    //Power-Led Blinken lassen
    if ((millis() - lastblink) > 250){
      digitalWrite(pbPwLed, !digitalRead(pbPwLed)); // LED wird ein- bzw. ausgeschaltet
      lastblink = millis();
    }
  }
  else{
    digitalWrite(pbPwLed, HIGH);  // turn ON the LED
  }
  Serial.print("Input Voltage = ");
  Serial.println(voltage); 
}
![IMG_20220614_112835|690x388](upload://9AAZlyfI0bxVR6BPW983hPYxZxg.jpeg)

Hi Seeedstudio-Team,
if you do not want to offer a solution, then at least say whether such a thing is possible.
I’m working on it for a few days, without success and do not want to make forever with it.
I thought that such a board (Seeed XIAO BLE) would be ideal for my project, if not, then I have to look for something else, too bad.
Thank you!

Sorry to be late.
I need to explain some complex circuit knowledge. What is read using analogRead() is the value of the ADC. When there is a USB cable supply, at this time the IC supply voltage is 3.3V and the components are operating at normal voltage, so the ADC input of 2.9V can be distinguished normally.
When the USB cable is unplugged, because the input voltage is less than the set ldo input voltage range, the ldo cannot step down and the output becomes 2.9V, at this time the IC can only work at 2.9V, it will be misjudged as full voltage (3.3V) supply, which is a phenomenon caused by illegal operation, we also do not allow such a low input voltage, although the IC can work normally, but the ADC has appeared The situation of inaccurate reading has occurred.
If you need to measure voltage, our advice is. Use a battery higher than 3.3V (not greater than 5V) and let him work with a voltage lower than 3.3V (or equal) then the alarm warns that it should be charged and used.

Hi @Citric ,

Thank you for the detailed and explanatory answer and please excuse my impatience.
Now it seems logical to me why my plans could not work.
I will now use the solution with the 3.7 volt Lipo battery.

Many thanks
Filippo