# Switching a USB connection on/off

**URL:** <https://forum.core-electronics.com.au/t/switching-a-usb-connection-on-off/9268>\
**Category:** Support\
**Created:** [27 January 2021 07:02 UTC](https://forum.core-electronics.com.au/t/switching-a-usb-connection-on-off/9268 "2021-01-27T07:02:08Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kevin48023](https://avatars.discourse-cdn.com/v4/letter/k/c0e974/32.png) [@Kevin48023](https://forum.core-electronics.com.au/u/Kevin48023)\
**Post date:** [27 January 2021 07:02 UTC](https://forum.core-electronics.com.au/t/switching-a-usb-connection-on-off/9268/1 "2021-01-27T07:02:08Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Bryce](https://yyz2.discourse-cdn.com/flex028/user_avatar/forum.core-electronics.com.au/bryce/32/3631_2.png) [@Bryce](https://forum.core-electronics.com.au/u/Bryce)\
**Post date:** [27 January 2021 23:18 UTC](https://forum.core-electronics.com.au/t/switching-a-usb-connection-on-off/9268/2 "2021-01-27T23:18:03Z")

</div>

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?

> **[Raspberry Pi USB Power Cable with Switch (Pi 2/3/3B/3B+)](https://core-electronics.com.au/raspberry-pi-usb-power-cable-with-switch.html)**
>
> This handy little guy is a standard microUSB cable with an inline switch which allows you to toggle the device ON or OFF without the need to unplug. Simply wonderful!

> **[USB-C In-Line Power Switch Cable for Raspberry Pi 4](https://core-electronics.com.au/micro-usb-c-in-line-power-switch-cable-for-raspberry-pi-4.html)**
>
> This handy little guy is a female to male USB C cable with an inline switch which allows you to toggle the device ON or OFF on your Raspberry Pi 4.

---

<div class="post-metadata">

**Author:** ![Kevin48023](https://avatars.discourse-cdn.com/v4/letter/k/c0e974/32.png) [@Kevin48023](https://forum.core-electronics.com.au/u/Kevin48023)\
**Post date:** [27 January 2021 23:30 UTC](https://forum.core-electronics.com.au/t/switching-a-usb-connection-on-off/9268/3 "2021-01-27T23:30:33Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Bryce](https://yyz2.discourse-cdn.com/flex028/user_avatar/forum.core-electronics.com.au/bryce/32/3631_2.png) [@Bryce](https://forum.core-electronics.com.au/u/Bryce)\
**Post date:** [28 January 2021 04:10 UTC](https://forum.core-electronics.com.au/t/switching-a-usb-connection-on-off/9268/4 "2021-01-28T04:10:49Z")

</div>

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!
