# How to create USB toggle switch controled by GPIO

I have Raspberry Pi setup as NAS. The storage is external HDD powered through USB. I would like to encase this whole thing in some nice box and still be able to easily use the HDD, without disassembling the whole thing.

My idea is following:

The USB A would be connected to the external HDD and USB B would be just USB extension cord accessible from the outside. The box with question marks would be toggle switch controllable through GPIO header.

My idea how to wire the whole thing in the easiest way is sketched in following diagram (the switches represent USBs):

simulate this circuit – Schematic created using CircuitLab

I'm not sure if it's ok, just to switch the power rail between the two outlets.

So far I have following ideas how to implement that:

• design transistor flip-flop toggle switch
• use analog switch, something like CMOS-4066
• use dedicated circuit like MAX1562 (this one is switch only, though)

Is my idea sensible? Do you know any ready made solution? Do you know how to solve this?

• In what way is this a better idea than using a hub? – Ignacio Vazquez-Abrams Mar 28 '14 at 23:08
• Are you saying that you want to disconnect the HDD from the RPi so that some other host can acccess the HDD directly? Sort of an "inverse hub"? – Dave Tweed Mar 28 '14 at 23:22
• Does "(the switches represent USBs):" mean that the DPDT switches marked USB A, USB B, and USB C are really the USB connectors on your proposed unit? If so, please remove them from your drawing - they are horribly misleading to anyone who can read a schematic. Wires with the USB signal names, and a "USB Conn A" label would be much clearer. – Peter Bennett Mar 29 '14 at 0:05
• USB A-B Switches do exist, even commercially. Have you thought of using one of those? – Passerby Mar 29 '14 at 1:32
• @IgnacioVazquez-Abrams this works other way around. One HDD, two computers. DaveTweed yes PeterBennett fixed Passerby they are generally only manual and quite expensive. – jnovacho Mar 29 '14 at 9:25

You do not want to try running the USB signals through a mechanical switch. Too much loss of signal integrity.

Do not attempt to switch the USB signals with a '4066 type chip. USB 2.0 runs at 480MHz when in high speed mode and the cheap mux chip will severely attenuate the signalling.

You need to manage the USB Power separate from the USB D+/D- lines. A drive operating off the USB port could be pulling up to 500 mA and that takes a circuit specifically designed for that current level to manage it's switching.

I have built USB selection devices in the past using special circuitry on a board I designed. The USB data signals were switched via a Fairchild FSUSB30MUX part. The power to the target device was managed through a MIC2026-1YM USB power switch device.

The picture below shows the general layout of the circuit.

At work, some years ago, we built a device to switch an USB key drive from one computer to another. We used four DPST reed relays, although it's not a standard way to work with USB, it worked like a charm. However, some rules must be followed:

• You have to handle all four power and signal lines
• Don't switch the lines: just disconnect/reconnect them from the bus. To disconnect, fist disconnect the data lines then the power lines. To reconnect, connect first the power then the data lines.