ilagi01
Posts: 15
Joined: Sat Nov 15, 2014 7:01 pm

Powering USB on and off

Sat Nov 22, 2014 6:50 pm

Hi,
I'm building a RPi based system in a remote location running from Solar panels and battery, using Sleepy Pi to shut itself off overnight, which would be periodically snapping pictures and upload through GPRS.
Unfortunately the webcam must be located 30 feet away from the RPi, so I use a long USB cable. That is working fine, but obviously requires quite some current for the long cable, so I would like to programmatically shut the power off for the given USB device and then switch it on when I need to snap a picture to minimize power use.
I tried different techniques to power the usb off and on in raspbian, but no luck.

Any idea?

Thanks

User avatar
davidcoton
Posts: 5026
Joined: Mon Sep 01, 2014 2:37 pm
Location: Cambridge, UK
Contact: Website

Re: Powering USB on and off

Sat Nov 22, 2014 7:37 pm

Sorry I don't have a direct answer, but just to note that a long cable should not draw any more supply current than a short one. Signalling might need more, but that is unlikely to be significant.
Have you made any measurements of current to base your calculations on?
Signature retired

ilagi01
Posts: 15
Joined: Sat Nov 15, 2014 7:01 pm

Re: Powering USB on and off

Sat Nov 22, 2014 8:58 pm

Thanks, yes, I did measurements.

I have the RPi's power supply running through a voltage meter (voltage and current) and I connected the webcam to the RPi directly, snapped a picture and then connected it through the USB cable and there is quite of a different in current used.
With the 10m USB extension cable the RPi uses about 0.10A more.

I guess this is because it isn't just a 10M USB cable (as 10m would be too long of a USB cable), but it needs a repeater to be able to cover the 10M reliably. I'm using this one: http://www.monoprice.com/Product?c_id=1 ... 1&format=2

I will actually will need an even longer one, 25M (http://www.monoprice.com/Product?c_id=1 ... 1&format=2), so power usage will certainly be even more of an issue, hence my question whether I can switch off and on the power to the USB programmatically on the RPi.

User avatar
aTao
Posts: 1093
Joined: Wed Dec 12, 2012 10:41 am
Location: Howlin Eigg

Re: Powering USB on and off

Sat Nov 22, 2014 9:17 pm

How about using SleepyPi to turn off more often than overnight?

Is the RPi doing anything other than taking and transmitting pictures? How often do you want to take a picture?
You might also get some power saving by storing several pictures and transmitting them as a compressed folder.
>)))'><'(((<

ilagi01
Posts: 15
Joined: Sat Nov 15, 2014 7:01 pm

Re: Powering USB on and off

Sat Nov 22, 2014 9:23 pm

I already do use the Sleepy Pi to turn off the RPi not only for the nights, but through the day too.
Basically it only turns on the RPi to take the picture and then powers it down, but it would help a lot if the USB could be switched on to take the pictures and shut down after, as taking the picture only takes 5-10 seconds, but uploading it and other things take several minutes, so having the USB powered for that period is completely wasteful.
Hence my question how to progammatically switch the power for the USB off and on.

User avatar
aTao
Posts: 1093
Joined: Wed Dec 12, 2012 10:41 am
Location: Howlin Eigg

Re: Powering USB on and off

Sat Nov 22, 2014 9:59 pm

Aha. I get it.

Answer: possibly but its not going to be on a plate.

I doubt very much if the extender can be switched off this way, it depends on the camera firmware wether it can be switched off.

One method could be to use a GPIO pin and MOSFET to cut the power on a mangled USB lead. Or if you have a B+ board there might be links for power to each USB port stack. Or, if you are using a USB hub you might do the same trick inside it.
>)))'><'(((<

ilagi01
Posts: 15
Joined: Sat Nov 15, 2014 7:01 pm

Re: Powering USB on and off

Sat Nov 22, 2014 10:14 pm

Okey, thanks.
Yes, I have a Raspberry B+, so I will check out power links for the USBs.

ilagi01
Posts: 15
Joined: Sat Nov 15, 2014 7:01 pm

Re: Powering USB on and off

Sat Nov 22, 2014 10:35 pm

I have managed to do it.

It turns out there is a way to turn off all USBs (including the network), which works for me just fine.
See:

#!/bin/bash
#Code to stop
/etc/init.d/networking stop
echo 0 > /sys/devices/platform/bcm2708_usb/buspower;
echo “Bus power stopping”

#!/bin/bash
#Code to start
echo 1 > /sys/devices/platform/bcm2708_usb/buspower;
echo “Bus power starting”
sleep 2;
/etc/init.d/networking start

Source: http://babaawesam.com/2014/01/24/power- ... pberry-pi/

drgeoff
Posts: 10765
Joined: Wed Jan 25, 2012 6:39 pm

Re: Powering USB on and off

Sat Nov 22, 2014 11:01 pm

While that may power down the LAN/USB chip I doubt that it removes power from anything plugged into the USB sockets.

On the Model B the only component between the 5 volt micro-USB power input and the 5 volt pins of the USB sockets is the polyfuse. (And another polyfuse on the first versions.)

Switching the 5 volt line to a USB device while leaving the two data lines connected may not be a good idea either. It is not for nothing that USB plugs and sockets are designed so that that outer two pins make connection before the inner two.

ilagi01
Posts: 15
Joined: Sat Nov 15, 2014 7:01 pm

Re: Powering USB on and off

Sun Nov 23, 2014 1:38 am

I did measure the current and turning the LAN/USB chip this way does certainly cuts the power, as the current goes down quite a lot on RPi B+.
Having an Ethernet cable, usb keyboard and webcam connected on that 10m USB I'm measuring 0.4A, while running the above to turn off the LAN/USB chip that goes down to 0.2A.
Turning it back on brings it back to 0.4A, so as far as power consumption goes this seems to be working as advertised.

I'm not sure about what concern cutting the power but not the data lines would be.

drgeoff
Posts: 10765
Joined: Wed Jan 25, 2012 6:39 pm

Re: Powering USB on and off

Sun Nov 23, 2014 11:20 am

ilagi01 wrote:I did measure the current and turning the LAN/USB chip this way does certainly cuts the power, as the current goes down quite a lot on RPi B+.
Having an Ethernet cable, usb keyboard and webcam connected on that 10m USB I'm measuring 0.4A, while running the above to turn off the LAN/USB chip that goes down to 0.2A.
Turning it back on brings it back to 0.4A, so as far as power consumption goes this seems to be working as advertised.

I'm not sure about what concern cutting the power but not the data lines would be.
1 Put your voltmeter on the power pins of a USB socket and try the software switch.
2. The concerns are that:
a. there is the possibility (though maybe extremely small) of damaging a device if the data lines are active before it is powered up.
b. there is the possibility that the device may not power up again in a properly working state if data lines are active before power is applied. Again the risk of this may be very small.
Look at the contacts in a USB plug.

User avatar
aTao
Posts: 1093
Joined: Wed Dec 12, 2012 10:41 am
Location: Howlin Eigg

Re: Powering USB on and off

Sun Nov 23, 2014 11:55 am

drgeoff wrote:
ilagi01 wrote:I did measure the current and turning the LAN/USB chip this way does certainly cuts the power, as the current goes down quite a lot on RPi B+.
Having an Ethernet cable, usb keyboard and webcam connected on that 10m USB I'm measuring 0.4A, while running the above to turn off the LAN/USB chip that goes down to 0.2A.
Turning it back on brings it back to 0.4A, so as far as power consumption goes this seems to be working as advertised.

I'm not sure about what concern cutting the power but not the data lines would be.
1 Put your voltmeter on the power pins of a USB socket and try the software switch.
2. The concerns are that:
a. there is the possibility (though maybe extremely small) of damaging a device if the data lines are active before it is powered up.
b. there is the possibility that the device may not power up again in a properly working state if data lines are active before power is applied. Again the risk of this may be very small.
Look at the contacts in a USB plug.
I was going to have a look at the USB spec but its a 27M pdf for USB2.0 more for 3(arghh)I did however see that there is part of the spec that governs standby. it is required that a device cuts its power requirements when told to. Perhaps shutting down the USB chip signals devices to go into standby. This would explain what ilagi01 has seen. Best to trawl through the spec though, but there is hope.

One question.. if you are shutting down the USB/ethernet, how is the RPi going to "phone home" with the images?
>)))'><'(((<

ilagi01
Posts: 15
Joined: Sat Nov 15, 2014 7:01 pm

Re: Powering USB on and off

Sun Nov 23, 2014 2:47 pm

Hi aTao,
Thanks, yes, that would explain why I observer the drop in power use when switching this off.
The Raspberry will be in a location where there is not wired internet or Wifi, so it is going to call home by using a GPRS board, to which I'm connecting through GPIOs, so shutting down the USB and Ethernet should not affect that.

ilagi01
Posts: 15
Joined: Sat Nov 15, 2014 7:01 pm

Re: Powering USB on and off

Sun Nov 23, 2014 3:15 pm

Hi drgeof,

1 Put your voltmeter on the power pins of a USB socket and try the software switch.
>> I did. Even after running the software switch to turn it off the power pins of the USB sockets still show 5V, but the total power use of the RPi B+ drops from 0.4A to 0.2A
2. The concerns are that:
a. there is the possibility (though maybe extremely small) of damaging a device if the data lines are active before it is powered up.
>> To minimize this I use a single script to snap the photo with the webcam, save it and when that is complete then run the software switch turning the power off for the chip while uploading the image to the server via the GPRS board and doing other stuff.
b. there is the possibility that the device may not power up again in a properly working state if data lines are active before power is applied. Again the risk of this may be very small.
>> Again in the same script I run the software switch on and then capture the webcam image.

So far it is working, although obviously I can't tell how reliable this will be in the long run, but the drop in the total power usage of the RPi is a measured fact.

drgeoff
Posts: 10765
Joined: Wed Jan 25, 2012 6:39 pm

Re: Powering USB on and off

Sun Nov 23, 2014 4:54 pm

To what value does the current consumption drop if, after the software switch, you physically unplug the USB device(s)?

User avatar
Burngate
Posts: 6302
Joined: Thu Sep 29, 2011 4:34 pm
Location: Berkshire UK Tralfamadore
Contact: Website

Re: Powering USB on and off

Sun Nov 23, 2014 5:58 pm

drgeoff wrote:
ilagi01 wrote:I'm not sure about what concern cutting the power but not the data lines would be.
The concerns are that:
a. there is the possibility (though maybe extremely small) of damaging a device if the data lines are active before it is powered up.
b. there is the possibility that the device may not power up again in a properly working state if data lines are active before power is applied. Again the risk of this may be very small.
Look at the contacts in a USB plug.
Given that the AP255x (which the B+ uses) is a device designed to limit or cut the power to USB devices in the event of a fault, and it only controls the +5v, I would guess that it's considered ok to leave the data lines uncontrolled, at least by the designers of that device

Of interest, for the B+, is that the Fault output of that chip, and the Enable input, are connected to the prtctrl-2 of the hub chip.
That means that if you can get the hub to pull its prtctrl down, power will be cut to the sockets.

jctlmm
Posts: 11
Joined: Fri May 08, 2015 12:22 am

Re: Powering USB on and off

Tue May 12, 2015 2:03 am

Are your ON/OFF scripts working like a charm? I have a similar need where I need to turn OFF the USB HUB while the unit is in idle state and when it is in operational mode I need to turn it ON; this is for the purpose to save as much power as possible since I'm running from a battery pack.

I have tried running the following script for testing/reliability purposes:

Code: Select all

while true
do
/etc/init.d/networking stop
echo 0 > /sys/devices/platform/bcm2708_usb/buspower;
echo "Bus power stopping"
sleep 10;
echo 1 > /sys/devices/platform/bcm2708_usb/buspower;
echo "Bus power starting"
sleep 2;
/etc/init.d/networking start
sleep 5;
ping google.com -c 2
sleep 30;
done
Unfortunately, in my case, after 15+ minutes the unit hangs and I need to manually reboot it...

Any ideas/suggestions?

ZPMMaker
Posts: 109
Joined: Sun Aug 23, 2015 11:04 am
Location: Australia

Re: Powering USB on and off

Tue Nov 28, 2017 4:47 am

I realise this conversation is a few years old, but I found it via Google when I was facing the same problem, so figured I'd share the below solution in case someone else needs it in future...

This solution works as-is on an RPi 3 model B running Raspbian Stretch.
http://embeddedapocalypse.blogspot.com. ... sb-or.html

Instructions copied below from source, just in case the source is taken offline in future...:
sudo apt-get update
sudo apt-get install libusb-dev
git clone https://github.com/codazoda/hub-ctrl.c
cd hub-ctrl.c
gcc -o hub-ctrl hub-ctrl.c -lusb
cp hub-ctrl ..
cd ..
Turn off Ethernet Port: sudo ./hub-ctrl -h 0 -P 1 -p 0
Turn on Ethernet Port: sudo ./hub-ctrl -h 0 -P 1 -p 1
Turn off all USB Ports: sudo ./hub-ctrl -h 0 -P 2 -p 0
Turn on all USB Ports: sudo ./hub-ctrl -h 0 -P 2 -p 1

Return to “Advanced users”