Home Tech Raspberry Pi Raspberry Pi Automatic Fans Using L298n PWM

Raspberry Pi Automatic Fans Using L298n PWM

Article Length : 10 Minutes. Using L298N PWM to control two cooling fan units.


Welcome, We all know Raspberry Pi SBC Likes to get hot, And If your ambient temperature gets above 40 Degrees Celsius, A Pi will throttle down even at moderate loads. While the internet is filled with Cases, Heatsinks and Fans, We decided we need to combine all these things inorder to get optimal performance from rPi. Welcome to our guide Raspberry Pi Automatic Fans Using L298n PWM.

We will be using these things :

  • Heatsinks
  • Acrylic Case with Cooling Fan.
  • A 8×8 12v PC Cooling Fan.
  • L298n PWM Controller.
  • 12v 2A Power Supply.
  • Raspberry Pi (ofcourse) .

Getting Started : Assembling the Case & Heatsinks :

Many raspberry cases support a mini fan that can be connected to the GPIO of rPi :


We don’t recommend connecting the case fan to the GPIO, as drawing power for the fan heats up the raspberry pi. This also results in fan being always on, Which reduces its life.
While we assemble the case, lets also place heatsinks on our Pi. The hottest chips are the CPU, the Wifi and the USB chip. So the two ones with silver covers, and the largest of the 3 chips near the USB connectors.
This moves us one step closer to Raspberry Pi Automatic Fans Using L298n PWM module.

Raspberry Pi 4 8GB, USB Boot, 64-bit Pi OS & Updates


Attaching a Bottom Fan :

While the little top fan is nice and cute, We will be placing a PC radiator fan below our pi. You can get these 80x80mm DC 12V Cooling Fan for PC Case for as low as $5 (200 Rupees).
We will also have to mod our case, In our case, we cut the bottom Acrylic plate along its corner with a hot knife, This allows maximum air-flow to the bottom side of Raspberry Pi.

DC 12V Cooling Fan
A DC 12V Cooling Fan. Remember to place the fan so it pushes air to the bottom of the pi.

Setting up the L298n PWM Motor Driver with Raspberry Pi :

This L298N is a motor driver module for driving DC and Stepper Motors with Raspberry Pi. This module consists of an L298 motor driver IC and a 78M05 5V regulator. As L298N Module can control up to 4 DC motors (2 Motors per output) , or 2 DC motors with directional and speed control.


Our L298n will control both fans, The one on the case and one below the Pi. Here is a pin description of L298n PWM :

Pin Name


IN1 & IN2

Motor A input pins. Used to control the spinning direction of Motor A

IN3 & IN4

Motor B input pins. Used to control the spinning direction of Motor B


Enables PWM signal for Motor A


Enables PWM signal for Motor B


Output pins of Motor A


Output pins of Motor B


12V input from DC power Source


Supplies power for the switching logic circuitry inside L298N IC


Ground pin


Here is a nice video by PiddlerInTheRoot explaining the interfacing of L298n with Raspberry Pi GPIO :

Insert your Case fan inputs to IN1 on the L298N, and the bottom fans input at IN2. Don’t worry about the polarity of the wires since we can tweak it by code later. With the interfacing of L298N with the Raspberry Pi and both fans complete, We move onto mounting all these together in a nice package.

Mounting the Package :

We hope our Raspberry Pi Auto-Fans Using L298n PWM setup provides you with reference :

Raspberry Pi Automatic Fans Using L298n

The Top fan is mounted using screws, but we have changed the default fan supplied with the case with a custom fan using a DVD-Writer motor. This lets us spin the top fan with a little extra OomPh.

Raspberry Pi Automatic Fans Using L298n

Bottom Fan is stuck to the lower side of our Raspberry Pi Case using Super Glue. While this method makes removing of the case a bit difficult, the results are worth it.
We have used 3M Double Sided Adhesive Tape to stick the L298N PWM to the side of our Raspberry Pi. This makes all the wiring less cluttered and offers a nice mount to the module.

Python Script for L298n And Raspberry Pi Interfacing :

There are different ways in which you can set the fans to start. You can read the board temperature in Python using gpiozero package or you can set the fans to start at an interval for a period of time.
We have set our fans to turn on for every 2 Minutes, every 5 Minutes using this script :

import RPi.GPIO as gpio
import time
def init():
gpio.setup(17, gpio.OUT)
gpio.setup(22, gpio.OUT)
gpio.setup(23, gpio.OUT)
gpio.setup(24, gpio.OUT)
def run(tf):
gpio.output(17, False)
gpio.output(22, True)
gpio.output(23, False)
gpio.output(24, True)
print ("run")

Save this as fan.py in my documents

Place this code in Documents. We will now add this program to our crontab, Which would execute it every 5 Minutes. Open crontab by typing sudo crontab -e in the terminal, and adding these lines at the bottom of the file :

*/5 * * * * /home/pi/documents/fan.py

If you have created other scripts using different algorithms, Let us know in the comment section !

Read More : Sudo’ed : Android 7.1.1 Reviewed On Raspberry Pi 3 !

Result of Raspberry Pi Automatic Fans Using L298n PWM :

In our case, Our Raspberry Pi runs a motioneye server, a SMB Server, some python scripts, and occasionally some media on VLC/Kodi. Our Temperature before average on Room Temperature 40 °C :

Idle : 60°C
Under Load : 80°C (Before Throttling)

Using this automatic fan setup for Raspberry Pi, We managed to get these temperatures :

Idle : 48°C
Under Load : 75°C (No Throttling)

Lower temperature increase the speed of your rPi as well as provide a boost to board life. You can also overclock harder if your cooling setup if nice enough.