Detecting USB or battery power

Hi all. I have decided to use the XIAO-ESP32-C6 devices in my projects. So far they are great.

How can I detect in my software if there is USB power being provided or not? As I would like to have this as a readable Matter attribute to determine if my device is just being battery-powered, or charging via USB.

I am using ESP-IDF and and ESP-Matter repositories for this (Not Arduino).

Thanks in advance

Hi there,
Take a look at how it is done using the Xiao nRF52840 , search the thread and demo I have on here.
The usb being plugged in detectable on one of the pins. I forget which at the moment. Go look and read up on it.
HTH
GL :wink: PJ :v:

“detecting USB plugged in” > magnifier top right. :+1:

Hi, you can connect a resistor between the 5V pin and the analog pin on the XIAO, and then use the analog pin to measure the voltage at the 5V pin. Since the 5V pin is directly connected to the power supply from the Type-C, you should measure around 5V when the data cable is plugged in. If powered by a battery, the voltage on that pin will be noticeably lower than 5V. By observing this voltage change, you can determine whether the power source is the battery or the data cable.

you have to size the resistor so that the 3.3v will not be exceeded at the chip else it will burn

@cgwaltney Thanks for your reply. This is what I thought initially. However, I am hoping that there is a way to do this without attaching more hardware. The 5V pin gets 5V with the USB plugged in, and 0V when not. I still can’t figure out how to read the VBUS from the ESP32-C6. A part of me says that there must be a way.

yes we are really wanting this functionality, as i believe many gpio become not connected with the XIAO form factor, so it should be possable in the future… as far as i know it is not possable other than the way you stated… i heard somewhere that something in the wifi subsystem could be query to get power… but that would not be usb… the only other idea would to be to write some code to see if usb data is active… but this would only work on usb power and data cable… i think many power bricks do communicate over usb data lines as well… also an idea that has been floated was to try to read a gpio pin a built in led is connected to and somehow back calculate the voltage… and if the voltage is higher than charge voltge you would know… but also the voltage regulator may block this…

@cgwaltney In fact the USB D+ and D- is connected to GPIO pins 12 and 13 of the SOC according to the schematic. Reading these pins should detect if the USB is connected or not, even when in the idle state and no data is being sent or received?

yes, i believe it is not as simple as just reading… i think you have to try to negociate a connection to start communication on the line… if the host trys also to establish a connection then you have your answer… i think you would need to maintain some sort of communication link to be able to detect a disconnect… but def a road worth exploring

@cgwaltney My assumption is that D+ will always be in the inverse of D- when it is connected (when either in idle state or sending data ). These lines should be both low when USB is not connected? Again, just an assumption:)

im not sure … it will not hurt to test I wait to see what you find out…

Hi there,
SO this is the reason these devices are NOT really ready to be considered for a real application of Wearable tech.
You can’t read the battery level and the Xiao ESP32C6 is trash with only the led. charge/NOT ?
I want to see someone get down to 15uA with that boat anchor. one word
PMIC

at least with the nRF52840 Xiao you have some capabilities and options for reading BATTERY and if the usbC port is plugged in.
PMIC sandwich board would be great for both lines, but the Xiao nRF52840 BLE is the king of low power. Nordic has always been the power efficiency leader. Esspreso say’s they are getting better but Idunno, I smell that smell :face_with_hand_over_mouth:. I got to dig up that pmic xiao board idea and get a couple made asap.

GL :slight_smile: PJ :v:

1 Like

@cgwaltney I tried it and of course the D+ and D- values are changing all the time. Even both 0 when connected. There is no choice but to feed back the 5V pin into an input pin via a voltage divider to determine if USB is connected .

I’m thinking with resistors of 45K and 100K. It should divide the voltage down to 3.5 V at the input pin. However, would there be much power loss if any doing this, if constantly connected? Something similar will need to be done for measuring the battery voltage also.

i dont think the power drain would be much… you could also do a digital read probably and get a 1 or 0 result… maybe use a potentiometer for a temp test

Yes it will be a digital read for the USB voltage and an ADC read for the battery voltage. Thanks. I’ll let you know how it goes:)

1 Like