Control your Raspberry Pi FAN using hardware PWM

Chelaru Adrian
1 min readOct 26, 2023

If you’re a Raspberry Pi enthusiast, you may have encountered the need to control the fan speed for cooling your Pi when it’s under heavy load.

Here I’ll show you how to do that using a simple Python script and a library that controls the PWM signal using a hardware driver, that way we can use higher frequencies and there’s no CPU load.

PWM signal example, copyright

The Python library that is needed in this case: rpi-hardware-pwm.

Python hardware PWM FAN control script

This is the script, and if you want to add it on the startup of the Raspberry Pi, then scroll at the bottom for installing instructions 🥳.

Script is based on DriftKingTW software PWM implementation.

Installing instructions and run at the startup

These instructions will automatically put the script into the /usr/local/bin/fan_control.py and get the service to run it at startup:

Here’s the fan_control.service, if you want to take a quick look:

--

--

Chelaru Adrian

💻 Hey, I'm here to share my passion for tech! 🚩 Follow me on Medium for exciting new ideas and innovative solutions.