Power/Data Efficient best practices - Electron

Does anyone have sample code that uses the FuelGuage data and sleep or any methods to conserve battery on the electron?

I am trying to find out if the electron is connected to the USB so I can relax power savings, once on the battery. I turn off LED and turn off the radio. Once an input is received the radio goes on and transmit changes and goes back off.

I am guessing this is not the best data efficient way since it has to handshake on every connect.

Has anyone attempted something like this that makes more data/power efficient use of the battery and the radio?

How does the Sleep command work for you with an ISR?

Any hint would be appreciated!

Here’s an answer to one small part of your question: How to determine if you’re powered by USB:

#include "Particle.h"

bool isUsbPowered(); // forward declaration

PMIC pmic;
bool lastPowerState = false;


void setup() {
        pmic.begin();
}

void loop() {
        bool powerState = isUsbPowered();
        if (powerState != lastPowerState) {
                Particle.publish("usbPowered", (powerState ? "true" : "false"), 60, PRIVATE);
                lastPowerState = powerState;
        }

        delay(2000);
}

bool isUsbPowered() {
        byte systemStatus = pmic.getSystemStatus();

        return ((systemStatus & 0xc0) == 0x40);
}

/*
//System Status Register
//NOTE: This is a read-only register
REG08
BIT
--- VBUS status
7: VBUS_STAT[1] | 00: Unknown (no input, or DPDM detection incomplete), 01: USB host
6: VBUS_STAT[0] | 10: Adapter port, 11: OTG
--- Charging status
5: CHRG_STAT[1] | 00: Not Charging,  01: Pre-charge (<VBATLOWV)
4: CHRG_STAT[0] | 10: Fast Charging, 11: Charge termination done
3: DPM_STAT             0: Not DPM
                                1: VINDPM or IINDPM
2: PG_STAT              0: Power NO Good :(
                                1: Power Good :)
1: THERM_STAT   0: Normal
                                1: In Thermal Regulation (HOT)
0: VSYS_STAT    0: Not in VSYSMIN regulation (BAT > VSYSMIN)
                                1: In VSYSMIN regulation (BAT < VSYSMIN)ault is 3.5V (101)
0: Reserved
 */
3 Likes

Thanks @rickkas7!

Great info here…

Can you explain the systemStatus & 0xc0 == part?

I see the VBUS status, and Charging status, but don’t understand the hex part.

Sorry for asking too much, but its the only way I can learn :smile:

Sure, the system status register is 8 bits wide, and 4 of the bits are simple one-bit on-off values (DPM_STAT, PG_STAT, THERM-STAT, VSYS_STAT). However two of the fields are 2 bits wide, VBUS_STAT and CHRG_STAT. The one we’re interested in is VBUS_STAT, which is the two most-significant bits in the byte value. Those are bits with the values 0x80 and 0x40, but we want to check both bits, which is 0x80 + 0x40 = 0xc0, which we then check to see if they match 0x40.

Another way I could have written it is using a right shift 6 places to the right so those bits are in the least significant bit place.

return ((systemStatus >> 6) & 0b11) == 0b01);
1 Like

Got it!

Thanks. Makes perfect sense now!