2

For an embedded system I need to be able to switch certain USB devices on a RPi on and off without physically disconnecting them. The whole setup will be in an enclosed casing. My idea now was to hardwire the USB devices to the USB ports from the Pi but separate the +5V power line and connecting that to a GPIO pin. In theory I could control the GPIO pin now to a high or low state with a small C program to 'emulate' the plugging of the USB device / to power it on and off.

What do you think about that? Is that possible or will it fry my Raspberry Pi?

SlySven
  • 3,581
  • 1
  • 15
  • 44
  • You can disable/enable USB via `sysfs` but I am not sure how applicable that is to individual ports; ideally, if the device implements such functionality, [you may be able to ask it to turn off](http://unix.stackexchange.com/a/166601/25985). – goldilocks Jun 07 '16 at 09:45
  • Does the USB device only require power to operate, or does it also communicate using the data lines in the USB interface? – PhilM Jun 10 '16 at 06:36

3 Answers3

1

There is a way to do this using software (control power to ports on a USB hub).

Some useful information from a forum thread on the Raspberry Pi website:

mahjongg wrote:
the Pi model B+ has no hardware in place to disconnect power on the USB ports!

it can detect a short (USB power Fault) and then the USB power controller (P2553W6) will
automatically block the USB power, but it can't switch off the power to USB using software.
What it can do is bring the LAN9514 into sleep mode, but that doesn't turn the power off!

reply from a moderator & RPi engineer:
Yes it can. Issuing a USB Hub class request (CLEAR_FEATURE PORT_POWER) to port 2
on the LAN9514 will deactivate the USB output power switch.

There's an app for that:
https://github.com/codazoda/hub-ctrl.c

For the above application, the command you can use is sudo ./hub-ctrl -h 3 -P 1 -p 0
- h is the hub, P is the port, and p is for switching power on (1) or off (0).
The hub and port values for a device can be found with lsusb.

Some other details (from a reply in 2016, lsusb data could be more accurate):

Just tested this with the Pi 3 [...] The USB port numbering from the picture posted above:

Hub:Port -- Controlled port(s) 
0:1 -- Controls the Ethernet port
0:2 -- Controls all four USB ports (not the Ethernet)
0:3 -- Controls USB Port 4
0:4 -- Controls USB Port 2
0:5 -- Controls USB Port 3

As best I can tell, USB Port 1 cannot be controlled individually.

Edward
  • 162
  • 9
tlhIngan
  • 3,342
  • 5
  • 18
  • 33
  • If whatever's on the other end of that link changes or disappears then the answer goes with it. Can you summarise your answer? – goobering Jun 07 '16 at 06:40
  • 1
    @goobering information from the hyperlink has been added - you could delete your comment because it's not needed. – Edward Dec 31 '17 at 15:25
  • There's a related question that also uses this method in an answer, at https://raspberrypi.stackexchange.com/questions/44194/usb-ports-stop-working-after-a-week-needs-restart – Edward Jan 01 '18 at 17:38
1

You will almost certainly fry your Pi's GPIO pins if you connect them to the 5V input of any USB devices. External USB devices require 5V, and can pull hundreds of milliamps from that line. The GPIOs are 3.3V, and are rated to a combined maximum of something like 50mA.

The safe way to achieve what you're trying to do is to use either a (relatively expensive) pre-built software switched USB hub such as the Yepkit YKUSH, or to build some simple transistor circuits as shown in Mose's answer here.

goobering
  • 10,610
  • 4
  • 38
  • 64
1

You can disable/enable USB via sysfs but I am not sure how applicable that is to individual ports; ideally, if the device implements such functionality, you may be able to ask it to turn off.

You definitely do not want to be connecting GPIO pins directly to USB devices, although you could do so by putting the right transistor or IC in between, using that to control 5V power with a 3.3V pin. Various cheap, widely available "ULNxxxx" IC's are capable of this. I would investigate the sysfs route as far as you can first though.

goldilocks
  • 56,430
  • 17
  • 109
  • 217