1.1 Introduction of Raspberry Pi RGB cooling HAT
1. Introduction of Function:
This is a Multi-function cooling expansion board designed for the Raspberry Pi board. It is perfectly compatible with the 4B/3B+/3B to protect Raspberry Pi and extends its life. A 4pin IIC interface for OLED display, which can real-time display CPU temperature, CPU usage, hard disk space, memory and IP address. The large-size cooling fan on the board with strong wind power, it can make Raspberry Pi can run more stably by automatically adjust the speed according to the CPU temperature. 3 high-brightness RGB programming lights on the bottom of the expansion board, which can realize following lights, breathing lights, marquees and so on. It also expand Raspberry Pi 40pin header and can be used to connect to other devices. We will provide a driver package for all Raspberry Pi images, which is convenient for users to drive fans, OLED displays, RGB lights.
2. On-board components location
Front:
Back:
①D8 indicator
Insert Raspberry Pi 4B, which can be used to indicate the Raspberry Pi 4B boot/shutdown status.
This indicator is connected to the Raspberry Pi 3.3V pin. If the Raspberry Pi is powered on, the 3.3V pin voltage with 3.3V high voltage, so D8 keep on; if the Raspberry Pi is turned off or abnormal, the 3.3V pin has no voltage. D8 is off.
! Note: For Raspberry Pi 3B+ board. Even if 3B+ is not boot, as long as the USB is plugged in 3B+ board, the 3.3V pin will output 3.3V. If the Raspberry Pi is abnormal, the 3.3V pin will have no voltage.
②D1 indicator
D1 is running indicator of the MCU.
After the start up normally , the D1 indicator will light the effect of the breathing light. If the D1 indicator is not have breathing effect, it means that the MCU is not running, or the program is incorrect.
③I2C interface (oled display)
You can insert a 128*32 oled display into this interface. Use the Raspberry Pi drive program to the display system information, or other images.
④40pin male pin
Directly connected to the 40pin pin header on the Raspberry Pi, it has the same function as the 40pin pin header on the original Raspberry Pi.
⑤Fan
For cooling, you can send commands to the MCU through the Raspberry Pi to adjust the fan speed.
⑥3 RGB lights
Used to display RGB light effects, you can send commands to the MCU to modify the color and effect through the Raspberry Pi.
⑦Pin header
Insert the pins of the Raspberry Pi directly.
3. Boot up
①Raspberry Pi cooling HAT starts up, the bottom three RGB lights are become green, and the brightness gradually increases. When the brightness reaches the maximum, it stays always bright, which proves that the MCU has completed initialization. The D1 indicator starts to run the breathing light effect, indicating that the MCU is operating normally.
②If you do not run the program to drive the expansion board, the fan and RGB lights of the expansion board do not work by default. We can install the boot-up program directly in the system running install.sh, or write the code control expansion board according to the content of the communication protocol.
4. Installation
1.2 Install related software and settings
1. The Raspberry Pi opens the I2C settings.
PS: The Raspberry Pi RGB_Cooling_HAT and Raspberry Pi control method is operated by I2C, so we need to enable the Raspberry Pi I2C service.
Input command:
sudo raspi-config
Then, select the【Interfacing Options】
Select 【P5 I2C】 and confirm with “YES”.
2. Install wiringPi
PS: Raspberry Pi official raspbian system will bring its own wiringPi by default. You can run gpio –v to view the version. If there is, skip this step.
Input command:
cd ~
git clone git://git.drogon.net/wiringPi
If this command cannot be downloaded, use the following command to download the unofficial wiringPi image:
git clone https://github.com/WiringPi/WiringPi.git
cd WiringPi
sudo ./build
3. Install gcc
PS: Raspberry Pi official raspbian system will bring its own wiringPi by default. You can run gpio –v to view the version. If there is, skip this step.
Input command to install gcc:
sudo apt-get install gcc
4. Oled display drive
Just need to put the three driver files of the oled driver library (ssd1306_i2c.c/ ssd1306_i2c.h/oled_fonts.h) in the same folder as the source code need to be run, and compile with gcc command.
Eg:
gcc -o oled oled.c ssd1306_i2c.c -lwiringPi
./oled
2.1.1 Temperature control switch version
The Raspberry Pi RGB_Cooling_HAT needs to be properly plugged into the GPIO port of the Raspberry Pi and open the Raspberry Pi system I2C function.
This experimental phenomenon shows that the OLED displays the CPU usage, CPU temperature, running memory usage, disk usage and IP address of the Raspberry Pi. The RGB light turns on the special effect. When the CPU temperature of the Raspberry Pi reaches a certain level, the fan will automatically turn on, cooling the Raspberry Pi. When the temperature drops, the fan will automatically turn off.
1. File transfer
1.1 Install WinSCP tool on the computer side, connect the Raspberry Pi and transfer the temp_control.zip package to the pi directory of the Raspberry Pi.
Path of WinSCP:[Raspberry Pi RGB_Cooling_HAT]---[Tools]---[winscp556_setup.1416364912.exe]
1.2 Extract file
Open the Raspberry Pi terminal and input command ls to find the temp_control.zip file.
As shown below:
Input command to extract file:
unzip temp_control.zip
2.Compiling and running program
2.1 Input command to enter temp_control find file:
cd temp_control/
ls
2.2 Input command to compile:
gcc -o temp_control temp_control.c ssd1306_i2c.c -lwiringPi
Among them, the gcc compiler is called, -o means to generate the file, temp_control is the generated file name, temp_control.c is the source program, ssd1306_i2c.c is the library that drives oled, and -lwiringPi is the wiringPi library that references the Raspberry Pi.
2.3 Input command to run the program
./temp_control
At this point, the system will prompt “init ok!”, RGB lights show special effects, and the OLED screen displays information such as CPU usage, CPU temperature, running memory usage, disk usage and IP address of the Raspberry Pi.
①If you use the Raspberry Pi 4B board, when the CPU temperature reaches 55℃, the fan will automatically turn on, to send heat to the Raspberry. When the temperature drops to 48℃, the fan is automatically turn off.
②If you use the Raspberry Pi 3B+ board, when the CPU temperature reaches 46℃, the fan will automatically turn on, and when the CPU temperature drops to 40℃, it will automatically turn off.
Press Ctrl+C to exit this program.
3. Add boot self-starting
3.1 Input command to enter relate folder
cd /home/pi/temp_control
3.2 Input command t view script file
ls
3.3 Run script command to install
sudo sh install.sh
Finally, system will prompt “install ok!”
As shown below.
!Note: If you already have the autostart folder, it will be prompt can not create the autostart folder, which will not affect our use.
4.Restart Raspberry pi
Input command to restart Raspberry Pi:
sudo reboot
2.1.2 Automatic temperature control version
!Note:This method will be a bit noisy, it is recommended to use [2.1.1 Temperature control switch version].
The Raspberry Pi RGB_Cooling_HAT needs to be properly plugged into the GPIO port of the Raspberry Pi and open the Raspberry Pi system I2C function.
This experimental phenomenon shows that the OLED displays the CPU usage, CPU temperature, running memory usage, disk usage and IP address of the Raspberry Pi. The RGB light turns on the special effect. Then speed of fan will be adjusted due to CPU temperature.
1. File transfer
1.1 Install WinSCP tool on the computer side, connect the Raspberry Pi and transfer the temp_control.zip package to the pi directory of the Raspberry Pi.
Path of WinSCP:[Raspberry Pi RGB_Cooling_HAT]---[Tools]---[winscp556_setup.1416364912.exe]
1.2 Extract file
Open the Raspberry Pi terminal and input command ls to find the temp_control.zip file.
As shown below:
Input command to extract file:
unzip temp_control_1.zip
2.Compiling and running program
2.1 Input command to enter temp_control find file:
cd temp_control_1/
ls
2.2 Input command to compile:
gcc -o temp_control_1 temp_control_1.c ssd1306_i2c.c -lwiringPi
Among them, the gcc compiler is called, -o means to generate the file, temp_control_1 is the generated file name, temp_control_1.c is the source program, ssd1306_i2c.c is the library that drives oled, and -lwiringPi is the wiringPi library that references the Raspberry Pi.
2.3 Input command to run the program
./temp_control_1
At this point, the system will prompt “init ok!”, RGB lights show special effects, and the OLED screen displays information such as CPU usage, CPU temperature, running memory usage, disk usage and IP address of the Raspberry Pi. And speed of fan will be adjusted due to CPU temperature.
3. Add boot self-starting
3.1 Input command to enter relate folder
cd /home/pi/temp_control_1
3.2 Input command t view script file
ls
3.3 Run script command to install
sudo sh install_1.sh
Finally, system will prompt “install ok!”
!Note: If you already have the autostart folder, it will be prompt can not create the autostart folder, which will not affect our use.
4.Restart Raspberry pi
Input command to restart Raspberry Pi:
sudo reboot
2.2 Control Fan
The Raspberry Pi RGB_Cooling_HAT needs to be properly plugged into the GPIO port of the Raspberry Pi and open the Raspberry Pi system I2C function.
This experimental phenomenon shows that after 2s, the fan speed is increased every second, next, it will run for 2 seconds with the highest speed, finally, it stops again and keep looping in this state.
1. File transfer
1.1 Install WinSCP tool on the computer side, connect the Raspberry Pi and transfer the temp_control.zip package to the pi directory of the Raspberry Pi.
Path of WinSCP:[Raspberry Pi RGB_Cooling_HAT]---[Tools]---[winscp556_setup.1416364912.exe]
1.2 Extract file
Open the Raspberry Pi terminal and input command ls to find the temp_control.zip file.
As shown below:
Input command to extract file:
unzip temp_control.zip
2. Compiling and running program
2.1 Input command to enter temp_control find file:
cd temp_control/
ls
2.2 Input command to compile:
gcc -o fan fan.c -lwiringPi
Among them, the gcc compiler is called, -o means to generate the file, fan is the generated file name, fan.c is the source program, and -lwiringPi is the wiringPi library that references the Raspberry Pi.
2.3 Input command to run the program
./fan
After 2s, the fan speed is increased every second, next, it will run for 2 seconds with the highest speed, finally, it stops again and keep looping in this state.
3. About code
3.1 Initialize the Raspberry Pi I2C configuration
3.2 Cycle control fan speed, according to the protocol, we can know that grade of fan speed:
0x00: Close fan,
0x01: full speed,
0x02: 20% speed,
0x03: 30% speed,
...,
0x09: 90% speed
3.3 Limit the state size, set to 0 when greater than 9 to achieve loop effect
2.3 Get CPU temperature, Adjust fan speed
The Raspberry Pi RGB_Cooling_HAT needs to be properly plugged into the GPIO port of the Raspberry Pi and open the Raspberry Pi system I2C function.
The experimental phenomenon is to read and print the Raspberry Pi CPU temperature, and adjust the speed of the fan rotation according to the temperature. The higher the temperature, the faster the rotation speed.
1. File transfer
1.1 Install WinSCP tool on the computer side, connect the Raspberry Pi and transfer the temp_control.zip package to the pi directory of the Raspberry Pi.
Path of WinSCP:[Raspberry Pi RGB_Cooling_HAT]---[Tools]---[winscp556_setup.1416364912.exe]
1.2 Extract file
Open the Raspberry Pi terminal and input command ls to find the temp_control.zip file.
As shown below:
Input command to extract file:
unzip temp_control.zip
2. Compiling and running program
2.1 Input command to enter temp_control find file:
cd temp_control/
ls
2.2 Input command to compile:
gcc -o fan_temp fan_temp.c -lwiringPi
Among them, the gcc compiler is called, -o means to generate the file, fan_temp is the generated file name, fan_temp.c is the source program, -lwiringPi is the wiringPi library that references the Raspberry Pi.
2.3 Input command to run the program
./fan_temp
We can see the Raspberry Pi CPU temperature will be printed, and adjust the speed of the fan rotation according to the temperature. The higher the temperature, the faster the rotation speed.
3. About code
3.1 First, import the file control library and the I2C library. The path of the Raspberry Pi to view the CPU temperature is defined as TEMP_PATH.
3.2 Define CPU temperature related parameters and I2C related parameters in the main function
3.3 Cycle through the CPU temperature file and save fd_temp, if the open fails it will return -1 . Then, read the temperature, save it to buf, and return -1 if it fails.
When the temperature reading is successfully saved to the buf, since the value is relatively large, divide by 1000 to get the current temperature, the unit is Celsius, and save to temp. Run the close() function to close the file each time the file is read.
3.4 Get the temperature. Next, judge the temperature value and modify the fan speed. Can be modified according to actual needs.
2.4 Control RGB light
The Raspberry Pi RGB_Cooling_HAT needs to be properly plugged into the GPIO port of the Raspberry Pi and open the Raspberry Pi system I2C function.
This experimental phenomenon shows that all RGB light become green.
1. File transfer
1.1 Install WinSCP tool on the computer side, connect the Raspberry Pi and transfer the temp_control.zip package to the pi directory of the Raspberry Pi.
Path of WinSCP:[Raspberry Pi RGB_Cooling_HAT]---[Tools]---[winscp556_setup.1416364912.exe]
1.2 Extract file
Open the Raspberry Pi terminal and input command ls to find the temp_control.zip file.
As shown below:
Input command to extract file:
unzip temp_control.zip
2. Compiling and running program
2.1 Input command to enter temp_control find file:
cd temp_control/
ls
2.2 Input command to compile:
gcc -o rgb rgb.c -lwiringPi
Among them, the gcc compiler is called, -o means to generate the file, rgb is the generated file name, rgb.c is the source program, -lwiringPi is the wiringPi library that references the Raspberry Pi.
2.3 Input command to run the program
./rgb
We can see that all RGB light become green.
3. About code
3.1 There are three RGB lights on the expansion board, so the number of defined lights is 3, and the setRGB and closeRGB functions are declared.
3.2 void setRGB(int num, int R, int G, int B) function:
Set the RGB light color, num refers to which light, 0 is the first light, 1 is the second light, 2 is the third light. If it is greater than or equal to 3, then, all the lights are set at the same time. The R, G, and B values range from 0 to 255.
3.3 Turn off RGB. According to the protocol, the RGB register is turned off to 0x07 and the data is 0x00.
3.4 Initialize the I2C configuration in the main function.
3.5 Turn off the RGB light first, then set the RGB light. If it is not turned off first, it will affect the display effect. The effect of setRGB can be set by yourself.
2.5 Change RGB color according to CPU temperature
The Raspberry Pi RGB_Cooling_HAT needs to be properly plugged into the GPIO port of the Raspberry Pi and open the Raspberry Pi system I2C function.
The experimental phenomenon is to read and print the Raspberry Pi CPU temperature, adjust the color change of the RGB lamp according to the temperature.
The color distribution is roughly as follows: the low temperature turns blue, the medium temperature turns yellow, and the high temperature turns red.
1. File transfer
1.1 Install WinSCP tool on the computer side, connect the Raspberry Pi and transfer the temp_control.zip package to the pi directory of the Raspberry Pi.
Path of WinSCP:[Raspberry Pi RGB_Cooling_HAT]---[Tools]---[winscp556_setup.1416364912.exe]
1.2 Extract file
Open the Raspberry Pi terminal and input command ls to find the temp_control.zip file.
As shown below:
Input command to extract file:
unzip temp_control.zip
2. Compiling and running program
2.1 Input command to enter temp_control find file:
cd temp_control/
ls
2.2 Input command to compile:
gcc -o rgb_temp rgb_temp.c -lwiringPi
Among them, the gcc compiler is called, -o means to generate the file, rgb_temp is the generated file name, rgb_temp.c is the source program, -lwiringPi is the wiringPi library that references the Raspberry Pi.
2.3 Input command to run the program
./rgb_temp
The terminal will print the current CPU temperature value, and the RGB light color will also change with the color change.
3. About code
3.1 First, import the file control library and the I2C library. The path of the Raspberry Pi to view the CPU temperature is defined as TEMP_PATH.
3.2 Define RGB related parameters and I2C related parameters
3.3 Define CPU temperature related parameters
3.4 Cycle through the CPU temperature file and save fd_temp, if the open fails it will return -1 . Then, read the temperature, save it to buf, and return -1 if it fails.
When the temperature reading is successfully saved to the buf, since the value is relatively large, divide by 1000 to get the current temperature, the unit is Celsius, and save to temp. Run the close() function to close the file each time the file is read.
3.5 Get the temperature, then judge the temperature value and modify the fan speed. It can be modified according to actual needs. The RGB light color can be searched online to view the RGB color comparison table.
3.6 Set RGB and close RGB
2.6 RGB effect
The Raspberry Pi RGB_Cooling_HAT needs to be properly plugged into the GPIO port of the Raspberry Pi and open the Raspberry Pi system I2C function.
This experimental phenomenon shows that all RGB light become purple breathing light.
1. File transfer
1.1 Install WinSCP tool on the computer side, connect the Raspberry Pi and transfer the temp_control.zip package to the pi directory of the Raspberry Pi.
Path of WinSCP:[Raspberry Pi RGB_Cooling_HAT]---[Tools]---[winscp556_setup.1416364912.exe]
1.2 Extract file
Open the Raspberry Pi terminal and input command ls to find the temp_control.zip file.
As shown below:
Input command to extract file:
unzip temp_control.zip
2. Compiling and running program
2.1 Input command to enter temp_control find file:
cd temp_control/
ls
2.2 Input command to compile:
gcc -o rgb_effect rgb_effect.c -lwiringPi
Among them, the gcc compiler is called, -o means to generate the file, rgb_effect is the generated file name, rgb_effect.c is the source program, -lwiringPi is the wiringPi library that references the Raspberry Pi.
2.3 Input command to run the program
./rgb_effect
We can see that all RGB light become purple breathin light.
3. About code
3.1 There are three RGB lights on the expansion board, so the number of defined lights is 3,
Define the register address: RGB_Effect is 0x04, RGB_Speed is 0x05, and RGB_Color is 0x06. Declare the function you need to use.
3.2 void setRGB(int num, int R, int G, int B) function:
Set the RGB light color, num refers to which light, 0 is the first light, 1 is the second light, 2 is the third light. If it is greater than or equal to 3, then, all the lights are set at the same time. The R, G, and B values range from 0 to 255.
3.3 Turn off RGB. According to the protocol, the RGB register is turned off to 0x07 and the data is 0x00.
4. void setRGBEffect(int effect) function:
First,we need to judge the input value. There are five kinds of special effects to choice, 0-water light, 1-breathing light, 2-marquee, 3-rainbow lights, 4-colorful lights.
5. void setRGBSpeed(int speed) function:
Modify the RGB light switching speed of the mode. 1-low speed, 2-medium speed (default), 3-high speed. If you do not set, it is medium speed(default).
6. void setRGBColor(int color) function:
Set the color of the water light and breathing light, 0-red, 1-green (default), 2-blue, 3-yellow, 4-purple, 5-cyan, 6-white.
7.Initialize the I2C configuration
8.An example of setting a purple high-speed breathing light.
2.7 OLED display raspberry pi status
The Raspberry Pi RGB_Cooling_HAT needs to be properly plugged into the GPIO port of the Raspberry Pi and open the Raspberry Pi system I2C function.
This experimental phenomenon shows that OLED display CPU usage, CPU temperature, running memory usage, disk usage and IP address of the Raspberry Pi
1. File transfer
1.1 Install WinSCP tool on the computer side, connect the Raspberry Pi and transfer the temp_control.zip package to the pi directory of the Raspberry Pi.
Path of WinSCP:[Raspberry Pi RGB_Cooling_HAT]---[Tools]---[winscp556_setup.1416364912.exe]
1.2 Extract file
Open the Raspberry Pi terminal and input command ls to find the temp_control.zip file.
As shown below:
Input command to extract file:
unzip temp_control.zip
2. Compiling and running program
2.1 Input command to enter temp_control find file:
cd temp_control/
ls
2.2 Input command to compile:
gcc -o oled oled.c ssd1306_i2c.c -lwiringPi
Among them, the gcc compiler is called, -o means to generate the file, oled is the generated file name, oled.c and ssd1306_i2c.c are the source program, -lwiringPi is the wiringPi library that references the Raspberry Pi.
2.3 Input command to run the program
./oled
We can see that OLED display CPU usage, CPU temperature, running memory usage, disk usage and IP address of the Raspberry Pi.
3. About code
3.1 Import the wiringPi/I2C library, the oled display library, the file control library, the read IP library, and the read disk library.
3.2 Define temperature, system information, disk information, IP and other related parameters.
3.3 Initialize the OLED and output initialization success information “init ok!” from on the terminal.
3.4 Read the system information, if it fails, display sysinfo-Error on OLED, and wait 0.5 seconds to re-read.
3.5 Read the CPU usage, where the function of the “sprintf” function is to concatenate the string.
3.6 Read the running memory usage. The unit of read data is b. For the convenience of display, it needs to be converted into Mb, which can be shifted to the right by 20 bits.
It can also be written as follows: unsigned long totalRam = sys_info .totalram / 1024 / 1024;
3.7 Read the IP address, you can display the IP address of the network cable and WiFi network, and give priority to the IP address of the network cable.
3.8 Get the temperature.
3.9 Get disk space
3.10 Set what to display on OLED
The ssd1306_drawText(int x, int y, char *str) function is to set the content displayed on oled, the first parameter is x,which control the left and right offset. The second parameter is y, which control the upper and lower offset. The third parameter is a string pointer, which is what you want to display.
Finally, you must run the ssd1306_display() function to refresh the display.
2.8 Raspberry Pi boot self-starting setting
There are many ways to start the Raspberry Pi boot. Choose one of them (new .desktop file) as a demonstration.
1. Add boot self-starting
1.1 New startup script start.sh
nano /home/pi/temp_control/start.sh
Input following content:
#!/bin/sh
sleep 5s
cd /home/pi/ temp_control /
./ temp_control
Press ctrl+X, press Y to save, press Enter.
2. New create boot startup program
2.1 Input following command to open.config folder
cd /home/pi/.config
2.2 Input following command to new create autostart folder
mkdir autostart
2.3 Input following command to enter autostart folder
cd autostart
2.4 Input following command to new create shortcut for self-starting
nano start.desktop
Input following content:
[Desktop Entry]
Type=Application
Exec=sh /home/pi/cpu_show_v3/start.sh
Press ctrl+X, press Y to save, press Enter.
Exec=Start command。
!!!Note:
Since this self-starting method needs to be started after the desktop is started, the startup will be slower. If it is found that it cannot be self-started after adding, please check if there is a ## in front of hdmi_force_hotplug=1 in the /boot/config.txt file. If there is a # number, please delete the # number. The picture will prevail.
3. Restart Raspberry Pi
After restarting, the temp_control program will start automatically, and the fan, RGB light and oled screen will have corresponding responses.
Input following command to restart Raspberry Pi:
sudo reboot
4. Exit the program
Since the self-starting program is running in the background, we cannot directly exit the program in the open terminal. If you need to modify the program, but the background process interferes with our debugging results, so you need to see the process number (PID) of the daemon, and then end this process.
4.1 Input top in the terminal to open the process list
Sometimes it may take a while for the system to queue up processes that take up a lot of CPU resources. From the picture we can see the boot temp_control program, its PID is 718, so we kill this process number, temp_control program will not run in the background.
Press Ctrl+C to exit top.
4.2 Input following command to end the process
sudo kill -9 PID
For example: In the above case, we can run the sudo kill -9 718 command to end the temp_control process running in the background. If you run it again, it will be prompted that the process does not exist.
4.3 Restart the background operation
If we have finished the process running in the background, but we want to restart the background process.
First method: restart the Raspberry Pi,
Second method: Add an “&” to the running program.
For example, we still run the temp_control program in the background:
We need to input following command to enter the target folder (based on the location of the file saved by the individual)
cd ~/temp_control
./temp_control &
At this point, the system will prompt the PID of this process (1015).
Press Ctrl+C again. You can see that the terminal can enter other commands, and the program is running in the background.