How to detect Electron goes from microUSB power to battery?

Is there a Particle function or simple method to detect when my Electron goes from running on microUSB power to battery? It would be interesting for detecting power outages. Then next question would be is there a function or easy way to monitor the LiPo’s current voltage level so I can determine how much longer before the battery capacity is getting low? Thanks in advance for any tips!!

I’m know there are functions that will provide battery SOC (State of Charge 0- 100%) and I’m pretty sure you can read battery voltage also.

All the functions for the Electron should be listed here;

1 Like

Thank you @RWB!! I’ve figured out my second question first. The answer I found using your help, more specifically in this “Fuel Gage” section of the reference area

2 ways to do it…

  1. Show the actual voltage of the battery
  2. Show the % of capacity (SOC) as you mentioned

I tried it out in a sample sketch already and it works. I put the “Fuelgage fuel;” up in the top of the sketch and then in the setup area put in the two lines of code

Serial.println( fuel.getVCell() );
Serial.println( fuel.getSoC() );

Very nice :slight_smile: I can see the results when I monitor the serial port console on startup


Sweet, glad I could help out :smiley:

I have the Electon here, can’t wait till I have some time to play with it.

1 Like

Just got my Electron a couple days ago. Love it! Learning a lot and reading a lot more. The cellular stuff is a whole new world!

I think the answer to my first question about microUSB is out there but not yet documented in the reference area. I believe one of these functions in this section of the guide has the answer, I’m just not sure how to compose the code. One of the many advanced functions at

Just a thought here, it does have several functions in the PMIC such as even turning off the buck converter. You could read the voltage in on the BQ module because it would be separate from the Lithium function.

So basically have 3 functions
-Detect VIN Voltage on the BQ
-Detect LIPO fuel gauge and compare to X interval (sloping downward would mean discharging if that isn’t already accessible through I2C)
-Read the switching frequency and current incoming. (The battery will be a lower voltage so it will have more current, and if the VIN register is 0 (no USB or VIN), you can know that it is discharging the battery, but you also get the rate and can estimate how long you can survive for. Then you can read a VIN voltage and decide to enable the charging or power the Electron with it (if the source is unstable you can filter it, and shut it out to prevent damage. A good example is an unregulated power supply that claims its for 12v but puts out 16 when no load is applied, and loads down to 1 volt when you over-run it’s current captivity. I doubt the BQ will like that very much without some guidelines you can set.)

Other ideas
-Detect LED on PMIC module
-Read the pre diode side of the Micro USB with a Sharp PC817 Optocoupler, Resistor, switching diode and ground. (IE if the power is on before the input diode, the LED turns the optocoupler on (which is completely isolated, and give you a high on a pin of your choosing) This would turn it down to 2 functions.
“I am I alive, ok I have a power source.” "
Is XX high or low? “Ok, now I know I am getting power from something to charge the battery so I can stay alive fighting for the users!”

I just took a look at the functions in the link you provided and there is tons advanced functionality in the Texas Instruments PMIC module. That’s really nice to see. It looks like I can even customize the battery charging chips charging voltage and have it work with a different battery chemistry possibly.

So it sounds like you just want to know when the Input power source that feeds the regulator that feeds the processor and uBlox module switches from running on USB Input over to Battery Power.

I think this function should tell you which power input source is currently feeding the buck regulator. They do not show any example code over the right so its hard to tell what the function return info will look like but I would try this to see what the function returns when running on USB vs battery power. If you get 2 different returns then you can use it to notify you when the Electron switches input supplies.

Let us know what you find out. I’m excited to see this level of customization being possible.

I have been looking at the BQ24195 since Particle was evaluating it. Other than a little difficult to layout for higher current, it is an amazing chip.I am willing to bet there is more in there than meets the eye. (Some registers that don’t yet have functions)
I use the L model, but I am not sure if Particle used the Base or the L. At one point I think I had heard it was not the 195, so I hope I am not causing confusion.

EDIT: Yes it is the 195, just checked.

If you are careful there are some testing pads to get more information externally, but be careful around that chip and I doubt that would be recoverable if you melt the traces or lift the pads.

Yea TI makes some great stuff.

I’m testing a few of their battery management chips and they are as good as it gets. Enough features to make your head spin :smile:

I had a dream to work for them in their new office a few blocks away from where I lived, but other things happened before I could even apply. Funny thing is they are today’s Fairchild in a way, as they were in the 60s and 70s. Inventing new things before they even have a use, but somehow always on target.

Have a look at the Supercap stuff, that is making me drool, but SMBUS is a bit more tricky…

Yea buddy I had to figure out the SMBUS communication for the battery management fuel gauges and it was a real bitch and took me and 2 other guys on this forum a week to work though it all. TI’s documents on it were not correct either so that didn’t help either.

TI’s application support has been excellent to me though. A++

So can anyone share code on how to identify if the source is battery or USB?

And/Or know how much power is left in the battery?

@frlobo I think this thread addresses that


@RWB - have you figured out from those sparse docs how to get that data? It’s a little confusing to me.

Which data exactly? I did figure out how to raise the battery charging voltage but it did not do what I wanted so I changed it back to default.

The functions listed in the link seems to be pretty clear cut. Have you tried any of them that just return values?

Just whether each power source is connected. Haven’t tried those functions myself as @BulldogLowell 's trick seemed to work, but if it’s baked in that would definitely be better to just call a function.

1 Like