User Guide
Last revision 2025/12/22
The following tutorial includes fan control and RGB control, and also provides some examples of displaying common information on the screen, such as IP address, temperature, Raspberry Pi operating status, and more.
Hardware Preparation
- Raspberry Pi 4B/3B/3B+ 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 (SKU: DFR0672) x1, Purchase Link
Software Preparation
- Raspberry Pi OS
- Git (for cloning the software package)
- GCC compiler (pre-installed on Raspberry Pi OS)
- wiringPi library (used for I2C communication)
Other Preparation Work
refer to Getting Started
Wiring Diagram
Plug the Smart Cooling Hat into the Raspberry Pi board's GPIO. Refer to the following image:

How to use software package
1. Control a fan
- Enter a file folder and check the files inside it.
command: cd temp_control
command: ls

- Compile program files
command: 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
command: ./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.
command: cd temp_control/
command: ls

- Compile program files
command: 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
command: ./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.
command: cd temp_control/
command: ls

- Compile program files
command: 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
command: ./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.
command: cd temp_control/
command: ls

- Compile program files
command: 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
command: ./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.
command: cd temp_control/
command: ls

- Compile program files
command: 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
command: ./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.
command: cd temp_control/
command: ls

- Compile program files
command: 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
command: ./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
command: 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:
command: cd /home/pi/.config
- Create autostart file, skip this step if it already exists:
command: mkdir autostart
- Enter autostart file:
command: cd autostart
- Create autostart shortcut
command: 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.
command: 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
command: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.
command: cd ~/temp_control
Add a & at the end of the command to indicate it runs in the background.
command: ./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.
Was this article helpful?
