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 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.

F01-uhubctl.tif
Figure 1: USB power status with uhubctl.

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.

Burmakin_Andrey_123rf.psd
Figure 2: Monitoring port 2 for Pi USB power status.

Node-RED USB Control Dashboard

Node-RED is a visual programming tool included with the full desktop Raspberry Pi install. If Node-RED has not been installed, see the online docs.

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.

F03-pi_2lights.tif
Figure 3: Wire light string and LED lamp on a Raspberry Pi.

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.

F04-nr_usb_logic.tif
Figure 4: Node-RED logic to control and monitor Raspberry Pi USB ports.

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.

F05-nr_append_on_off.tif
Figure 5: Use buttons to pass on and off messages to a Bash USB power command.

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).

F06-nr_text_node.tif
Figure 6: Change formatting on dashboard text.

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).

F07-nr_ui.tif
Figure 7: Node-RED dashboard to control and monitor USB power.

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 that I placed on a littleBits mounting plate (Figure 8).

F08-pi_2fans.tif
Figure 8: Pi cooling with littleBits fans.

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.

F09-pi_w_controllers.tif
Figure 9: Raspberry Pi 4 powering other controllers.

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.

F10-pi_show_power.tif
Figure 10: The maximum power on all USB ports.

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.

Listings for this article.