How do I get an info about battery percentage that powering up the Arduino

Hello, I’m using the XIAO nRF52840 with a 3.7v li-po battery connected(see the photo).
I’m using the board with an app that I build via Bluetooth and I want to see the battery precentage and also get an indication if its charging or not. I searched the internet for help but didnt find something helpful, hope you guys can help me.
Thanks!!!

Hi,Smartiscar:
SeeedStudio doesn’t have a wiki tutorial for this, so I’m sorry I can’t help you because this is secondary development

I have been reading that BT and reading voltage value are sort of conflicting, with BT stopping every time voltage is read.
Let us know pls if you find a solution

I found a solution in this forum. there is the link:

I’ve a issue with my XIAO nRF52840 board and Li-Po battery setup. While working on a project that involves Bluetooth communication, I needed to monitor the battery percentage and receive charging status indications. I scoured the forum and documentation, but I couldn’t find a straightforward solution or example code specific to my hardware configuration. I can’t figure out how to implement this feature effectively.

For the XIAO nRF52840, battery voltage and charge status can be read without special hardware.

//----------------------------------------------------------------------------------------------
//Board Library : Seeed nRF52 Borads 1.1.1
//Board Select  : Seeed nRF52 Borads / Seeed XIAO nRF52840 Sense
//----------------------------------------------------------------------------------------------
#include <Arduino.h>
#include <Adafruit_TinyUSB.h>   // for Serial.print()

//Arduino15\packages\Seeeduino\hardware\nrf52\1.1.4\variants\Seeed_XIAO_nRF52840_Sense\variant.cpp
#define PIN_VBAT        (32)  // D32 battery voltage
#define PIN_VBAT_ENABLE (14)  // D14 LOW:read anable
#define PIN_HICHG       (22)  // D22 charge current setting LOW:100mA HIGH:50mA
#define PIN_CHG         (23)  // D23 charge indicatore LOW:charge HIGH:no charge

void setup() 
{
  Serial.begin(115200);
  while(!Serial);
  
  pinMode(PIN_VBAT, INPUT);
  pinMode(PIN_VBAT_ENABLE, OUTPUT);
  pinMode(PIN_HICHG, OUTPUT);
  pinMode(PIN_CHG, INPUT);

  digitalWrite(PIN_VBAT_ENABLE, LOW); // VBAT read enable
  digitalWrite(PIN_HICHG, LOW);       // charge current 100mA
  
  // initialise ADC wireing_analog_nRF52.c:73
  analogReference(AR_DEFAULT);        // default 0.6V*6=3.6V  wireing_analog_nRF52.c:73
  analogReadResolution(12);           // wireing_analog_nRF52.c:39
 
}

void loop() 
{
  int vbatt = analogRead(PIN_VBAT);
  Serial.print(vbatt, HEX);
  Serial.print("    ");
  Serial.print(2.961 * 3.6 * vbatt / 4096);   // Resistance ratio 2.961, Vref = 3.6V 
  Serial.print("    ");
  Serial.println(digitalRead(PIN_CHG));       // 0:charge, 1:discharge 

  delay(1000);
}
1 Like

Hi there, Kamrynl
Have you used or considered the BLE,battery service i.e.
"BLEService batteryService(“180F”);// Bluetooth® Low Energy Battery Service
" BLEUnsignedCharCharacteristic batteryLevelChar(“2A19”, BLERead | BLENotify);
I have and it works well reports the battery level of the device very well.
check out this link :point_right:XIAO_BLE_Sense(mbed 2.7.2) battery charge and voltage monitor, analogRead(P0_31) does not work - #6 by PJ_Glasso
shows the advertised BLE service on NRF mobile app and


later got it working in MIT AI .
HTH
GL :slight_smile: PJ

Hi @philgib,
Do I understand correctly that analog reads from any pin will cause BT to “stop” ? What does it mean, that connection drops or gets buggy, or just that BT communications are temporarily paused ?

@msfujino gave an example that seems to work, but they don’t mention possible BLE issues.