How to turn Fan Shim Fan off on shutdown

One minor annoyance for me is that the Fan on the Fan shim keeps running when I do a shutdown from Raspbian on my Pi 4B. This isn’t a complaint, more of an observation type deal.
Why it does this is I believe a failsafe mode. If you install the Fan Shim and don’t install any software the fan will run and cool your Pi. Thats what you want to happen. You have to pull the fan control pin low to turn it off. Thats what the control Daemon does.
When you do a shut down the Daemon stops running and the control pin (GPIO pin) goes to a floating state and the fan runs. You have to unplug the power supply to stop it. Not a big deal for most people. I like to use the Fan Shim button to do the shut down, and boot the Pi up again when pressed again. That works fine, I just don’t want the fan running when its off.

I found a really easy solution that works for me. I just wired a 10k resistor from the Fan Shim, Fan Control pin, GPIO 18 physical pin 12 to ground. A pull down resistor that will make the default state of the fan off. You will have to command it on, with either the dtoverlay=gpio-fan config.tx entry, or via the Pimoroni software Daemon. I tested both and it worked as I wanted. The Fan turned on and off when it should with the Daemon, and turned on as it should with the dtovery entry. Pressing the button on the Fan Shim to do a shutdown had the fan stop. And stay off even with the power supply plugged into the Pi. Pressing the button again on boot up the fan was off if the temp was below the on threshold. :D

I like to just leave the fan running continuously while the Pi is on, just my personal preference, my temps hover right around 40c. I added this entry to my config.txt to turn the fan on at 30c. I don’t use the Daemon. You can with my mod but I don’t.
dtoverlay=gpio-fan,gpiopin=18,temp=30000
For the shutdown function I added this line to config.txt
dtoverlay=gpio-shutdown,gpio_pin=17,active_low=1,gpio_pull=up
You could just add dtoverlay=gpio-shutdown and it will work via GPIO 3, thats the pin used to do the boot up when the button is pressed. If you turn i2c on though the shutdown via GPIO 3 stops working which is why I went with the GPIO 17 entry.
Disclaimer, my mod breaks the fail safe auto on function. You will have to command the fan to run to have it turn on. Failing to do that will have your Pi potentially running Hot, it won’t overheat though, it will just down clock and you’ll lose performance. Install the Pimoroni daemon and thats not an issue, the fan will turn on when needed and off when things are cool.

If the fan is connected to the 5V and GND pins then they are not controlled by software. Those two pins are directly connected to your power supply, so as long as the power supply is connected and turned on at the wall socket your fan will continue to run.

The Fan Shim has a fan control circuit that can turn the Fan on and off via a GPIO Pin. If GPIO 18 is pulled low the fan turns off. Thats how its turned on and off based on temperature. On shutdown though that pin floats and the fan turns back on.
One way around this is to use
dtoverlay=gpio-poweroff,gpiopin=18,active_low=1
On shutdown GPIO pin 18 will be held low and turn the fan off. The downside to this is you can’t boot the Pi back up by grounding GPIO 3, you have to power cycle it or ground the reset or global enable pin.

See this thread for more info on this.