Control USB-powered devices with a Raspberry Pi
Rasp Pi Cooling Fan
Raspberry Pis have a number of cooling options that use the GPIO (general purpose input/output) pins to control and power external fans. A similar approach allows you to use USB fans. For this project, I used two littleBits fans [4] that I placed on a littleBits mounting plate (Figure 8).
The first step in this fan cooling project is to get the Pi's CPU temperature, which you can get with the vcgencmd measure_temp
command and then a grep
to extract just the floating-point value of the temperature:
$ vcgencmd measure_temp temp=45.7'C $ # Show just the temperature value $ vcgencmd measure_temp | grep -Eo '[0-9]+.+[0-9]' 45.7
To check whether one number is greater than another, I use the bc
(arbitrary precision calculator) command with the math library (-l
) option:
$ # Check number1 > number2. True=1 $ echo "33.4 > 36.1" | bc -l 0 $ echo "38.4 > 36.1" | bc -l 1
Now that all the basics are worked out, a simple script (Listing 1) can check the temperature against a high limit every 10 seconds and turn the USB power on and off as required.
Listing 1
Pi Cooling Script
01 #!/bin/bash 02 # 03 # Check the Pi temperature against a temperature high limit 04 # Turn on/off USB power (to fans) as required 05 # 06 tlim="46.0" 07 while :; 08 do 09 # get the temperature 10 tnow=$(vcgencmd measure_temp | grep -Eo '[0-9]+.+[0-9]' 11 # check the CPU temp vs. the limit 12 if (( $(echo "$tnow > $tlim" | bc -l ) )) ; then 13 # CPU temp is above limit, turn on fan 14 sudo uhubctl -l 1-1 -p 2 -a on 1>&- 15 else 16 # CPU temp is below limit, turn off fan 17 sudo uhubctl -l 1-1 -p 2 -a off 1>&- 18 fi 19 sleep 10 20 done
The uhubctl
command outputs status messages after it powers the USB ports on and off. For a quiet command, 1>&-
can be added at the end of the line.
Other Controllers
A Raspberry Pi can control the power to other controllers. Figure 9 shows a Pi 4 powering an Arduino Uno, an Arduino Nano (clone), and a BBC micro:bit controller.
For external modules that don't support WiFi or real-time clocks, a Raspberry Pi could be used as an easy way to power these external controllers up and down.
It's important to realize that a Raspberry Pi is not designed to power devices that have a high power requirement. The Raspberry Pi 3 and 4 have a maximum USB port output of 1200mA for all four ports combined (1200mA is available on a single port if no others are in use). This 1200mA limit assumes that the Pi is getting its required input power, which is 2.5A for the Pi 3 and 3A for the Pi 4.
If you are connecting smart USB devices such as memory sticks or third-party controllers, the device manufacturer has a defined MaxPower
rating that can be found once the device is connected. The command lsusb -v
outputs a very long list of vendor information for all the connected devices. To get just the maximum power for each device on the Raspberry Pi USB internal bus, enter:
lsusb -v 2>&- | grep -E 'Bus 00|MaxPower'
When this command is run with an Arduino Nano, Arduino Uno, and a BBC micro:bit, the total power requirements can be seen on a per-port basis (Figure 10). In this example, the total USB power used is 796mA (0+100+500+96+100+0), which is within the Raspberry Pi specs.
A Bash command to total the USB bus power requirements for all connected devices is:
$ lsusb -v 2>&- | grep MaxPower | grep -o -E '[0-9]+' | awk '{ sum += $1} END {print "\nTotal= " sum " mA"}' Total= 796 mA
Unfortunately, simple USB-powered devices such as USB lights and fans use the USB connection strictly for power, so they do not appear in the lsusb
output. To find the power requirements for these kinds of devices, you will have to reference the manufacturers' literature.
Final Comments
For home automation projects I prefer direct-wired GPIO pin connections or WiFi devices over USB-powered devices; however, it's nice to know that you have the USB option if you need it.
For kids' projects that use littleBits or micro:bits, a Raspberry Pi as a power source offers a nice, easy way to control or schedule their use.
Infos
- uhubctl docs: https://github.com/mvp/uhubctl
- Node-RED: https://nodered.org/
- Node Red docs: https://nodered.org/docs/getting-started/raspberrypi
- littleBits fan: https://sphero.com/products/fan?_pos=1&_sid=19532771f&_ss=r
« Previous 1 2
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
News
-
Gnome 48 Debuts New Audio Player
To date, the audio player found within the Gnome desktop has been meh at best, but with the upcoming release that all changes.
-
Plasma 6.3 Ready for Public Beta Testing
Plasma 6.3 will ship with KDE Gear 24.12.1 and KDE Frameworks 6.10, along with some new and exciting features.
-
Budgie 10.10 Scheduled for Q1 2025 with a Surprising Desktop Update
If Budgie is your desktop environment of choice, 2025 is going to be a great year for you.
-
Firefox 134 Offers Improvements for Linux Version
Fans of Linux and Firefox rejoice, as there's a new version available that includes some handy updates.
-
Serpent OS Arrives with a New Alpha Release
After months of silence, Ikey Doherty has released a new alpha for his Serpent OS.
-
HashiCorp Cofounder Unveils Ghostty, a Linux Terminal App
Ghostty is a new Linux terminal app that's fast, feature-rich, and offers a platform-native GUI while remaining cross-platform.
-
Fedora Asahi Remix 41 Available for Apple Silicon
If you have an Apple Silicon Mac and you're hoping to install Fedora, you're in luck because the latest release supports the M1 and M2 chips.
-
Systemd Fixes Bug While Facing New Challenger in GNU Shepherd
The systemd developers have fixed a really nasty bug amid the release of the new GNU Shepherd init system.
-
AlmaLinux 10.0 Beta Released
The AlmaLinux OS Foundation has announced the availability of AlmaLinux 10.0 Beta ("Purple Lion") for all supported devices with significant changes.
-
Gnome 47.2 Now Available
Gnome 47.2 is now available for general use but don't expect much in the way of newness, as this is all about improvements and bug fixes.