Battery Guidance for the nRF52840 Sense

Have a look at the state of P0.17.

So use something like this…

#define CHARGE_PIN  (23) // Pointing directly to P0.17 (~CHG)
...

// Setup
  pinMode(CHARGE_PIN, INPUT); // ~CHG is a hardware input flag

// At a set period - eg 30 secs.
void check_charging()
{
  if (digitalRead(CHARGE_PIN) == LOW) {
    Serial.println("Power Connected: LiPo is actively charging.");
  } else {
    Serial.println("Not charging (Fully charged, running on battery, or unplugged).");
  }
}

Another small change I noticed when testing. The name appears as “XIAO_” :thinking:

Try this change…

  // Bluefruit.Advertising.addName(); // Causes name truncation -> "XIAO_" 
  Bluefruit.ScanResponse.addName();

Finally - add a Battery Voltage calculator…

void check_battery() {
  // Pull P0.14 LOW to connect the internal voltage divider to ground
  digitalWrite(VBAT_ENABLE, LOW);
  // Give the hardware circuit a tiny fraction of time to stabilize electrically
  delayMicroseconds(100);
  // Read raw analog step data from P0.31
  int rawADC = analogRead(PIN_VBAT);
  // Immediately pull P0.14 HIGH to isolate the circuit and prevent parasitic battery drain
  digitalWrite(VBAT_ENABLE, HIGH);

  // Convert raw 12-bit ADC values back into Volts based on our 2.4V reference
  // Formula: (Raw Value / Max Steps) * Reference Voltage
  float pinVoltage = (rawADC / 4095.0) * 2.4;

  // Multiply by the inverse resistor value (1510k / 510k = 2.96) to extract real battery voltage
  // float batteryVoltage = pinVoltage * 2.9607;
  float batteryVoltage = pinVoltage * 2.8797;  // GR Calibrated

  SERIAL.print("Battery Voltage: ");
  SERIAL.print(batteryVoltage, 2);  // Print out voltage with 2 decimal accuracy
  SERIAL.println(" V");
}