Keeping Your Raspberry Pi Cool: A Temperature Manager Tool

Keeping Your Raspberry Pi Cool: A Temperature Manager Tool

I’ve been using a Raspberry Pi 4 as a home lab server for quite some time. Initially, I used an Argon NEO Case, which provides passive cooling, but it wasn’t enough. So, I 3D printed a custom case with a fan to cool it (watch this video). But, as always, it still wasn’t enough! So, I got to work designing temperature management and monitoring for the RPi, and here is what I’ve done.

What is the idea?

Let’s talk about what I wanted to create. My idea was to stick with the NEO case but modify it to support a small OLED to display vital information like network status, temperatures, etc., about the RPi, alongside a fan with speed control to adjust airflow based on system temperatures.

In the following sections, you will learn about the hardware and software I created for it.

Hardware

The first step was figuring out the hardware. I decided to go with a 128×64px OLED with an SSD1306 driver and a 5V fan with a DRV8833 motor controller.

  • 128×64px OLED SSD1306 - I2C protocol

  • Miniature 5V fan

  • DRV8833 Dual Motor Driver

  • Raspberry Pi 4

After testing the right configuration with the software (we’ll discuss that later), I created the schematic to connect everything to the RPi 4. This is how I connected everything to the Raspberry Pi GPIO:

Diagram of a Raspberry Pi 4 connected to a motor controller, OLED display, and DC fan. Various colored wires represent different connections between the components.

DRV8833 Driver ←→ Rasberry Pi

DRV8833Raspberry Pi 4 GPIO
AIN1GPIO13
AIN2GPIO12
VM5V (Pin #4)
GNDGND (Pin #6)
STBYGPIO27

5V Fan ←→ DRV8833 Driver

5V FanDRV8833 Driver
5V (RED)AIO1
GND (BLACK)AIO2

OLED (SSD1306) ←→ Raspberry Pi

OLED - SSD1306Raspberry Pi 4 GPIO
VCC3v3 (Pin #1)
SDAGPIO2
SCLGPIO3
GNDGND (Pin #9)

As you can see in the diagram and tables above, this is how to connect the hardware. After this configuration, it’s time to move on to the case and decide where to place the hardware.

3D Case

As mentioned earlier, I’m using an Argon NEO Case, which has an aluminum shell with two parts, one of which can be removed (as shown in the image below). I planned to modify the top shield to contain the hardware.

I started by measuring the case and designing a 3D file for it to be printed. The plan was to assemble the OLED and fan, where the data would be displayed on the OLED, and the fan would direct airflow from the top and push it out through the rear of the shield via designed grids.

After sending the design to the 3D printer, this was the final version of the case (on the left). It was time to assemble everything and try installing it on the Raspberry Pi.

You can access the 3D design files through this link: 3D Designed Files

Assembling

Now that everything was ready for final assembly, I first soldered some wires with connector headers to make connecting to the RPi easier. Then, I fixed the components to the 3D-printed part and glued two magnets to the new case to keep it closed. You can use some electrical tape on the circuits to prevent short-circuiting and damage to the components.

Software

For the software side, I used Python to code a script that would be added to the boot services and run automatically on system boot. This way, if your system shuts down or restarts, the system states will be preserved—especially useful if you’re running your Raspberry Pi headless.

Getting Started

First, clone the files from the RasPi Temp Manager GitHub repository:

git clone https://github.com/mosnfar/raspi-temp-manager.git

To run this script, you need the following libraries: gpiozero, Pillow (PIL), and adafruit_ssd1306, which are not built-in on the Raspberry Pi. To install them, navigate to the repository directory (cd raspi-temp-manager) and run:

pip install -r requirements.txt

Note: If you’re using a newer version of Python, please install the libraries globally (not within a virtual environment).

Next, copy the essential files (such as fonts and boot logos) into the desired directory. First, create a directory named “temp_manager” at /usr/local/share/ using:

mkdir -p /usr/local/share/temp_manager

Then, use the following commands to copy files from the repo directory to that location:

cp ./fonts/lucan.ttf /usr/local/share/temp_manager
cp ./images/raspberrypi_logo_inverted.bmp /usr/local/share/temp_manager

Test Configurations

Before moving on, there’s a chance the connections might not be correct or all the libraries might not be installed. I wrote a test tool to help debug these issues and ensure everything is in place. Before proceeding, you can run test_config.py, which will check:

  • Required libraries installation

  • I2C connection

  • Fan connection

  • Essential files

To run the test, use the following command:

python test_config.py

Boot Setup

If there are no errors, let’s configure the script to run on boot. First, copy the script file into /usr/local/bin/:

sudo cp ./source/temp_manager.py /usr/local/bin/

Next, create a service for the script by running:

sudo nano /etc/systemd/system/temp_manager.service

Then, add the following content to the service file and save it:

[Unit]
Description=Temperature Manager
After=network.target

[Service]
ExecStart=/usr/bin/python3 /usr/local/bin/temp_manager.py
Restart=always

[Install]
WantedBy=multi-user.target

After saving the file, enable and start the service with these commands:

sudo systemctl enable temp_manager.service
sudo systemctl start temp_manager.service

Hooray!

Everything is now configured correctly. You can test it by running a stress test on the CPU with this command: stress -c 4 -t 900s.

Here’s the final view of the project:


Let’s Connect ☺️

I hope this helps! I’m sharing my experience and findings on my blog, and you can follow me through 📰 newsletters. You can also subscribe to my ▶️ YouTube channel to find more helpful content.

Don’t forget to leave your comments and feedback 🤝


Resources — Here are some of the articles and content that I learned from them: