• sales

    +86-0755-88291180

Raspberry Pi ------ 3.5 Change RGB color according to CPU temperature User Guide

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


TAG: Windows Environment User Guide Raspberry-Pi-A7670E-GSM-GPRS-GNSS-LTE-Cat-1-2G-A-User-Guide Raspberry Pi 5 Official Original AI Kit Hailo8l 13Tops/26Tops PCIe M.2 HAT+ Board For Pi5 Raspberry Pi 1.54 inch e-Paper HAT+ (E) E-Ink E6 Full color 240x240 Screen Display For Arduino /STM32 /ESP32/Jetson Nano ESP32 ST7789 configuration ESP32 ST7789 tutorial Raspberry Pi 0.85inch LCD Computer RS485 to RJ45 SpotPear ESP32-S3 Development Board 1.85 inch Round LCD N16R8 DeepSeek AI Voice Chat Robot BOX Magnetic-charging X1006 Raspberry Pi 5 PCIe To 2.5 SATA And M.2 KEY-B 2280 SSD/HDD Double-SATA (NOT BOOT From SATA/SSD ESP32-P4 DEV-KIT C6 WiFi6 MIPI DSI 7/10.1 inch Display/CSI Camera/Audio Speaker For AI Deepseek Raspberry Pi ESP32-P4-WIFI6 4.3inch LCD Display AI Development Board 480×800 4.3 inch TouchScreen Deepseek ESP32 1.9 inch LCD Capacitive TouchScreen 1.9inch Display ST7789 170x320 for Arduino /Raspberry Pi /Pico Raspberry Pi OpenWrt Tutorial 1 RS485 to Ethernet ESP32 S3 Development Board 1.46 inch Round LCD 1.46inch Display 412x412 QMI8658 Sensor / MIC / SD /MP3 Audio /Battery Port LVGL/HMI For Arduino X1009 Raspberry Pi 5 PCIe To 5-Port SATA SATA3.0 (NOT BOOT From SATA)