Argon check power source?

argon
Tags: #<Tag:0x00007f038f9847b8>

#1

I’m looking for direction/ideas.

What is the best way to output the current “power status” for an Argon. I would like to know if the Argon is powered via USB or battery at a given time . When on battery, what is the current battery level and if it is charging or not?


#2

For that you could look at this thread

For detection whether external power is present or not, you can use the VUSB pin with voltage divider to keep the input range between 0 & 3.3V.

About charging, I’m not sure whether the CHG LED can be accessed easily but @rickkas7 may know.


#3

Yes, all of those are available on the Argon and Xenon.

#include "Particle.h"

SerialLogHandler logHandler;

char lastMsg[128];
unsigned long lastPublish = 0;

void setup() {
	Serial.begin();
	pinMode(PWR, INPUT);
	pinMode(CHG, INPUT);
}

void loop() {
	float voltage = analogRead(BATT) * 0.0011224;

	// PWR: 0=no USB power, 1=USB powered
	// CHG: 0=charging, 1=not charging
	char buf[128];
	snprintf(buf, sizeof(buf), "voltage=%.1f PWR=%d CHG=%d", voltage, digitalRead(PWR), digitalRead(CHG));

	if (strcmp(buf, lastMsg) != 0 && millis() - lastPublish > 2000) {
		Particle.publish("battery", buf, PRIVATE);
		Log.info(buf);
		strcpy(lastMsg, buf);
		lastPublish = millis();
	}
}

They’re available on the Boron as well, but the technique is different because you need to query the PMIC instead.


Boron Battery Connected
#4

Thanks @ScruffR @rickkas7 This is spot on!