Switching a USB connection on/off

My problem, put very simply, it to switch a USB connection off and on according to the presence of a 5V signal.

I’ve tried a mosfet bottom side switch on the gnd line - it doesn’t work. If the 5V is absent then the USB connection is not operable. But, if the 5V is present and the connection is active, dropping the 5V does not sever the USB connection.

Is there any way to do this?

BTW: There is a Pi at one end.

1 Like

Hi Kevin,

How interesting, what model of Pi is it? Also, why use the GND line rather than 5V for the MOSFET (I’m assuming it’s an N-Channel)? I’d expect that you’ll be getting issues powering/removing power from the Pi when using a floating ground such as that. The other option is to grab one of the inline switches for your Pi, then cannibalize it by switching out the latching switch for a relay or MOSFET appropriately. Also, are you only using the USB to power the Pi? If so, then why not power the Pi via the GPIO instead?

Thx for your reply.

  1. I didn’t think that switching the 5V line would work.

  2. The application: I have an Ender 3 Pro 3D printer that is connected to a Pi 3B+ via the said USB cable (running OctoPrint). The problem is that when the printer is turned off, the Pi continues to power the printer’s motherboard via the USB connection.

  3. It think the USB connection is maintained after the gnd is cut via the D+/D- rails - but I don’t know enough about the USB side of things.

  4. Yes, N-channel.

  5. The Pi will take care of itself - physical removal of the cable has no deleterious effect on the Pi or the OctoPrint software.

  6. I will investigate your suggestion re a bit of cannibalism.

Hi Kevin,

You’re welcome! I’ve answered your questions numbered below:

  1. Yes, what ends up happening if you’ve got the mosfet on the ground is that you get a floating ground, so you can’t be sure that no current is flowing.

  2. That should be fixed once the power to the Pi is correctly removed

  3. That’d be correct, as there’s still a floating ground causing a difference in voltage.

  4. Excellent, that should work nicely.

  5. As long as there’s no writing currently being completed on the SD card yes, you should be fine to do an ‘unsafe’ shutdown. Note the activity light on your Pi

  6. It’s probably the easiest way to do it, but that’s completely up to you

All the best with your project!

1 Like