Skip to content

About

Use WiringPi C library to control a PWM fan on Raspberry Pi

Resources

Stars

0 stars

Watchers

1 watching

Forks

Latest commit

 

History

20 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 

Repository files navigation

WiringPi-PWM-Fan-Control

Use WiringPi C library to control a 4-pin PWM fan on Raspberry Pi

After attempting software PWM using python, I quickly realized that it runs the CPU at around 20%. Using C, and the hardware PWM provided on the Pi drops that to virtually 0% CPU usage.

Depends on WiringPi - to install on Pi4B, I needed to install v2.52 as v2.50 on the apt repos wasn't compatible :
wget https://project-downloads.drogon.net/wiringpi-latest.deb
sudo dpkg -i wiringpi-latest.deb

If you're going to use the unofficial mirror (https://github.com/WiringPi/WiringPi) on a Pi4B, some code changes might need to be done to adjust for the new way it handles the 54MHz clock with this commit :
https://github.com/WiringPi/WiringPi/commit/651136a110d1a63320193d4f7d39e9399762847e

I'm running this on a Pi4B with a Delta ASB0305HP-00CP4 PWM fan.

Helpful guide for the hardware side of wiring a PWM 4-pin fan (also check fan datasheet for required values and resistor for tachometer) :
https://blog.driftking.tw/en/2019/11/Using-Raspberry-Pi-to-Control-a-PWM-Fan-and-Monitor-its-Speed/

I don't actually do anything with the RPM tachometer readings, other than display them on the socket, and in debug mode, but feel free to add functionality, or remove the feature entirely if you don't need it.

This is my first attempt at coding using C, so I make no guarantees with this code, simply wanted to share for anyone that wants to build upon this themselves.

Configuring :

This is configured to use the WiringPi pin numbering, to see a list of pins, this command will display a little table :
gpio readall

I haven't built in any kind of config file or arguments for this, so all the settings must be changed in the source.

pwm_pin : GPIO pin (WiringPi numbering) which connects to fan's PWM wire
pi_freq : Base frequency in Hz of Pi - 54 MHz for Pi4B (19.2MHz for older models)
pwm_freq : Fan PWM frequency in Hz (see fan datasheet for this value)
speed_start : Fan speed in % to start fan from dead stop (see fan datasheet for this value)
speed_min : Minimum fan speed in % (my fan couldn't reliably run under 10% speed)
speed_max : Maximum fan speed in % (depending on your temp_max, this speed may never get reached)
temp_min : Temperature in °C to stop fan
temp_max : Temperature in °C to set fan to speed_max
temp_start : Don't start fan until this temperature in °C is reached
tach_pin : GPIO pin (WiringPi numbering) which connects to the tach wire on fan, along with 3.3V with resistor
tach_pulse : Number of tachometer pulses per fan revolution (see datasheet for this value)
refresh_time : Seconds to wait between updates
port : Port used by localhost UDP socket
debug : Set to 1 to print debug message or 0 to run silently
email : Send notification to this e-mail on error using msmtp

Compile with :

gcc -o wpi wpi.c -lwiringPi -lpthread
chmod +x wpi

Run with :

sudo ./wpi
You can also create a systemd service to launch this as a service on startup.

Web interface :

The WiringPi-PWM-Fan-Control listens on localhost UDP port configured above.
Anything capable of connecting to this socket can get basic stats from the fan, i've included a simple CGI client (fan.c).
It simply provides RPM, temperature and current PWM speed.
To use it, install a CGI compatible web server, I use lighttpd :
sudo apt-get install lighttpd
sudo lighty-enable-mod cgi
sudo service lighttpd force-reload
sudo gcc -o /usr/lib/cgi-bin/fan.cgi fan.c
sudo chmod +x /usr/lib/cgi-bin/fan.cgi
Access via http://<pi-ip-address>/cgi-bin/fan.cgi

E-mail error notifications :

I've also added the ability to send e-mail notifications if an error occurs.
I use msmtp, and simply lookup how to config /etc/msmtprc for your own SMTP service (I use gmail)
sudo apt-get install msmtp

About

Use WiringPi C library to control a PWM fan on Raspberry Pi

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages