Introduction
This is a multi-function cooling expansion board specifically designed for Raspberry Pi. It provides full compatibility with Pi 4B/3B+/3B to protect Raspberry Pi board and extends its lifespan.
There is a large-size cooling fan on the board with strong wind power that can make Raspberry Pi run more stably by automatically adjusting its speed according to the CPU temperature. Three high-brightness RGB programming LEDs on the bottom of the expansion board can be used to display various lighting effects: waterfall lighting, breathing lights, rainbow lamp and more. This board can be directly plugged onto Raspberry Pi board via its 40 pin interfaces, and it has expanded 40 pin header for connecting to other devices without affecting the use of GPIO ports on Pi board. Furthermore, the place for OLED screen is reserved on the board to display CPU temperature, CPU usage, hard disk space, memory and IP address in real-time.
This fan can effectively reduce the board temperature by about 15℃ in actual tests.
Note: Raspberry Pi board is not included. |
Specification
- Support OLED display, can display the running status of Raspberry Pi in real-time.
- The fan on the board can automatically adjust its speed according to the CPU temperature.
- Perfectly compatible with Raspberry Pi 4B/3B+/3B, not occupy any Raspberry Pi pin or heat sink space.
- Interface: directly plug into Raspberry Pi board IO ports
- Dimension: 655525mm/2.562.170.98”
- Weight: 34.1g
Tutorial
Requirements
- Hardware
- Raspberry Pi x1
- Network Cable x1
- Micro SD Card x1
- Raspberry Pi Adapter x1
- HDMI Display x1
- HDMI Connector x1
- Smart Cooling Hat For Raspberry Pi 4B x1
Plug the fan hat into the Raspberry Pi board's GPIO and install the screws before we are ready to install and configure Raspberry Pi system.
Raspberry Pi System Installation and Configuration
For detailed information about how to install Raspberry Pi system, please refer to the official document: Install raspberrypi guide
Power on to start Raspberry Pi system. The Camera and I2C are all in disabled state for the initial Pi system so we have to enable them mannually. Run the command:
sudo raspi-config |
Select option 5, enable I2C and then restart.
Software Download
There are two ways to download and transfer software packages to Raspberry Pi
- Run the command:
git clone https://github.com/OldWang-666/temp_control.git |
Copy the files into Raspberry Pi system directly.
- Click the link to download software package
The software package can be tansferred into Raspberry Pi system via samba or copied into the system using U-disk.
Unzip the software package
Open Raspberry Pi terminal, find the temp_control.zip file we tranferred into just now.
Input the following command to unzip the file:
unzip temp_control.zip |
How to use software package
1. Control a fan
- Enter a file folder and check the files inside it.
cd temp_control/ ls |
- Compile program files
gcc -o fan fan.c –lwiringPi |
Invoke gcc compiler. -o means to generate files, and will be followed by the name of generated file. fan.c is source program, and -lwiringPi means to use the wiringPi library in Raspberry Pi.
- Run program
./fan |
The fan will start to rotate in 2s, and its speed will increase every second. When reaching to the highest speed, it will run for two seconds and then stop, and so on.
- Codes Analysis
- Init Raspberry Pi I2C configuration.
- Control fan speed in loop. The fan speed level can be known from the protocol: 0x00: close; 0x01: full speed; 0x02: 20%speed; 0x03: 30%speed;....0x09: 90%speed.
- Limit "state" within 9. When it is more than 9, set it to 0 to realize the loop.
2. Read CPU temperature, and adjust fan speed.
- Enter a file folder and check the files inside it.
cd temp_control/ ls |
- Compile program files
gcc -o fan_temp fan_temp.c -lwiringPi |
Invoke gcc compiler. -o means to generate files, and will be followed by the name of generated file. fan.c is source program, and -lwiringPi means to use the wiringPi library in Raspberry Pi.
- Run the program
./fan_temp |
The current CPU tempterature will be printed on the Raspberry Pi terminal, and the fan speed increases as the temperature goes up.
- Codes Analysis
- First, import file control library and I2C library. The path to check Raspberry Pi's CPU temperature is defined as TEMP_PATH.
- Define parameters related to CPU temperature and I2C.
- Open CPU temperature file circularly and save fd_Temp (In Linux, everything is a file). If it fails to open, return - 1. Next, read the temperature and save it in buf. also returns - 1 when failed. After the temperature reading is saved to buf successfully, it need to be divided by 1000 because the value is relatively large, then the current temperature can be obtained in centigrade, and save it to temp. Run the close() function to close the file manually after reading the file.
- Get temperature, judge CPU's temperature value, and revise fan speed(can be changed according to actual needs).
3. Turn on RGB light
- Enter a file folder and check the files inside it.
cd temp_control/ ls |
- Compile program files
gcc -o rgb rgb.c -lwiringPi |
Invoke gcc compiler. -o means to generate files, and will be followed by the name of generated file. rgb.c is source program, and -lwiringPi means to use the wiringPi library in Raspberry Pi.
- Run the program
./rgb |
Three RGB LEDs light up in green at the same time.
- Codes Analysis
- There are three RGB LEDs on the board, so we define the number of light as 3, declare setRGB and closeRGB function.
- void setRGB(int num, int R, int G, int B) function:
Set RGB LED color. num refers to the LED number: o is the first LED, 1 for the second LED, 2 for the third LED. When num is over 3, it means to set all the LEDs. The range of R, G, B is 0~255.
- Turn off RGB LED. According to the protocol, the register to turn off RGB is 0x07, data 0x00.
- Init I2C configuration in main function.
- Turn off the RGB light before setting the RGB light, otherwise, the display effect may be influenced. The effect of setRGB can be set by yourself. Here, for example, all the LEDs are set to be ON in green.
4. Program RGB lights to change color with CPU temperature
- Enter a file folder and check the files inside it.
cd temp_control/ ls |
- Compile program files
gcc -o rgb_temp rgb_temp.c -lwiringPi |
Invoke gcc compiler. -o means to generate files, and will be followed by the name of generated file. rgb_temp.c is source program, and -lwiringPi means to use the wiringPi library in Raspberry Pi.
- Run the progam
./rgb_temp |
The terminal prints out the current CPU temperature, and the RGB LEDs changes its color with the CPU temperature.
- Codes Analysis
- Import file control library and I2C library, the path to check CPU temperature of Raspberry Pi is defined as TEMP_PATH.
- Define RGB LED and I2C parameters
- Define temperature related parameters
- Open CPU temperature file circularly and save fd_Temp (In Linux, everything is a file). If it fails to open, return - 1. Next, read the temperature and save it in buf. also returns - 1 when failed. After the temperature reading is saved to buf successfully, it need to be divided by 1000 because the value is relatively large, then the current temperature can be obtained in centigrade, and save it to temp. Run the close() function to close the file manually after reading the file.
- After the current temperature is obtained, determine the temperature value and then revise the fan speed (can be changed according to actual use). Search the corresponding color table online for the RGB LEDs.
- Set the state of RGB LEDs.
5. RGB LED Display Effects
- Enter a file folder and check the files inside it.
cd temp_control/ ls |
- Compile program files
gcc -o rgb_effect rgb_effect.c -lwiringPi |
Invoke gcc compiler. -o means to generate files, and will be followed by the name of generated file. rgb_effect.c is source program, and -lwiringPi means to use the wiringPi library in Raspberry Pi.
- Run the programs
./rgb_effect |
Three RGB LEDs display breathing effect in purple at the same time.
- Code Analysis
- There are three RGB LEDs on the board, so define the number of LEDs as 3. Define the register address: RGB_Effect is 0x04, RGB_Speed is 0x05, RGB_Color is 0x06. Declare the necessary functions.
- void setRGB(int num, int R, int G, int B) function:
Set RGB LED color. num refers to the LED number: o is the first LED, 1 for the second LED, 2 for the third one. When num is over 3, it means to set all the LEDs. The range of R, G, B is 0~255.
- Turn off RGB LED. According to the protocol, the register to turn off RGB is 0x07, data 0x00.
- void setRGBEffect(int effect) function:
Determine the input value. Corresponding to the protocol, there are 5 display effects to choose: 0->waterfall, 1->breathing, 2->chasing, 3->rainbow, 4->colorful
- void setRGBSpeed(int speed) function:
Revise the switching speed of the LEDs display effects. 1->slow, 2->Middle(default), 3->fast.
- void setRGBColor(int color) function:
Set the color of the LEDs effect in waterfall and breathing mode. 0 for red, 1 for green(default), 2 for blue, 3 for yellow, 4 for purple, 5 for cyan, 6 for white.
- Init I2C configuration
- Display fast breathing effect in purple.
6. Present Raspberry Pi state on OLED
- Enter a file folder and check the files inside it.
cd temp_control/ ls |
- Compile program files
gcc -o oled oled.c ssd1306_i2c.c -lwiringPi |
Invoke gcc compiler. -o means to generate files, and will be followed by the name of generated file. oled.c and ssd1306_i2c.c are the source programs, and -lwiringPi means to use the wiringPi library in Raspberry Pi.
- Run the program
./oled |
Now, you can check the CPU utilization, CPU temperature, operatiing memory utilization, disk utilization, IP address and other information of your Pi on the OLED screen.
- Codes Analysis
- Import wiringPi/I2C library, oled display library, file control library, IP reading library, and disk reading library.
- Define parameters about temperature, system information, disk information, and IP address.
- Init oled screen, and output init success information from the terminal.
- Read system information. Display sysinfo-Error on the OLED if it fails, and re-read 0.5s later.
- Read CPU utilization. There is a sprintf function inside for concatenating strings.
- Read RAM usage (unit: b). For easy display, it needs to be converted to Mb. Shift the value 20 bits to the right, or written as follows: unsigned long totalRam = sys_info.totalram / 1024 / 1024;
- Read IP address of cable network and WiFi, prior to display WiFi IP address.
- Read temperature.
- Read disk space.
- Display information on OLED.
The function ssd1306_drawText(int x, int y, char *str) is used to set the content displayed on OLED. The first parameter is x, which controls the left and right offset. The second one is y, which controls the up and down offset. The third is string pointer, also the content to be displayed.
Run the function ssd1306_display() to refresh and display.
7. Raspberry Pi Auto-start
- Create script start.sh
nano /home/pi/temp_control/start.sh |
Input the following content:
Press ctrl+X+Y to save, and tap button "Enter".
- Create auto-start program
Input the following command to open .config file:
cd /home/pi/.config |
Create autostart file, skip this step if it already exists:
mkdir autostart |
Enter autostart file:
cd autostart |
Create autostart shortcut
nano start.desktop |
Then, input the following content.
[Desktop Entry]
Type=Application
Exec=sh /home/pi/cpu_show_v3/start.sh
Press ctrl+X+Y to save, and tap button "Enter".
Exec=startup command among them.
This autostart method can only be started after the desktop is started, so it would take some time. If it cannot be started automatically after adding library, pleas check whether there is a "#" in front of hdmi_force_hotplug=1 in /boot/config.txt file, if so, delete it, shown as below:
- Restart Raspberry Pi
Input the following command into the terminal to restart Raspberry Pi. The temp_control program will be auto started after that. Then, the fan, RGB LEDs, and OLED will response accordingly.
sudo reboot |
- Exit the program
Since the autostart program is running in the background, we cannot directly exit the program at the opened terminal. If we need to revise the program, but worry that the background process would interfere with the debug results, what should we do? Well, it is easy, just check the process number(PID) of the background program, and then end the process.
- Input top into the terminal to open process table.
The system will list out the processes that take up more CPU resources, and sometimes it may be necessary to wait for a while. From the figure above, we can see that the PID of the autostart program temp_control is718. Now, just kill it, and press Ctrl+C to exit top.
- End the process
sudo kill -9 PID |
For example, run the command sudo kill -9 718 to end the temp_control process, then it will prompt the process does not exist when re-running.
- Restart and Run in background
If we have ended a process running in the background, but don't want to restart the background program, we can restart the Raspberry Pi first of course. However, for saving our time, we can directly add a symbol "&" behind the running program.
For instance, we need to run the temp_control program in the background:
First of all, enter the target file holder.
cd ~/temp_control |
Add a & at the end of the command to indicate it runs in the background.
./temp_control & |
Then the system will prompt the PID(1015) of the process.
Press Ctrl+C again. Now we can input other commands in the terminal with the program running in the background.
FAQ
For any questions, advice or cool ideas to share, please visit the DFRobot Forum