XIAO_BLE_Sense(mbed 2.7.2) battery charge and voltage monitor, analogRead(P0_31) does not work

nengyu thanks,
We are waiting for your resolution.

1 Like

Thank you - been banging my head all day - this patch fixes the problem!

Nengyu please do resolve this.

Everytime the library is updated we go back to the beginning with this issue. So much so I’ve added a self-reminder to the header of my Xiao sketches!

1 Like

Thank you for your attention to this. Mr nengyu,

Just recieved 3 more Sense devices… (15)
I look forward to a Competent fix for this ongoing issue. Mr. Nengyu​:wink::ok_hand:



Does anyone have an Arduino sketch that can read the battery voltage? When I tried the above (with arduino and serial includes and using the raw pin numbers, not “PO_” becuase Arduino doesn’t see those as proper pins) it tells me that pins 14 and 31 don’t exist. I did add them to the pins_arduino.h file as “D14” and “A31”, but that doesn’t work either, giving me a complaint from the gpio hal:

entry 0x403ce000
[    94][E][esp32-hal-gpio.c:102] __pinMode(): Invalid pin selected
Build:Feb  7 2021
rst:0x8 (TG1WDT_SYS_RST),boot:0x8 (SPI_FAST_FLASH_BOOT)

This is using the XIAO_ESP32C3 board in Arduino directly. I don’t know what is MBED actually, which I think MFUJINO is reporting the error in. But, it’s possible the same error is propagated to the Arduino variants. Thanks!

Hi braddo,
The problem in this post only occurs with the combination of the board “Seeed XIAO_nRF52840” and the board library “Seeed nRF52 mbed-enabled Borads 2.7.2/2.8.1/2.9.0”.
For more information about your board “XIAO_ESP32C3”, please see the following link.

To all those who care about this:
Sorry for the long wait! We finally have the R&D resources to do the testing in question, and we’ll have it done within the week.

That’s GREAT News ! Well We are ALL here waiting to help , If I may say So.
You have such a Great product in the Xiao Line.
Lots of FAN Folks TOO, so let’s make some good end user products with your help.
Thank You.
GL :wink:

Hi Seed-Liu,
Will the following issues also be considered in this upgrade?


Tool Seeeduino:openocd@0.11.0-arduino2 already installed
Tool Seeeduino:arm-none-eabi-gcc@7-2017q4 already installed
Tool Seeeduino:bossac@1.9.1-arduino2 already installed
Tool Seeeduino:dfu-util@0.10.0-arduino1 already installed
Downloading packages
Replacing platform Seeeduino:mbed@2.9.0 with Seeeduino:mbed@2.9.1
Uninstalling Seeeduino:mbed@2.9.0
Platform Seeeduino:mbed@2.9.0 uninstalled
Uninstalling Seeeduino:arm-none-eabi-gcc@7-2017q4, tool is no more required
Uninstalling Seeeduino:bossac@1.9.1-arduino2, tool is no more required
Uninstalling Seeeduino:dfu-util@0.10.0-arduino1, tool is no more required
Uninstalling Seeeduino:openocd@0.11.0-arduino2, tool is no more required
Configuring platform.
Platform Seeeduino:mbed@2.9.1 installed

i’m hopeful.
GL :crossed_fingers:

FAIL. I ran my code after a fresh reboot and compile with the new… NO-GO no Battery Level get’s passed to the BLE peripheral to the Nrf App Battery level service value.
if I edit the file with the msfujino’s supper patch it WORKS again. So in baseball 2.9.1 strikes OUT! imo
NEXT!!! :dizzy_face:
may have to get ChatGPT to fix it… LOL :stuck_out_tongue_winking_eye:

v2.9.1 has been released, but
analogRead(P0_31) returns 0xFFFFFFFFFF and analogRead(PIN_VBAT) hangs the IDE.
It is not usable at all.

analogRead(PIN_VBAT) hangs the IDE ----> GREEN LED ON/OFF hangs the IDE

What the SAM HILL is going on? I 'm beginning to wonder if they READ? do they even test the stuff? Looks like it was a bunch of deletes from the package is all, Idunno but I’m Tired… :sleeping:
So what every time they roll something out it’s going to break my Stuff. :rage:
They better get it sorted. :face_with_symbols_over_mouth:
GL :slight_smile:

I’m very sorry about this, the engineer told me he tested it before the update. We will test again immediately. :smiling_face_with_tear:

msfujino’s patch is great, but it modifies the code in the /core/arduino directory instead of /variant, and trust me, if your code is not aligned with ArduinoCore-mbed, compatibility will be an issue. :slightly_smiling_face:
In 2.9.1, PIN_VBAT works, while P0_31 still does not. This was a serious mistake, for which I am sorry.
We will fix it soon (chatgpt go for it!

Hi, you are right, analogRead(P0_31) is still not available in v2.9.1, but analogRead(PIN_VBAT) should work. Can I have a look at your code?

In v2.9.1,analogRead(PIN_VBAT) should work, Refer to the code below

const int analogInPin = PIN_VBAT;
int raw_bat = 0;        // value read from the pot
int v_bat = 0;
uint8_t state = 0;

void setup() {
  digitalWrite(PIN_VBAT_ENABLE, LOW);

void loop() {
  state = 1 - state;
  digitalWrite(LED_BUILTIN, state);
  // read the analog in value:
  raw_bat = analogRead(analogInPin);
  v_bat = (raw_bat * 3 * 3300) / 1024;

  Serial.print("raw = ");
  Serial.print(", V = ");

I confirmed that analogRead(PIN_VBAT) works, but the IDE hangs if there is a digitalWrite(LEDG, LOW); or digitalWrite(P0_30, LOW);.

void setup() 
//  while(!Serial);

//  pinMode(P0_31, INPUT);    //Battery Voltage monitoring pin
  pinMode(PIN_VBAT, INPUT);    //Battery Voltage monitoring pin
  pinMode(P0_13, OUTPUT);   //Charge Current setting pin
  pinMode(P0_14, OUTPUT);   //Enable Battery Voltage monitoring pin
  digitalWrite(P0_13, LOW); //Charge Current 100mA   
  digitalWrite(P0_14, LOW); //Enable

  analogReference(AR_INTERNAL2V4);  //Vref=2.4V
  analogReadResolution(12);         //12bits

void loop() 
  digitalWrite(LEDG, LOW);
//  int Vadc = analogRead(P0_31);
  int Vadc = analogRead(PIN_VBAT);

  float Vbatt = ((510e3 + 1000e3) / 510e3) * 2.4 * Vadc / 4096;
  Serial.print("0x");Serial.print(Vadc, HEX);
  Serial.print(", "); 
  Serial.println(Vbatt, 3); 
  digitalWrite(LEDG, HIGH);

Hi Seeed-Jx,

I checked the LED and LED pin name and analogRead(PIN_VBAT) combination, but the 2/3 combination did not work correctly. In the worst case, the COM port was lost!
Below is the sketch I used to check.

#define LED LED_BUILTIN      // default
//#define LED LEDR                // OK : can read voltage and flush red LED
//#define LED LEDG                // NG : COM port disappears and off green LED
//#define LED LEDB                // OK : can read voltage and flush blue LED
//#define LED P0_26               // NG : can read voltage but always off red LED
//#define LED P0_30               // NG : COM port disappears and on green LED
//#define LED P0_6                // NG : can read voltage but on blue LED

const int analogInPin = PIN_VBAT;
int raw_bat = 0;        // value read from the pot
int v_bat = 0;
uint8_t state = 0;

void setup() {
  pinMode(LED, OUTPUT); 
  digitalWrite(PIN_VBAT_ENABLE, LOW);

void loop() { 
  state = 1 - state;
  digitalWrite(LED, state); 
  // read the analog in value:
  raw_bat = analogRead(analogInPin);
  v_bat = (raw_bat * 3 * 3300) / 1024;
  Serial.print("raw = ");
  Serial.print(", V = ");

Not to mention EVERY update to the IDE, will break the end result without the PINS fix . So YEA! LoL ChatGpt…
and when your trying to produce a prototype with code that will LIVE up to NOT just my HIGH personal standards but truth in advertising matter’s as much as PERFORMANCE IMO :pray: Apparently someone at Seeed marketing thought enough to say so on the second bullet point. (and you do realize if you can’t read the battery properly and reliably then battery powered is in two words is NOT REAL.

AND while I’m up here… :star_struck: YOU notice the two biggest issues folks have on here with this chip is “Deep Sleep” and Battery power items along with mbed conflicts and incompatibilities… So it’s a GREAT product alright but ON-Going and Timely technical support and continuing Engineering is warranted at $16
----soap box off__

" * Wireless Capabilities: Implement Bluetooth 5.0, BLE functions with onboard antenna, also provide NFC connectivity

  • Elaborate Power Design: Provide ultra-low power consumption as 5 μA in deep sleep mode while supporting lithium battery charge management
  • Advanced onboard Functionality: Assemble additional digital microphone and 6-axis IMU in the tiny board for embedded Machine Learning applications
  • Thumb-sized Design: 21 x 17.5mm, Seeed Studio XIAO series classic form-factor, suitable for wearable devices

Seeed Studio XIAO nRF52840 Sense has an ultra-low power consumption of only 5 μA in the deep sleep mode, the embedded BQ25101 chip supports battery charge management which prolongs its use time. (we hope)

I look forward to a fix. I’ll try it.

Thanks for you quick response’s.

For some reason, analogRead(PIN_VBAT) and LEDs have a close relationship, and some combinations can cause hang-up.Below is a summary of the relationship between red LED or green LED and analogRead().

				      v2.9.0   v2.9.0+patch	  v2.9.1	v2.9.1+variant
with LEDG
analogRead(PIN_VBAT)	XXX			O			XXX			O
analogRead(P0_31)		X			O			X			O

analogRead(PIN_VBAT)	XXX			O			O			O
analogRead(P0_31)		X			O			X			O

X:	can't read
XXX:hang up
O:	work

Both v2.9.0 and v2.9.1 do not work properly with the green LED. Applying the patch proposed in #1/30 to v2.9.0 makes it work as expected, but Seeed-Jx pointed out a compatibility problem.I modified two files in the variant folder to work around the compatibility problem. I replaced variant.cpp and pins_arduino.h with the attached files and checked the sketch and it works fine.
I would be happy if someone could confirm this.

analogReadTEST.zip (5.4 KB)

The following is the confirmation procedure.

  1. Unzip the file “analogReadTEST.zip”.
  2. Delete “pin_arduino.h” and “variant.cpp” in directry “\Arduino15\packages\Seeeduino\hardware\mbed\2.9.1\variants\SEEED_XIAO_NRF5284” and “\Arduino15\packages\Seeeduino\hardware\mbed\2.9.1\variants\SEEED_XIAO_NRF5284_SENSE”,
    then copy “pin_arduino.h” and “variant.cpp” from the unzip folder.
  3. Copy “analogRead_LED” to an appropriate location.
  4. Compile and execute “analogRead_LED” from ArduinoIDE.

The pinDefinitions.h patch is not necessary since the variants file will be replaced in v2.9.1.