Temperature Based Fan Control for the Raspberry Pi

The fan (3010S DC 5V 0.20A) I use in the NESPi CASE with a Rapsberry Pi 3 Model B+ is quite loud. To reduce the noise, a system to switch the fan on/off based on demand was implemented.


Several similar approaches have been implemented; e.g.: TheDrHax/rpi-fan-control and How to control a fan to cool the CPU of your RaspBerryPi - Edo Scalafiotti or lowerning the supply voltage to 3.3 V: https://www.reddit.com/r/RetroPie/comments/80410w/nespi_fan_always_on/

Control Circuit

The fan has two wires: +5 V and ground. It is controlled with a GPIO pin connected to a custom MOSFET circuit supplying the fan. The used n-channel IRF3710 MOSFET requires a Gate Threshold Voltage above the 3.3 V of the GPIO pin. Thus, a drive circuit similar to Fig 1. Inverting Driver was constructed (The pull down transistor R6 for the transistor base was omitted). Parts were chosen by what I had lying around.

The completed circuit:
circuit front
circuit back

The inverting configuration results in the default state of the fan being on. Which theoretically is safer with respect to high temperatures and also allows the takeover of the software control after startup to be audibly confirmed.


A hole was drilled in the support structure of a screw hole of the case. Using a zip tie through the MOSFET heat sink the circuit was affixed to the case.

installation of circuit

The circuit is connected to the 2 pin fan connector on the case circuit board and to GPIO pin 21 on the Rapsberry Pi with the green wire. The fan is connected to the 2 pin header on the control circuit.

Control Software

The software is a C++ program reading the CPU temperature from /sys/ and using the WiringPi library to control the fan with a GPIO pin. When the on or off threshold temperature is reached, the fan is switched on respectively off.

Due to the use of systemd for starting the program no special damonization code is needed (see: New-Syle Daemons). A .service file for systemd is provided.

Pulse Width Modulation

A try with Pulse Width Modulation (PWM) had no usable success; both with Software and Hardware Modulation. While the fan could be slowed somewhat (not to low percentages ~30%), the modulation increased the noise. A dedicated PWM input on the fan (4 wire connector) would probably be needed for a successful PWM.

Contrary success using Software PWM is noted by: soulgriever/FanGPIO and GPIO en C: Control automático de ventilador según temperatura - josrios

Source Code

The source code is at: