Charging status for a battery connection

Hello, I’m using XIAO nRF52840 which connected to a battery. I manage to succeed reading battery percentage , but I also searching for a way to get an indication if the battery is charging or not. I didnt find any solution and glad if you can help me guys. Thanks!!

If your BSP is “mbed 2.9.2”

#define CHG P0_17

if your BSP is “non-mbed 1.1.1”

#define CHG (23)

Serial.println(digitalRead(CHG) ? “No charge” : “Charge”);

how do I know my BSP?

When you select a board in ArduinoIDE, which board do you select?
Seeed XIAO nRF52840 Sense ← non mbed
Seeed XIAO BLE Sense - nRF52840 ← mbed

“XIAO nRF52840 BLE” but without the “Sense” I tried to use “Serial.println(digitalRead(CHG) ? “No charge” : “Charge”);” with the “#define CHG P0_17” on top but I got an error : “stray ‘\342’ in program”

EDIT: I succeed, just wrote #define CHG P0_17 &
Serial.println(digitalRead(CHG)); and it worked!
Thanks alot!!!

I’m faced with the task of figuring out how to detect whether the battery is being charged or not. Despite searching through available resources, I haven’t been able to find a solution that addresses this specific issue. It would be really valuable to know if there’s a method, a register, or any specific approach that I could use to determine the charging status of the battery.

1 Like

See @msfujino’s reply. The green LED on the board will light up when charging, all you need to do is set that pin to input and read it when you want to know.

I’m currently tasked with the challenge of determining the battery charging status. Despite thoroughly searching through various resources, I have yet to uncover a solution that adequately addresses this precise issue. I’m eager to discover whether there exists a method, a register, or a specific approach that I could employ to accurately ascertain the battery’s current charging status. Any insights into this matter would be immensely appreciated.

CesarWade,
Battery voltage and charging status can be read, but what exactly is the information you need?

Hello, do you mean you just wanna know whether it is charging or getting the voltage information?

I need the current battery voltage level and charging status, including whether the battery is charging or discharging. This information is crucial for monitoring the battery’s health, determining if it needs charging, and ensuring proper power management in devices that rely on batteries.

Try this.

//----------------------------------------------------------------------------------------------
//Board Library : Seeed nRF52 Borads 1.1.4
//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));

  delay(1000);
}

Can anyone provide a similar example for XIAO ESP32-S3?