Tell if USB is connected

The official pyboard running MicroPython.
This is the reference design and main target board for MicroPython.
You can buy one at the store.
Target audience: Users with a pyboard.
Post Reply
User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Tell if USB is connected

Post by Roberthh » Sun Sep 11, 2016 10:20 am

Is there a way to tell if PyBoard is plugged into a computer's USB port. I'm not talking about vcp.is_connected(), since this function returns 'True' only after a session is established. I'm looking for a status indicator in the USB controller at the physical connection level. I tried various control & status registers, but with no luck so far.

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Tell if USB is connected

Post by dhylands » Sun Sep 11, 2016 6:23 pm

On the pyboard, VBUS from the USB connector is tired to PA9, which also has a board name of 'USB_VBUS'.

So can query the state of that pin:

Code: Select all

vbus = pyb.Pin('USB_VBUS')
if vbus.value():
    print('Powerd via USB'
else:
    print('Not powered via USB')

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: Tell if USB is connected

Post by Roberthh » Sun Sep 11, 2016 7:08 pm

Hi Dave, thanks a lot for the hint. Now that you're telling it, I notice it in the schematics, but the name USB_VBUS for this PIN is not mentioned in the docs. So this hint is essential, and I have hoped that you would know.

Post Reply