Control USB-powered devices with a Raspberry Pi
Power Point
Command-line tools and Node-RED on a Raspberry Pi let you control projects that use the USB ports.
For home automation projects, a Raspberry Pi offers a simple, low-cost approach to managing and controlling a wide variety of devices. Typically these devices are either digitally wired 0-5V devices such as motion detectors, or wireless Ethernet devices such as smart plugs. It's important to note that a Raspberry Pi can also control USB-powered devices, such as USB fans, lights, and low-end controllers.
In this article, I look at how to monitor, control, and measure USB power in two Raspberry Pi projects. The first project uses Node-RED to create a web dashboard to monitor and control USB lights. The second project turns on USB cooling fans according to the Pi's CPU temperature.
Controlling USB Ports
A number of techniques allow you to control USB ports, and I found that one of easiest approaches is to use the uhubctl
[1] utility, which lets you view and control local USB ports and ports on smart USB hubs. To load this utility, enter:
sudo apt-get install libusb-1.0-0-dev git clone https://github.com/mvp/uhubctl cd uhubctl make sudo make install
Figure 1 shows the output on a Raspberry Pi 4 with no USB devices connected. The Pi 4 has two internal USB hubs: Hub 1 connects to all the USB ports with the USB 2.10 standard, and hub 2 controls all the ports with the USB 3.00 standard and the Ethernet jack.
For the Raspberry Pi 3 and 4, the power on all USB ports is ganged together through port 2, so unfortunately it is not possible to power up and down an individual USB port.
The commands to turn on or off or toggle the USB ports and keep the Ethernet jack powered are:
sudo uhubctl -l 1-1 -p 2 -a on sudo uhubctl -l 1-1 -p 2 -a off sudo uhubctl -l 1-1 -p 2 -a toggle
These commands return messages showing the current status, the power requested state, and the new status.
Monitoring USB Power
The uhubctl
command lets you check the status of Pi port 2, the ganged power port (Figure 2). With some Bash statements, the power status is parsed to show just the off or power message. The Bash statement
$ sudo uhubctl | grep 'Port 2' | awk '{print $4}' off
shows the power status on a Node-RED dashboard.
Node-RED USB Control Dashboard
Node-RED [2] is a visual programming tool included with the full desktop Raspberry Pi install. If Node-RED has not been installed, see the online docs [3].
A number of low-cost USB lighting options can be used with a Raspberry Pi (Figure 3), including LED strips, wire lights, and small USB lamps. Node-RED doesn't have a node to monitor or control USB power, but Bash commands can be used directly in Node-RED.
A simple Node-RED dashboard can be created to turn Raspberry Pi USB ports on and off and check the status of power on these ports. The logic (Figure 4) would include two dashboard button nodes, one dashboard text node, and two exec nodes. The uhubctl
utility can be used directly in the exec nodes.
The first exec node contains the Bash command to turn the USB ports on or off (Figure 5). The on or off string is sent from the dashboard buttons as a msg.payload
message that is appended to the command in the exec node. The output from the first exec node triggers the second exec node to get the latest USB port status.
The USB power status message can be made more presentable by editing the Value format field in the dashboard text node. For this example, I used an <h1>
heading and uppercase
formatting (Figure 6).
Once the logic is complete, the Deploy button on the right side of the menubar will make the dashboard available to web clients at: https://raspberry_pi_address:1880/ui. For this project, I added an enhancement to include a countdown or sleep timer (Figure 7).
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
-
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.
-
Latest Cinnamon Desktop Releases with a Bold New Look
Just in time for the holidays, the developer of the Cinnamon desktop has shipped a new release to help spice up your eggnog with new features and a new look.
-
Armbian 24.11 Released with Expanded Hardware Support
If you've been waiting for Armbian to support OrangePi 5 Max and Radxa ROCK 5B+, the wait is over.
-
SUSE Renames Several Products for Better Name Recognition
SUSE has been a very powerful player in the European market, but it knows it must branch out to gain serious traction. Will a name change do the trick?
-
ESET Discovers New Linux Malware
WolfsBane is an all-in-one malware that has hit the Linux operating system and includes a dropper, a launcher, and a backdoor.
-
New Linux Kernel Patch Allows Forcing a CPU Mitigation
Even when CPU mitigations can consume precious CPU cycles, it might not be a bad idea to allow users to enable them, even if your machine isn't vulnerable.
-
Red Hat Enterprise Linux 9.5 Released
Notify your friends, loved ones, and colleagues that the latest version of RHEL is available with plenty of enhancements.
-
Linux Sees Massive Performance Increase from a Single Line of Code
With one line of code, Intel was able to increase the performance of the Linux kernel by 4,000 percent.