# Battery Charging Indicator - not working?

**URL:** <https://community.particle.io/t/battery-charging-indicator-not-working/48345>\
**Category:** BLE // NFC\
**Tags:** xenon\
**Created:** [March 10, 2019, 7:42pm UTC](https://community.particle.io/t/battery-charging-indicator-not-working/48345 "2019-03-10T19:42:55Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![armor](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/armor/32/18398_2.png) [@armor](https://community.particle.io/u/armor)\
**Post date:** [March 10, 2019, 7:42pm UTC](https://community.particle.io/t/battery-charging-indicator-not-working/48345/1 "2019-03-10T19:42:55Z")

</div>

I have been monitoring the digitalRead(CHG) for the battery is charging along with digitalRead(PWR).

For CHG pin 0 = charging and 1 = not charging. For PWR 0 = battery, 1 = USB powered.

When battery powered the CHG will show as 0 (charging)? Is there a reason for this - as it appears to be a fault/bug?

---

<div class="post-metadata">

**Author:** ![Fragma](https://avatars.discourse-cdn.com/v4/letter/f/5fc32e/32.png) [@Fragma](https://community.particle.io/u/Fragma)\
**Post date:** [March 10, 2019, 10:52pm UTC](https://community.particle.io/t/battery-charging-indicator-not-working/48345/2 "2019-03-10T22:52:48Z")

</div>

Not working for me either:

> [@Can Argon (or Xenon) Read the Battery State?](https://community.particle.io/t/can-argon-or-xenon-read-the-battery-state/45554/45):
>
> Have you tested the code recently? I’m getting some odd results. I flashed it on a Xenon (w/0.9.0) and the USB & CHG was always 1. Another Xenon returned the correct USB value but CHG was always 0. A 3rd Xenon shows CHG is always 1, not sure on USB, don’t have a battery for that one.

---

<div class="post-metadata">

**Author:** ![ScruffR](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/scruffr/32/6952_2.png) [@ScruffR](https://community.particle.io/u/ScruffR)\
**Post date:** [March 11, 2019, 6:04am UTC](https://community.particle.io/t/battery-charging-indicator-not-working/48345/3 "2019-03-11T06:04:29Z")

</div>

In the [schematics](https://docs.particle.io/datasheets/mesh/xenon-datasheet/#power-1) this pin is marked as /CHG which indicates active low which will be pulled high _when_ there is a supply coming from VUSB.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/particle/original/3X/d/c/dce79c6c7087ed46d5713c7d98f81ac7b6027612.png)

Without VUSB (indicated by PWR) the 6k2 pull-down will enforce the low level.  
So only the combination `PWR && !CHG` will provide the actual charging status.

---

<div class="post-metadata">

**Author:** ![armor](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/armor/32/18398_2.png) [@armor](https://community.particle.io/u/armor)\
**Post date:** [March 11, 2019, 6:26am UTC](https://community.particle.io/t/battery-charging-indicator-not-working/48345/4 "2019-03-11T06:26:34Z")

</div>

So on U3 /CHG means active LOW pin? I am glad you can read these schematics.  
As you say it is tri-state battery, powered & not charging, powered & charging. Good explanation.

---

<div class="post-metadata">

**Author:** ![caseywdunn](https://avatars.discourse-cdn.com/v4/letter/c/2bfe46/32.png) [@caseywdunn](https://community.particle.io/u/caseywdunn)\
**Post date:** [March 21, 2019, 9:36pm UTC](https://community.particle.io/t/battery-charging-indicator-not-working/48345/5 "2019-03-21T21:36:52Z")

</div>

Thanks for clarifying this! I am still figuring out how to apply it to detect whether there is USB power or not, while operating on battery. `digitalRead(CHG)` and `digitalRead(PWR)` are both 0 with battery plugged in, whether USB is plugged in or not. Should I be approaching this in a different way?

---

<div class="post-metadata">

**Author:** ![Fragma](https://avatars.discourse-cdn.com/v4/letter/f/5fc32e/32.png) [@Fragma](https://community.particle.io/u/Fragma)\
**Post date:** [March 21, 2019, 10:48pm UTC](https://community.particle.io/t/battery-charging-indicator-not-working/48345/6 "2019-03-21T22:48:57Z")

</div>

FYI, I’ve been doing this:

```auto
        hasUsbPower = digitalRead(PWR); 
        isCharging = (hasUsbPower && !digitalRead(CHG));  
        onBatteryPower = (!hasUsbPower || !isCharging);

```

Based on that if you have both battery and usb you’ll see the usb is only used to charge the battery, then it runs off battery for awhile. That surprised me, I thought it’d charge the battery and then run off the USB but that’s not what the above is showing in my testing.

–April update: I think the battery part of the above is wrong. It appears to use USB and not battery, when USB is available, thus onBatteryPower = !hasUsbPower; is what I use now. The part that tripped me up before is the voltage is different between USB only and USB + charged battery.

---

<div class="post-metadata">

**Author:** ![caseywdunn](https://avatars.discourse-cdn.com/v4/letter/c/2bfe46/32.png) [@caseywdunn](https://community.particle.io/u/caseywdunn)\
**Post date:** [March 22, 2019, 1:06am UTC](https://community.particle.io/t/battery-charging-indicator-not-working/48345/7 "2019-03-22T01:06:22Z")

</div>

Thanks again @Fragma and @ScruffR.

Is it possible to do conditional compilation based on what the target board is? Some sort of `#if .... #endif` to check power one way for xenon and another for boron? I’d really like to have one code base to roll out on both boards if possible.

---

<div class="post-metadata">

**Author:** ![ScruffR](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/scruffr/32/6952_2.png) [@ScruffR](https://community.particle.io/u/ScruffR)\
**Post date:** [March 22, 2019, 5:55am UTC](https://community.particle.io/t/battery-charging-indicator-not-working/48345/8 "2019-03-22T05:55:47Z")

</div>

> [@caseywdunn](#):
>
> Is it possible to do conditional compilation based on what the target board is?

Yes, `PLATFORM_ID` would be the macro you want to check against `PLATFORM_ARGON`, `PLATFORM_BORON` or `PLATFORM_XENON`.

Here is the list for all platforms IDs

> <https://github.com/particle-iot/device-os/blob/e7a513bf0132119ff4498e5d1388e5a84109f750/hal/shared/platforms.h#L27>

---

<div class="post-metadata">

**Author:** ![caseywdunn](https://avatars.discourse-cdn.com/v4/letter/c/2bfe46/32.png) [@caseywdunn](https://community.particle.io/u/caseywdunn)\
**Post date:** [March 22, 2019, 1:35pm UTC](https://community.particle.io/t/battery-charging-indicator-not-working/48345/9 "2019-03-22T13:35:18Z")

</div>

Worked like a charm. The following allowed me to write different codes for Xenon and Boron in the same file:

```auto
#if (PLATFORM_ID == PLATFORM_BORON)

// Stuff for Boron

#else

// Stuff for other boards
pinMode(PWR, INPUT);

#endif

```

Also, I had some trouble with `digitalRead(PWR)` until I realized that it needs `pinMode(PWR, INPUT);` in `setup()` to work, per [Mesh (Argon/Xenon) detecting USB power loss](https://community.particle.io/t/mesh-argon-xenon-detecting-usb-power-loss/46221/7) .

Everything is working well now. I really appreciate the fast and spot on help in this forum - thanks again.
