https://github.com/jokerqyou/fanctl
A simple thermal fan controller for Raspbian
https://github.com/jokerqyou/fanctl
c fan raspberry-pi raspbian temperature-control
Last synced: 18 days ago
JSON representation
A simple thermal fan controller for Raspbian
- Host: GitHub
- URL: https://github.com/jokerqyou/fanctl
- Owner: JokerQyou
- License: bsd-3-clause
- Created: 2020-06-02T09:48:04.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2020-07-03T06:55:47.000Z (almost 6 years ago)
- Last Synced: 2025-08-08T04:33:11.726Z (10 months ago)
- Topics: c, fan, raspberry-pi, raspbian, temperature-control
- Language: C
- Homepage: https://blog.mynook.info/post/rpi-gpio-thermal-fan/
- Size: 15.6 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# fanctl
A simple thermal fan controller for Raspbian.
[中文文档](https://blog.mynook.info/post/rpi-gpio-thermal-fan/)
# Hardware
## Required parts
- 5V DC fan x1, 3mm / 4mm or any other size suitable for your Raspberry Pi.
- 2N2222 NPN transistor x1.
- 680ohm resistor x1.
- (Optional) small piece of PCB. If you don't have one, it is also possible to solder all parts directly to the fan wires..
## The circuit

Or you could reference to [this post](https://howchoo.com/g/ote2mjkzzta/control-raspberry-pi-fan-temperature-python#a-explanation-of-the-circuit) for connecting your parts together.
Choose any GND / VCC (5V) pin to supply power. For the controller pin, use [GPIO 8 (AKA BCM 14)](https://pinout.xyz/pinout/pin8_gpio14).
# Software
## Install dependencies
- First of all update apt cache: `sudo apt update`.
- Install `cmake`, `gcc` and `git`, as you would do for all other C projects: `sudo apt install cmake build-essential gcc git`.
- Install the [MRAA lilbrary](https://github.com/eclipse/mraa) for accessing GPIO:
- Clone MRAA project: `git clone https://github.com/eclipse/mraa.git`
- Enter the cloned repository: `cd mraa`
- Prepare to build: `mkdir build && cd build`
- Generate makefiles: `cmake ..`
- Build: `make`
- Install the built tools: `sudo make install`
- Refresh linker related stuff so MRAA library can be located by gcc: `sudo ldconfig -v`
- To verify you have correctly installed the MRAA library, run `mraa-gpio version`, it should print the model of your Raspberry Pi
## Build this project
- Clone this project: `git clone https://github.com/JokerQyou/fanctl.git`
- Enter the cloned repository: `cd fanctl`
- Prepare to build: `mkdir build && cd build`
- Generate makefiles: `cmake ..`
- Build: `make`
- Install: `sudo make install`
## Start service
- Reload systemd units: `sudo systemctl daemon-reload`
- To verify you have correctly installed the service, run `sudo systemctl status fanctl`, it should print some information about this tool
- To start the service: `sudo systemctl start fanctl`
When the chip temperature reaches 60°C, the fan will be started by pulling `FAN_VCC_PIN` to high (causing the transistor to close). When the chip temperature drops to 45°C or below, the fan will be stopped by pulling `FAN_VCC_PIN` to low. When the fan is started or stopped, the corresponding temperature value will be written to journald logs, to follow the log: `sudo journalctl -u fanctl -f`.
## Configure
Edit the installed service file (in `/etc/systemd/system`), use `THRESHOLD_ON` and `THRESHOLD_OFF` to set temperature limits. ON is the upper limit, fan starts when chip temperature reaches above it; OFF is the lower limit, fan stops when chip temperature drops below it.
```systemd service
[Service]
# ...
Environment=THRESHOLD_ON=50
Environment=THRESHOLD_OFF=30
```
You'll need to reload the daemon before restarting the service:
```bash
sudo systemctl daemon-reload
sudo systemctl restart fanctl
```
To use a different pin for controlling the fan, modify `FAN_VCC_PIN` (Notice: MRAA library uses the **physical pin number**). Then rebuild and reinstall again. Service will need to be restarted.
## Why C
Well, I've actually written a Python script and installed that as a service, it kept working for almost 6 months. But I don't think such a simple job is worth using 15MB of memory. The memory footprint dropped under 400KB after transforming to C.