Smart Cooling Hat For Raspberry Pi 4B/3B+/3B Wiki - DFRobot

DFR0672 Smart Cooling Hat For Raspberry Pi 4B

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

Tutorial

Requirements

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.

DFR0672-8

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

DFR0672-5

Select option 5, enable I2C and then restart.

Software Download

There are two ways to download and transfer software packages to Raspberry Pi

git clone https://github.com/OldWang-666/temp_control.git

Copy the files into Raspberry Pi system directly.

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.

DFR0672-6

Input the following command to unzip the file:

unzip temp_control.zip

DFR0672-7

How to use software package

1. Control a fan

cd temp_control/ ls

DFR0672-9

gcc -o fan fan.c –lwiringPi

DFR0672-10

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.

./fan

DFR0672-11

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.

  1. Init Raspberry Pi I2C configuration.

DFR0672-12

  1. 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.

DFR0672-13

  1. Limit "state" within 9. When it is more than 9, set it to 0 to realize the loop.

DFR0672-14

2. Read CPU temperature, and adjust fan speed.

cd temp_control/ ls

DFR0672-15

gcc -o fan_temp fan_temp.c -lwiringPi

DFR0672-16

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.

./fan_temp

DFR0672-17

The current CPU tempterature will be printed on the Raspberry Pi terminal, and the fan speed increases as the temperature goes up.

  1. First, import file control library and I2C library. The path to check Raspberry Pi's CPU temperature is defined as TEMP_PATH.

DFR0672-18

  1. Define parameters related to CPU temperature and I2C.

DFR0672-19

  1. 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.

DFR0672-20

  1. Get temperature, judge CPU's temperature value, and revise fan speed(can be changed according to actual needs).

DFR0672-21

3. Turn on RGB light

cd temp_control/ ls

DFR0672-22

gcc -o rgb rgb.c -lwiringPi

DFR0672-23

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.

./rgb

DFR0672-24

Three RGB LEDs light up in green at the same time.

  1. There are three RGB LEDs on the board, so we define the number of light as 3, declare setRGB and closeRGB function.

DFR0672-25

  1. 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.

DFR0672-26

  1. Turn off RGB LED. According to the protocol, the register to turn off RGB is 0x07, data 0x00.

DFR0672-27

  1. Init I2C configuration in main function.

DFR0672-28

  1. 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.

DFR0672-29

4. Program RGB lights to change color with CPU temperature

cd temp_control/ ls

DFR0672-30

gcc -o rgb_temp rgb_temp.c -lwiringPi

DFR0672-31

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.

./rgb_temp

DFR0672-32

The terminal prints out the current CPU temperature, and the RGB LEDs changes its color with the CPU temperature.

  1. Import file control library and I2C library, the path to check CPU temperature of Raspberry Pi is defined as TEMP_PATH.

DFR0672-33

  1. Define RGB LED and I2C parameters

DFR0672-34

  1. Define temperature related parameters

DFR0672-35

  1. 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.

DFR0672-20

  1. 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.

DFR0672-36

  1. Set the state of RGB LEDs.

DFR0672-26

5. RGB LED Display Effects

cd temp_control/ ls

DFR0672-30

gcc -o rgb_effect rgb_effect.c -lwiringPi

DFR0672-37

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.

./rgb_effect

Three RGB LEDs display breathing effect in purple at the same time.

  1. 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.

DFR0672-38

  1. 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.

DFR0672-26

  1. Turn off RGB LED. According to the protocol, the register to turn off RGB is 0x07, data 0x00.

DFR0672-27

  1. 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

DFR0672-39

  1. void setRGBSpeed(int speed) function:

Revise the switching speed of the LEDs display effects. 1->slow, 2->Middle(default), 3->fast.

DFR0672-40

  1. 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.

DFR0672-41

  1. Init I2C configuration

DFR0672-42

  1. Display fast breathing effect in purple.

DFR0672-43

6. Present Raspberry Pi state on OLED

cd temp_control/ ls

DFR0672-30

gcc -o oled oled.c ssd1306_i2c.c -lwiringPi

DFR0672-44

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.

./oled

DFR0672-45

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.

  1. Import wiringPi/I2C library, oled display library, file control library, IP reading library, and disk reading library.

DFR0672-46

  1. Define parameters about temperature, system information, disk information, and IP address.

DFR0672-47

  1. Init oled screen, and output init success information from the terminal.

DFR0672-48

  1. Read system information. Display sysinfo-Error on the OLED if it fails, and re-read 0.5s later.

DFR0672-49

  1. Read CPU utilization. There is a sprintf function inside for concatenating strings.

DFR0672-50

  1. 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;

DFR0672-53

  1. Read IP address of cable network and WiFi, prior to display WiFi IP address.

DFR672-52

  1. Read temperature.

DFR0672-54

  1. Read disk space.

DFR0672-55

  1. 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.

DFR0672-56

7. Raspberry Pi Auto-start

  1. Create script start.sh
nano /home/pi/temp_control/start.sh

Input the following content:

DFR0672-57

Press ctrl+X+Y to save, and tap button "Enter".

  1. 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:

DFR0672-58

  1. 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
  1. 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.

  1. Input top into the terminal to open process table.

DFR0672-59

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.

  1. 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.

DFR0672-60

  1. 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 &

DFR0672-61

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

More Documents