XIAO nrf52840 always 100% battery value

Hi I am using a Xiao BLE NRF 52840 to monitor the analog value of a load cell connected to pin A1. I also need to have access to the value of the supply voltage. I read the value from pin A0. Unfortunately, after disconnecting the USB from the device, the value is always 100%. Battery is connected to gold pins on a back of XIAO BLE NRF 52840. This is most likely because the reference value is the battery value. Is there any way to monitor the value of the battery from which the system is powered? Below is my program code.

#include <ArduinoBLE.h>
BLEService ledService("180A");
BLEByteCharacteristic switchCharacteristic("2A57", BLERead | BLEWrite);
BLEService loadService("9A48ECBA-2E92-082F-C079-9E75AAE428B1");
BLEStringCharacteristic loadLevelChar("2D2F88C4-F244-5A80-21F1-EE0224E80658", BLERead | BLENotify, 20);
BLEService batteryService("180F");
BLEUnsignedCharCharacteristic batteryLevelChar("2A19", BLERead | BLENotify);
const double Freq = 30;
unsigned long Cur_ms_Count;
unsigned long Last_ms_Count;
unsigned long T_ms_Count;
unsigned long millisoffset;
unsigned long zeromillis;
int flaga = 0;
String sText;
void setup() {
  Serial.begin(9600);
  T_ms_Count = 1000 / Freq;
  sText = "";
  Last_ms_Count = 0;

  //while (!Serial)
  //;

  pinMode(LED_BUILTIN, OUTPUT);
  if (!BLE.begin()) {
    while (1)
      ;
  }

  BLE.setLocalName("LoadMonitor");
  BLE.setAdvertisedService(loadService);
  loadService.addCharacteristic(loadLevelChar);
  BLE.addService(loadService);
  BLE.setAdvertisedService(batteryService);
  batteryService.addCharacteristic(batteryLevelChar);
  BLE.addService(batteryService);
  BLE.setAdvertisedService(ledService);
  ledService.addCharacteristic(switchCharacteristic);
  BLE.addService(ledService);

  BLE.advertise();
  Serial.println("Bluetooth device active, waiting for connections...");
}

void loop() {
  BLEDevice central = BLE.central();

  if (central) {

    digitalWrite(LED_BUILTIN, HIGH);

    while (central.connected()) {

      if (switchCharacteristic.written()) {
        switch (switchCharacteristic.value()) {
          case 01:
            flaga = 1;
            break;
          case 02:
            flaga = 2;
            millisoffset = millis();
            break;
          default:
            flaga = 0;
            break;
        }
      }
      while (flaga == 0) {

        break;
      }
      while (flaga == 1) {
        Cur_ms_Count = millis();
        if (Cur_ms_Count - Last_ms_Count >= T_ms_Count) {
          int Battery = analogRead(A0);
          int BatteryLevel = map(Battery, 0, 1023, 0, 100);
          batteryLevelChar.writeValue(BatteryLevel);
          Cur_ms_Count = millis();
          break;
        }
      }
      while (flaga == 2) {
        Cur_ms_Count = millis();
        if (Cur_ms_Count - Last_ms_Count >= T_ms_Count) {
          zeromillis = Cur_ms_Count - millisoffset;
          int Load = analogRead(A1);
          int LoadLevel = map(Load, 0, 1023, 0, 100);
          int Battery = analogRead(A0);
          int BatteryLevel = map(Battery, 0, 1023, 0, 100);
          sText = String(zeromillis) + '\t' + String(LoadLevel);
          loadLevelChar.writeValue(sText);
          batteryLevelChar.writeValue(BatteryLevel);
          Last_ms_Count = Cur_ms_Count;
          break;
        }
      }
    }
    digitalWrite(LED_BUILTIN, LOW);
  }
}

Yes, using a fixed voltage reference (I have no experience with this microcontroller and don't know if it has an internal voltage reference). Or you could use an extra analog input pin to read a fixed voltage from a zener diode or even an LED (perhaps red because they have the lowest dropout voltage) and with a little math you could calculate the battery voltage. I see you're using this

way to calculate battery capacity, which I think is wrong.

Could be worth asking in the support forum that SEEED have specifically for the XIAO, for instance;

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