19

I have a simple device connected to the Raspberry Pi, a small USB fan. The problem is that the fan does not have a on/off switch so I must unplug the fan every time I want it to stop.

My question is: is there any way I can cut the power coming from the Pi's USB ports?

nc4pk
  • 1,378
  • 1
  • 13
  • 25
opc0de
  • 644
  • 5
  • 11
  • 18
  • Do you mean outgoing power, you want to be able to switch off the fan but not the pi, or incoming power, in whic the fan's power supply is backpowering the pi, even though the pi's own power supply has been disconnected? – René Wolferink Mar 12 '13 at 09:07
  • I want to switch off the fan the pi must run normally. – opc0de Mar 12 '13 at 09:13

5 Answers5

33

You can use my tool uhubctl, it supports Raspberry Pi models B+, 2B, 3B, 3B+ and 4B - these models have hardware ability to turn USB power off and on.

Use it like this:

Turn off power to all USB ports (must use port 2):

sudo uhubctl -l 1-1 -p 2 -a 0

Turn on power to all USB ports (must use port 2):

sudo uhubctl -l 1-1 -p 2 -a 1

Turn off power to Wifi+Ethernet (must use port 1):

sudo uhubctl -l 1-1 -p 1 -a 0

Note that Raspberry Pi 4B is very different from previous models as it has USB3 chip. It doesn't support turning off power to Wifi+Ethernet, and for USB you will need to use something like that to turn off (this turns off all USB ports - RPi 4B hardware does not support individual port power control):

sudo uhubctl -l 2 -a 0

If your Raspberry Pi does not support USB power switching, you can connect external USB hub that does (see list of compatible models), and control power on that external hub using uhubctl.

mvp
  • 509
  • 4
  • 9
4

No, the usb power is hardwired straight to the RPi power.

You can interrupt the power to the fan with a transistor or a relay, but you'll have to cut the red wire.

John La Rooy
  • 11,947
  • 9
  • 47
  • 75
  • 2
    Good answer, gnibbler, and quite correct from what I read on the raspberrypi.org forums. I can add that I've tried to do this on other machines (ARM and x86) and it is rarely possible -- only some USB controllers allow software power control.

    http://stackoverflow.com/questions/4702216/controlling-a-usb-power-supply-on-off-with-linux

    http://stackoverflow.com/questions/1163824/linux-usb-turning-the-power-on-and-off

    So I'm not surprised this isn't possible with the minimal USB in the Pi.

    – Tai Viinikka Mar 14 '13 at 14:06
  • Often you can toggle power in a usb hub's ports. Might try with fan on hub on pi, then switch power on/off on that hub port. May need external powered hub, depends on how thick your air is and how hard fan has to work. – lornix May 03 '13 at 07:25
  • 13
    This answer is wrong. It's possible to control USB power for Raspberry Pi models B, 2B, 3B - see my answer how exactly to do that. – mvp Jan 03 '18 at 04:59
  • @mvp, I can equally say that your answer is exactly wrong for all the raspberry PI that existed when the question was asked. I won't change this answer as it's still true for millions of the original raspberry PIs. It's easy for someone with a PI manufactured after 2014 to see your answer is here too. – John La Rooy Jan 08 '20 at 03:57
  • Might add that all of the other answer are correct on a software level. Those commands allow to switch power off in a way that the raspberry see all the usb as powered off and it does no longer communicate with them. However, in a hardware level, 5V is still issued by the USB port, and if you want a power cycle on your device it does not work. – Spoutnik16 Mar 29 '22 at 08:06
  • @Spoutnik16, Well when this question was asked and answered, there was only the original raspberry pi which had no software control of the USB power. The other answers are certainly more useful for the raspberry pi models that can be purchased these days (if you can find one!). – John La Rooy Mar 29 '22 at 09:41
  • @JohnLaRooy Yeah I know. But I spent time today searching this, and this answer was the one google gave me. So I put it here for the next person on the same hunt :-) – Spoutnik16 Mar 30 '22 at 06:18
4

You can use one usb fan with "on/off" switch.

enter image description here

David
  • 244
  • 1
  • 5
  • 10
    wow never thought of that !! – opc0de Mar 13 '13 at 08:52
  • I don't really understand why is your comment so rude ? You never said you did think about the switch! I tried to help you (help: Is not that what you asked for). But, I understand you frustration, see my edit. It seems that the question has already been asked and answered. – David Mar 13 '13 at 11:25
  • 2
    Dont worry about it David - Maybe it was not the answer for the OP - But somebody else might like it for another circumstance. Your answer is good. +1 - Not sure why you got downvoted- The OP said he does not have an on.off switch and you showed him where to buy one with an on/off :) -1 was not needed here and a bit rude. yes :P – Piotr Kula Mar 14 '13 at 12:30
  • 2
    David, indeed you shouldn't worry, but your answer just doesn't answer the question, which is how to cut the power coming from the pi's usb ports. So it's not a good answer. I am surprised you did get any up votes at all. – Jaap Versteegh Aug 10 '15 at 08:14
  • 2
    This did not answer the question. – Dr. McKay Jun 16 '18 at 19:28
1

You can turn of all USB ports with their power:

sudo tee /sys/bus/usb/drivers/usb/unbind

Data transfer will not be possible too.

To turn USB ports bak you can use:

sudo tee /sys/bus/usb/drivers/usb/bind

I have stolen this solution by user faceofbo from Raspberry Pi forum

Roman Matveev
  • 405
  • 4
  • 16
1

This also works for me on the Raspberry Pi 3. It was copied from here.

   sudo apt-get update
   sudo apt-get install libusb-dev
   git clone https://github.com/codazoda/hub-ctrl.c
   cd hub-ctrl.c
   gcc -o hub-ctrl hub-ctrl.c -lusb

This will create an executable named hub-ctrl. You can then run it to do the following:

  • Turn off power to all USB ports

    sudo ./hub-ctrl -h 0 -P 2 -p 0
    
  • Turn on power to all USB ports

    sudo ./hub-ctrl -h 0 -P 2 -p 1 
    

Follow the link to also turn on/off the Ethernet port.

psiphi75
  • 119
  • 3