I got a battery attached to my Seeed Studio XIAO nrf52840 and once I plug in USB-c cable the charging LED goes on. Is there a way in code to detect if the USB-c cable has been plugged in so I can run some code when that happens?
Hi Philipp,
Good question I have not seen a specific answer, but it leaves me to believe you should be able to read the charging state? and get the same result. Maybe one of the other GREAT seeed Hive mind members could reply. I’m somewhat skeptical however, because of reading the battery charge level is broken in 2.9.x board files and seeed engineering is not responding to it.
HTH
GL ![]()
PJ
What about reading an analog input pin connected to a voltage divider circuit from the +5V source to determine if the USB is connected? The charging state can be determined by reading P0_17, eg:
pinMode(P0_17, INPUT);
charging = ! digitalRead(P0_17); // negated so 0=not charging 1=charging
However, once a battery is fully charged, even if the USB cable remains connected the charging LED will go out and the variable “charging” in the example above will be zero.
So long as the USB cable is connected, the battery charge will be maintained but the charging LED will never go back on nor will the charging state change.
Yes, it is possible to detect whether the USB-C cable has been plugged in or not by monitoring the status of the USB connection using the nRF52840 microcontroller on the Seeed Studio XIAO board. You can achieve this by utilizing the USB API provided by the nRF SDK (Software Development Kit).
No examples exist for the Arduino IDE and Xiao Nrf52840 that I know of or have seen, So SDK may be the only method.
HTH
GL ![]()
I’m working with the Seeed Studio XIAO nrf52840 board and have a specific requirement related to detecting the status of the USB-C cable connection. I have a battery attached to the board, and whenever I plug in the USB-C cable, the charging LED turns on. I’m wondering if there’s a way to detect this USB-C cable connection in my code so that I can execute certain actions or run specific code when the cable is plugged in. Thanks
You may consider to use Vbus of the USB port to fire an interrupt. However the Vbus voltage is too high for Xiao and you need to add a divider circuit before connecting to Xiao. In other words you have to modify Xiao otherwise there is no way to detect the USB port.
A bit late to the party, but on the schematic, the P0.17 pin is wired to a charging status pin of the charging IC. It’s the same signal that drives the LED. Hope it helps.
To all future visitors of this thread. Use this line
if (NRF_POWER->USBREGSTATUS & POWER_USBREGSTATUS_VBUSDETECT_Msk)
or if using softdevice (don’t access registers directly when Bluefruit is active) use the functions below
/**@brief Enables or disables the power USB-detected event.
*
* Enabling this will give a SoftDevice event (NRF_EVT_POWER_USB_DETECTED) when a voltage supply is detected on VBUS.
* The event can be retrieved with sd_evt_get();
*
* @param[in] usbdetected_enable True if the power ready event should be enabled, false if it should be disabled.
*/
sd_power_usbdetected_enable(uint8_t usbdetected_enable)
/**@brief Enables or disables the power USB-removed event.
*
* Enabling this will give a SoftDevice event (NRF_EVT_POWER_USB_REMOVED) when a voltage supply is removed from VBUS.
* The event can be retrieved with sd_evt_get();
*
* @param[in] usbremoved_enable True if the power ready event should be enabled, false if it should be disabled.
*/
sd_power_usbremoved_enable(uint8_t usbremoved_enable)
The last 2 lines are found in “AppData\Local\Arduino15\packages\Seeeduino\hardware\nrf52\1.1.11\cores\nRF5\nordic\softdevice\s140_nrf52_7.3.0_API\include\nrf_soc.h”
I think there are threads on this subject here somewhere… basicly i can think of 2 ways… use the serial! command or tie the 5v thru a resistor to an ADC line and sense the voltage that way… FYI use a proper resistor to bring the voltage down to below 3v3 or you will burn out the ADC
