Electronics Projects

USB switch

It is not a rare phenomenon that some USB devices attached to a computer during power-up remain invisible to the OS after booting. This concerns home-made devices during experiments and also some commercial ones. To make them visible again one has to disconnect the USB plug and reconnect it again. This leads to excessive contact wear of the USB connector. It would be nice to have a way to reconnect the device without mechanically unplugging it. A trivial solution based on buttons/switches or relays won't work because of their contact bouncing, which might confuse as device as well as the OS.

A solution to this problem, proposed in [1], is based on using analog switch IC 74HC4066 to disconnect USB data lines and a MOSFET for breaking the power. However, the published USB switch has some drawbacks. First, because of the used analog switch IC the switch is not suitable for hi-speed USB mode. Second, it has two buttons: pressing one of them disconnects the device from computer, while pressing the other one connects it again. Finally, data and power lines are broken/connected simultaneously, while a USB connector is designed so that the power lines are connected before the data lines. This might be essential for some attached USB devices.

This project is an attempt to get rid of the above mentioned disadvantages. I used much faster analog switches allowing the attached device to operate in hi-speed USB mode. The new circuit is based on a tiny PIC microcontroller, which allows a lot of functionality. The device is operated with one button only. Pressing it alternates connecting and disconnecting the device from a computer. Moreover, by connection the power line is connected before the data lines. Disconnecting the device goes in the opposite order, thus fully emulating the physical USB connector. The PIC also takes care on button de-bouncing.

Schematic Top view PCB

The left (on schematic) USB connector goes to the computer and the right one is for connecting a controlled USB device. The used PIC has only 6 pins, a program memory for 256 instructions, and runs on 4 MHz. It contains an internal pull-up resistor at pin GP3. The last line of the code (at address 0xFF) is the factory calibrated oscillator code. It is specific for each PIC. You should read this value first, or do not allow your programmer to modify the calibration data. After initializing the ports the PIC goes into a sleep. A wake-up from sleep mode is provided by pressing the button, that changes the port state. The wake-up causes a reset on-change, which is checked by the code. Changing the switch state only happens if the button is pressed for at least 2 seconds. This prevents undesired connecting/disconnecting the device by occasional hitting the button. To connect the device the PIC sets GP0 low, waits 200 msec and sets GP1 low. Resistors R1 and R2 provide initial control for the analog switches and the MOSFET during the power-up when all PIC pins are in high-impedance state. This provides an initial default OFF state of the USB switch. Diode D1 indicates the connection status.

The USB switch is assembled on a small PCB designed for Hammond 1551H enclosure. All resistors and capacitors are in SMD 0603 package. Green LED D1 is of diameter 3mm. The PCB is mounted on the detachable box cover with 4 bolts M4 (or SAE #2). All SMD components are mounted on the bottom part of PCB. The top part components include USB connectors, MOSFET Q1, the button (C&K KSA0M311), and the LED. There are three wire jumpers on the PCB - two of them are on the top side and one on the bottom one.

PCB layout Bottom view Top view

The firmware is loaded into PIC via an ICSP cable. It can be temporarily soldered to the bottom part of PCB directly to the button and MOSFET pads and to the rectangular PCB pads at pins 1 and 5 of PIC.


  1. R. Reusch "USB switch", Elektor Electronics no. 7-8, 2009, p. 64.


  1. Source code usbsw.asm
  2. HEX file usbsw.HEX
  3. Eagle board layout usbsw.brd