4

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

5

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)
7
  • Is there something similar for VBUS detection to get the (not)plugged in?
    – Majenko
    Commented Jul 24, 2017 at 17:36
  • @Majenko: VBUS in USBSTA. Commented 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
    Commented Jul 24, 2017 at 18:48
  • 1
    if (UDADDR & _BV(ADDEN)) ... Commented 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
    Commented Oct 12, 2017 at 23:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.