Then we make it executable: chmod +x /etc/init.d/fan Kill $(ps aux | grep -m 1 'python /home/pi/Scripts/fan.py' | awk '') # Description: Fan controller auto start after boot
You can create a bash alias for this execution command, but here I'm going to show you how to add this script as a service to your Raspberry Pi and make it running after every time we reboot.Ĭreate a service file under /etc/init.d called fan, so the file path looks like /etc/init.d/fan, edit the file and paste the following: #!/bin/bash The only problem is we have to execute the command to run it every time we reboot the system. Now the script is working, it will monitor the temperature of the CPU, then turn on/off the fan base on the threshold degrees we set. We can test it by execute python /home/pi/Scripts/fan.py, you can see the log in the terminal. If temp < 48.0: # lower bound to turn off the fan If temp > 55.0: # upper bound to turn on the fan With open("/sys/class/thermal/thermal_zone0/temp", 'r') as f: # define the GPIO to control the transistor's B pin GPIO reference: Notes for Raspberry Pi #!/usr/bin/python If you didn't wire the jumper wires as I did, modify the GPIO accordingly. We simply use the code for blink a LED from gpiozero package to control the GPIO, documentation. We use Python to control the GPIO, and set the script to run after the Raspberry Pi system boot.įirst, create a Python file in /home/pi/Scripts/, name as fan.py. Here is a good explanation of a transistor's pins: NPN 2N222 Transistor.with schematic tutorial Raspberry Pi Fan Control Wire Actual wire Code Make sure the E is connected to the positive end (anode), B to the GPIO through a 1kΩ resistor, C to the GND of the Raspberry Pi. Notice the 3 pins for the transistor from left to right are E, B, C. few jumper wires (mainly male to male, male to female).a breadboard for prototyping (not necessary, but recommended).
You might need the following things other than a Raspberry Pi: In this tutorial, we are going to build a auto start/stop 5V Fan for the Raspberry Pi 4 based on the temperature of CPU. The simplest solution would be adding a fan to cool it down, but it makes no sense to let the fan running all the time. However, the temperature could go pretty high when it is implementing intensive tasks.
This truly makes the Raspberry Pi a workable personal portable computer. The latest Raspberry Pi 4 comes with an option of 4G ram.