Raspberry Pi 4 Model B: External Power Button With USB Fan

How I Got Here

I recently built a Printer-box enclosure for my Prusa i3 MK3S. My mantra is go big or go home, so I went for the whole package with low-voltage LED’s, exhaust fan, 4Gb Raspberry Pi 4B, Logitech C920 camera and modular control panel. My electronics skills are weak at best, so I followed a combination of the printer-box wiring diagrams and Colin Hill’s awesome videos.

After everything was done, I discovered the problem. The Pi 4 Rev 1.1 doesn’t power off the USB ports when it goes into standby. I’d click the pushbutton to stop the Pi, then have to unplug the Pi or buck converter to stop the fan. I found an alternate config that would stop the fan, but it also prevented the Pi from rebooting when the button was pushed again.

If you’re interested in the details, you can read all about WAKE_ON_GPIO and POWER_OFF_ON_HALT.

How To Fix It

pi@octopi:~ $ sudo apt-get install libusb-1.0-0-dev
pi@octopi:~ $ git clone https://github.com/mvp/uhubctl
pi@octopi:~ $ cd uhubctl
pi@octopi:~ $ sudo make install
  • Edit the bootloader configuration:
pi@octopi:~ $ sudo -E rpi-eeprom-config --edit
    • Verify WAKE_ON_GPIO and POWER_OFF_ON_HALT are configured as follows:
WAKE_ON_GPIO=2
POWER_OFF_ON_HALT=0
    • Save the configuration, reboot and verify:
pi@octopi:~ $ sudo reboot
pi@octopi:~ $ rpi-eeprom-config

[all]
BOOT_UART=0
WAKE_ON_GPIO=2
POWER_OFF_ON_HALT=0
DHCP_TIMEOUT=45000
DHCP_REQ_TIMEOUT=4000
TFTP_FILE_TIMEOUT=30000
ENABLE_SELF_UPDATE=1
DISABLE_HDMI=0
BOOT_ORDER=0xf41
pi@octopi:~ $
  • Identify the USB hubs:
pi@octopi:~ $ sudo uhubctl
Current status for hub 2 [1d6b:0003 Linux 5.10.103-v7l+ xhci-hcd xHCI Host Controller 0000:01:00.0, USB 3.00, 4 ports, ppps]
Port 1: 02a0 power 5gbps Rx.Detect
Port 2: 02a0 power 5gbps Rx.Detect
Port 3: 02a0 power 5gbps Rx.Detect
Port 4: 02a0 power 5gbps Rx.Detect
Current status for hub 1-1 [2109:3431 USB2.0 Hub, USB 2.10, 4 ports, ppps]
Port 1: 0100 power
Port 2: 0100 power
Port 3: 0503 power highspeed enable connect [046d:082d HD Pro Webcam C920 5B5DD3AF]
Port 4: 0103 power enable connect [2c99:0002 Prusa Research (prusa3d.com) Original Prusa i3 MK3 CZPX2118X004XK64851]
Current status for hub 1 [1d6b:0002 Linux 5.10.103-v7l+ xhci-hcd xHCI Host Controller 0000:01:00.0, USB 2.00, 1 ports, ppps]
Port 1: 0503 power highspeed enable connect [2109:3431 USB2.0 Hub, USB 2.10, 4 ports, ppps]
pi@octopi:~ $

I know my fan is connected to the same hub as my webcam & MK3, so I’m going to target hub 1-1 during shutdown.

  • Use your favorite text editor to create the magic script in the `/lib/systemd/system-shutdown` directory with the following contents:
#!/bin/bash

echo "Stopping USB..."
uhubctl -l 1-1 -a off

The -l limits the operation to hub 1-1. The command will not operate on multiple hubs at once. The -a turns the USB off (or on). Note that this command turns off all ports. This should be fine because there’s no reason to keep any ports running if you’re going into standby.

  • Make the script executable and reboot
pi@octopi:~ $ chmod 0755 /lib/systemd/system-shutdown/name-of-script
pi@octopi:~ $ sudo reboot

That’s it. You should now be able to use the pushbutton to turn the Pi and fan on and off.

 

Posted in Uncategorized.

Leave a Reply

Your email address will not be published. Required fields are marked *