Setting up a cooler for your Orange Pi/Raspberry Pi using Python.

Guilherme Müller
Level Up Coding
Published in
4 min readMar 18, 2019

--

Python can be used for almost anything, including help to control the temperature of your favorite single-board computer. As great as SBCs are today, most of them get pretty hot if you let them run for a while, shortening their lifespan and throttling their performance.

So, let me put a simple 5v or 12v fan on my SBC and all problems are solved, right? Not so fast. A twenty-four hour, seven days a week running fan will mostly drive you crazy with the noise, will last for a short period before needs replacement, and utilize unnecessary energy.

Now we’re getting somewhere. Let’s use python to set up a simple script that checks if the temperature is higher than a threshold and turn on the fan. Since most of the fans run on 5V or more (and most GPIO pins are 3.3V), I will be using a PNP transistor, so when the GPIO output is low, the transistor operates (this allows more flexibility with the transistor model).

You will need:

- 1 Orange Pi or Raspberry Pi
- 1 PNP transistor (BC 327 in the example for 5v fan)
- Wires
- Heat shrinking tubing
- A fan (5v or 12v)

DISCLAIMER

This tutorial is meant to help you to build a fan controller for your Orange Pi/Raspberry Pi. The following steps require some knowledge of electronics, electrical systems, and IT. If you follow those steps, you are at your own risk.

Connecting the GPIO

If you’re using a 5V fan, locate the GND, 5V pins, and one GPIO pin that will be used to control the transistor and plug the wiring (use can use headers or solder the wires). If you’re using a 12v fan, you will need an external power supply, and instead of using the board GND and 5V pins, use the 12v power supply wires. DO NOT CONNECT ANY OF THE 12V WIRING TO THE BOARD IN ANY CASE.

Orange Pi Zero GPIO

The PNP transistor wiring

Now, solder the wires that are coming from the GPIO following the schematic logic. The negative rail is the GND, the positive rail is the 5V and the GPIO pin is the GPIO output. The logic is simple, the transistor acts as a switch within the negative rail. When the GPIO pin is off, the current flows and allows the fan to spin. To finish, you can use heat shrink tubing to insulate the connections.

Simple schematic of the wiring using a PNP transistor
First prototype

The Python Script

Now, the wiring is complete and we can get to the software part of the deal. Make sure to install Python on your system, create a .py file, and paste the following code:

# Do the imports
import os
import sys
# OPi Library and instructions: https://pypi.org/project/OrangePi.GPIO/
import OPi.GPIO as GPIO
from time import sleep
# Orange Pi Setup
GPIO.setboard(GPIO.ZERO)
GPIO.setmode(GPIO.BOARD)
# Targeted temperature to start the fan (40 degrees celsius)
controlTemp = 40000
# I’m using GPIO and a PNP transistor to control the fan, by default it will be enabled. 1 sets the GPIO to HIGH and 0 to LOW.
outputValue = 0
# Output GPIO pin for the fan transistor. Check the pinout at: https://i0.wp.com/oshlab.com/wp-content/uploads/2016/11/Orange-Pi-Zero-Pinout-banner2.jpg?fit=1200%2C628&ssl=1
outputPin = 24
# Reads the armbian temperature file and checks if it's higher than the threshold, if positive, turns the fan on, otherwise, off.
with open(‘/etc/armbianmonitor/datasources/soctemp’, ‘r’) as fin:
temp=0
temp = int(fin.read())
if(temp >= controlTemp):
outputValue = 0
else:
outputValue = 1
#print temp
#print controlTemp
#print outputValue
GPIO.setup(outputPin, GPIO.OUT)
GPIO.output(outputPin, outputValue)
GPIO.cleanup()

Raspberry alternative

You can use this script with your Raspberry Pi. Change the OPi with the RPI library and swap the temperature reading of ‘/etc/armbianmonitor/datasources/soctemp’ with ‘/sys/class/thermal/thermal_zone0/temp’.

Setting up Cron

Now, let’s set up the script to run every five minutes, so it can check the temperature and turn on the fan if necessary. To do this, we’ll be using Crontab. In the terminal, type:

sudo crontab -e

And you’ll be asked to edit the cron file (use nano or your favorite editor). Enter the edit mode and go to the last line of the file and insert the following line:

/5 * * * * /usr/bin/python /path/to/yourscript.py

/5 means that every five minutes this script will be called by the system.
/usr/bin/python is the path to the python engine. (If your PATH is updated, you can also use only python).
/path/to/yourscript.py is the path to your script.

After that, save the file by pressing CTRL + X and “yes”.
Restart your system and you’re ready to go.
Your fan controller should be working.

The Result

Orange Pi with fan and controller transistor used as file server with external HDD.

Now your SBC can run with fan that only runs when needed.

--

--

A Brazilian software developer and devops engineer, who also likes communication and research.