today we would like to present you the first contribution of one of our new authors - Wilfried Klaas.
Wilfried Klaas will accompany us with interesting projects from now on!
Have fun reading!
With my Raspberry Pi NAS solution with OpenMediaVault I came across the problem that the fan built into the case is running day and night. The fan is a small 30mm 5V DC fan, but it is quite loud and it whistles quite high frequency. So a temperature control is needed. The temperature of the CPU in the Raspberry Pi can be read out directly. A separate temperature sensor is therefore superfluous. One would have to...
You would have to switch the fan directly via a GPIO pin with the help of a small piece of software. The program reads the CPU temperature and switches the fan on and off. The operating system should then periodically start the program.
Hardware list (next to the Raspberry Pi and the housing with fan):
|1||Transistor type BC547 B|
|1||Resistance 4K7 Ohm|
|1||Small piece of circuit board|
|A couple of Breadboard cables|
The GPIO pins of the Raspberry Pi have 3V3 logic and cannot switch the power of even such a small fan directly. Therefore a switching amplifier is needed. Fortunately, this is quite easy to realize with a NPN transistor of type BC547 B.
For testing you can also build the whole thing on the breadboard first. Red goes to +5V (pin 4) on the GPIO. Black (GND or ground) goes to pin 6 and orange, which is the control line, goes to pin 8 (GPIO14). On the fan side, the red wire from the fan goes directly to the +5V and the black goes to pin 1 from the transistor.
To test if the setup works, connect the orange wire directly to +5V instead of pin 8 (GPIO 14). Now the fan should start. If you connect the same wire to ground, the fan should stop for sure. What happens if the line stays open is not defined. The small circuit can then be quickly built on breadboard and plugged into the Raspberry Pi.
So much for the hardware.
Since Python is already installed on the current Raspbian operating system image for the Raspberry Pi and also on the OMV image, it makes sense to write the program in Python. There are plenty of resources and instructions on the internet. The program originates from here: alexbloggt/lueftersteuerung/. I just updated to Python3, added some comments and adjusted the times to my needs.
#!/usr/bin/python import os import time import RPi.GPIO as GPIO GPIO.setwarnings(False) GPIO.setmode(GPIO.BCM) GPIO.setup(14, GPIO.OUT) #funktion: Temperatur mit Hilfe von vcgencmd auslesen und als Text zurückliefern def getCPUtemperature(): res = os.popen('vcgencmd measure_temp').readline() return(res.replace("temp=","").replace("'C\n","")) # Temperatur lesen und in einen Float wandeln temp_float = float(getCPUtemperature()) try: # temperatur > 47, dann Lüfter an if (temp_float > 47): print(temp_float) print("power on fan...") # ein GPIO.output(14, True) # und jetzt 58 Sekunden laufen lassen. (Das passt dann gut mit dem Minuten Timer) time.sleep(58) print("power off fan...") # aus GPIO.output(14, False) print(float(getCPUtemperature())) else: print(temp_float) print("temp low") # Wird das Programm abgebrochen, dann den Lüfter wieder ausschalten except KeyboardInterrupt: print(float(getCPUtemperature())) print("power off fan...") GPIO.output(14, False) print("cancelling...")
In the current version of Raspbian the module RPi.GPIO is part of the image. If you get an error message, e.g. with the Lite version, you can install RPi.GPIO in the terminal as follows:
sudo apt-get update
sudo apt-get install python3-rpi.gpio
As mentioned in the beginning, my Raspberry Pi runs a NAS solution with OpenMediaVault (OMV). Therefore I describe here how to run the fan controller in this environment.
To be able to connect to the Raspberry Pi with a terminal, you first have to enable ssh (secure shell). This can be done via the OMV interface under Services/SSH.
Then the user must
pi (if not already done) is to be added to the group
Now you can log in to the Raspberry via SSH. I use Putty for this under Windows If the login does not work, now would be a good time to reboot the Raspberry. From here on the steps will also work on a normal Raspbian image.
To log in to the Putty, use the user pi with the password you have set in the OMV interface. (Of course you can also use another user, but then you have to set the appropriate permissions in the different places for the whole thing to work).
Now first try out whether the prerequisites for the program work. So first try to read out the temperature. To do this, please enter the following:
pi@omv:~ $ vcgencmd measure_temp temp=45.1'C pi@omv:~ $
It looks good. Now the python connection. First you start Python.
And now they try to start the fan with:
import RPi.GPIO as GPIO GPIO.setmode(GPIO.BCM) GPIO.setup(14, GPIO.OUT) GPIO.output(14, True)
The fan should run now.
GPIO.output(14, False) Turn it off again.
You leave the Python interpreter in the terminal with exit()or CTRL-D. If everything worked, you can install the program and let it start automatically.
First you create a new directory. To do this, go back to the /home directory, create a fan directory there, and then change into it.
cd /home mkdir fan cd fan
The program is then created there. With
sudo nano fancontrol.py
The editor starts. Now copy the program from above, save (CTRL + S) and leave the editor (CTRL + X). With
sudo chmod +x fancontrol.py
the program is made executable.
sudo python fancontrol.py The program starts. Optionally, must also be directly
So far so well, now the program has to be started automatically and once per minute. To do this, the crontab must be edited:
sudo crontab -e
Please write the following lines in the file
# starting the fan every minute */1 * * * * /home/fan/fancontrol.py
Do not forget to save and then reboot.
And you are ready to go. Now every minute the Python script is started. This looks for the temperature and if it is too high, the fan is started for 58 seconds.
The Raspberry now does its job almost without any noise. Only rarely the fan starts, mostly only when a backup of my server is running. Goal achieved. With a few adjustments, this Python program in the background is also suitable for the Raspberry Pi 4, which gets warmer due to its higher clock frequency and is therefore often equipped with a case fan.
Have fun with the rebuild.
I hope you enjoyed this little blog.
See you next time!