Adapted from Tim Etherington's guide.
Bash scripts for automatic & manual control of USB-powered fan on Raspberry Pi. Supported Raspberry Pi models:
- B+
- 2B
- 3B
- 3B+
- 4B
The libusb-1.0
library is required for uhubctl (>=1.0.16
recommended)
sudo apt-get install libusb-1.0-0-dev
Install the uhubctl system package
sudo apt install uhubctl
Clone this repository & provide the scripts read and write privileges
git clone https://github.com/feram18/pi-cooldown.git
cd pi-cooldown
sudo chmod 755 cooldown.sh power.sh
Firstly, you will need to figure out the hub you want to control according to the Raspberry Pi model you have. From uhubctl's documentation:
Single hub
1-1
, ports 2-5 ganged, all controlled by port2
:uhubctl -l 1-1 -p 2 -a 0
Trying to control ports
3
,4
,5
will not do anything. Port1
controls power for Ethernet + WiFi.
Main hub
1-1
, all 4 ports ganged, all controlled by port2
(turns off secondary hub ports as well). Port1
connects hub1-1.1
below, ports2
and3
are wired outside, port4
not wired.uhubctl -l 1-1 -p 2 -a 0
Secondary hub
1-1.1
(daisy-chained to main): 3 ports, port1
is used for Ethernet + WiFi, and ports2
and3
are wired outside.
⚠️ If your VL805 firmware is older than00137ad
(check withsudo rpi-eeprom-update
), you have to update firmware to make power switching work on RPi 4B.
USB2 hub
1
, 1 port, only connects hub1-1
below.USB2 hub
1-1
, 4 ports ganged, dual to USB3 hub2
below:uhubctl -l 1-1 -a 0
USB3 hub
2
, 4 ports ganged, dual to USB2 hub1-1
above:uhubctl -l 2 -a 0
USB2 hub
3
, 1 port, OTG controller. Power switching is not supported.
Once you have figured out the hub you want to control. Edit the PORT
variable on both the cooldown.sh
and power.sh
scripts. By default, it is set to 1-1
. If you had to edit the default value, make sure to give the script privileges
once again as previously demonstrated.
The cooldown.sh
script handles the automatic control. By creating a cron job, you can set the script to run
automatically at a given rate.
sudo crontab -e
Get the path to your pi-cooldown
directory and add the following line at the bottom of the file, so that the script
runs every 5 minutes and determines if the fan should be turned on:
*/5 * * * * <path to your pi-cooldown directory>/cooldown.sh
Your line will look something like */5 * * * * /home/pi/pi-cooldown/cooldown.sh
. You can also edit how frequent the
script gets executed as you desire:
┌───────────── minute (0 - 59)
│ ┌───────────── hour (0 - 23)
│ │ ┌───────────── day of the month (1 - 31)
│ │ │ ┌───────────── month (1 - 12)
│ │ │ │ ┌───────────── day of the week (0 - 6) (Sunday to Saturday;
│ │ │ │ │ 7 is also Sunday on some systems)
│ │ │ │ │
│ │ │ │ │
* * * * * <command to execute>
The power.sh
script allows you to manually control the power state of the Raspberry Pi's hub, by providing the on
or off
argument accordingly.
~/pi-cooldown/power.sh on
~/pi-cooldown/power.sh off
Both the cooldown.sh
and power.sh
write logs to the pi-cooldown.log
file so you can keep track of the power state
changes.