How to find if VUSB is powered or not from firmware?

I want to find if USB IN is powered or not from the Firmware. Is that possible. Please help.

@manihk, for which device - Photon or Electron?

I’ll assume you’re talking about the Electron. This is not possible on the Photon or Core because they don’t have a power management chip (PMIC). Also, I may not have done this in the preferred manner; I tried it for fun because I’d never done it before and was curious. It seems to work for me.

The docs are here, sort of:
https://docs.particle.io/reference/firmware/electron/#input-source-control-register

#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
 */

@peekay123 For photon. I understand that it doesnot come with a power management chip. I am building a product on photon in which I have a MCP73831 for charging. No special IC for management. I have replied to you in detail on the same in the other related question that I have raised in the forum.

@rickkas7 Its for photon.

@manihk, I answered you in the other topic. Let’s not have two topics going on this to reduce confusion.