pxt-microbit icon indicating copy to clipboard operation
pxt-microbit copied to clipboard

Is there a way to tell programmatically if the microbit is connected to a battery or getting it's power from the USB plug?

Open yossiy66 opened this issue 1 year ago • 3 comments

Hi, I would like to be able to know programatically (i.e. in my code) if the Microbit is getting its power from a battery (either through the battery connector on the card or through an extension board like SuperBit) or from the USB plug. It is important since when I try to run a DC/servo motor, it does make a difference, and I would like to be able to issue some sort of warning if the power supply is only from the USB plug. Thank you Yossi

yossiy66 avatar Mar 06 '24 18:03 yossiy66

@carlosperate ?

abchatra avatar Mar 06 '24 19:03 abchatra

An extension could be made calling the uBit.power.getPowerSource() C++ function. https://github.com/lancaster-university/codal-microbit-v2/blob/v0.2.66/inc/MicroBitPowerManager.h#L196-L203

One thing to keep in mind is that there is an issue, where battery power cannot be correctly detected if USB power is also present. But you should be able to tell if USB power is present or not:

  • https://github.com/lancaster-university/codal-microbit-v2/issues/72

microbit-carlos avatar Mar 07 '24 17:03 microbit-carlos

@yossiy66 Here's a rough proof-of-concept, but I'm not sure it'll quite fit your needs: https://github.com/bsiever/pxt-microbit-powerUtils .

Here's a project that shows its use: https://makecode.microbit.org/S99662-17088-86607-52275 (All directly based off @microbit-carlos's suggestion...I just crudely wrapped it in an extension)

bsiever avatar Mar 08 '24 03:03 bsiever

Question answered.

abchatra avatar Jul 18 '24 17:07 abchatra