I have a project which will require using a Raspberry Pi in a low power environment, but it will also require a 3G connection. I have not done any tests, yet, but I am under the assumption (and correct me if I am wrong) that a 3G modem plugged into a Raspberry Pi and connected to a network will use power, and given the environment, any extra power usage is a non requirement. So, how, programmatically, can I turn on and off a 3G modem, or the USB connector on a Raspberry Pi?
Sadly there is no way to fully power off with software alone. You will need to add a switching circuit connected over the GPIO pins to trigger a "hardware disconnect" by cutting power to your hardware.
To switch with GPIO it's just a simple transistor circuit.
B is attached to your GPIO pin. Depending if it's HIGH or LOW the transistor is on or off.
E you put the power line to the USB-Device (RL).
For more detailed information better look at this site: http://www.kpsec.freeuk.com/trancirc.htm#switching and specially at http://www.kpsec.freeuk.com/trancirc.htm#ic
I would not advise you to turn an USB device ON/OFF just by using a transistor base switch because proper power ON/OFF protocol and electrical interface should be used as defined by the USB specifications.
I would rather use a switchable USB hub that allows you to turn ON/OFF any of it's downstream ports through software by using a simple command.
Something like this USB switchable hub should work for what you want.
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. But keep in mind that this hardware only supports turning off or on all USB ports at once, not independently. However, USB and Wifi+Ethernet power rails can be controlled independently (except for 4B, which doesn't use USB interface for networking).
Use it like this:
List all devices that can be controlled:
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 4 is very different from previous models as it has USB3 chip. You would use command like this to turn power off (all ports):
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.
Hi I agree with @electrfunch since I am working on a Project of controlling devices on the USB ports. Instead of on-board Electronic Circuit proposed by @Mose, I created my own Isolation Board using Relays and looks similar to the USB Switch Hub and this makes sense since even though initially ignored- Currents within the Raspberry Pi will start becoming a criterion on a longer run which might affect your Pi performance.
For what I mean, you could refer these manuals for Low Current measurements: