pxt-microbit
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?
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
@carlosperate ?
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
@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)
Question answered.