The closest thing I can find is if(Serial) but that only returns true if there's a serial port open.
The function I want, ideally, should:
Return true if plugged into a computer
Return false if plugged into a USB power bank
Return false if not plugged in at all(Running off of its own lithium cell)
1 Answer 1
ADDEN
of UDADDR
is set if the '32U4 has been assigned a USB address by the host and the firmware wants to enable USB connectivity.
UDADDR & _BV(ADDEN)
answered Jul 24, 2017 at 17:18
Ignacio Vazquez-Abrams Ignacio Vazquez-AbramsIgnacio Vazquez-Abrams
17.7k1 gold badge28 silver badges32 bronze badges
-
Is there something similar for VBUS detection to get the (not)plugged in?Majenko– Majenko2017年07月24日 17:36:37 +00:00Commented Jul 24, 2017 at 17:36
-
@Majenko:
VBUS
inUSBSTA
.Ignacio Vazquez-Abrams– Ignacio Vazquez-Abrams2017年07月24日 17:37:32 +00:00Commented Jul 24, 2017 at 17:37 -
Stupid question, but where do I go from here? Is it as easy as (UDADDR & _BV(ADDEN)) != B00000000 ?Stephen– Stephen2017年07月24日 18:48:15 +00:00Commented Jul 24, 2017 at 18:48
-
1
if (UDADDR & _BV(ADDEN)) ...
Ignacio Vazquez-Abrams– Ignacio Vazquez-Abrams2017年07月24日 22:13:10 +00:00Commented Jul 24, 2017 at 22:13 -
Works but there is a problem with battery powered equipment when you disconnect the USB cable. This still returns true and after an restart it returns false. How to fix this?Codebeat– Codebeat2017年10月12日 23:39:15 +00:00Commented Oct 12, 2017 at 23:39