• sales

    +86-0755-88291180

Raspberry Pi ------ 3.3 Get CPU temperature,Adjust fan speed User Guide

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.

TAG: Core1121 LoRa LR1121 HF LF SPI For Sub-GHz and 2.4GHz Serial UART Bus Servo TTL ST3235 30KG.CM Magnetic Encoder 360° High Precision And Large Torque ESP32 0.85inch LCD ESP32-P4 ESP32-C6 Development Board RISC-V WiFi6 MIPI CSI Camera 7/10.1 inch DSI Display /RJ45 Ethernet /Mic /Audio Size Compatible with Raspberry Pi Modbus-Protocol-Specification User Guide ESP32-S3 0.85inch LCD Raspberry Pi 12.3 inch HDMI LCD Captive TouchScreen Display 1920x720 For Jetson Series/Computer PC DeepSeek XiaoZhi AI Voice Chat Robot BOX ESP32-S3 Development Board 1.85 inch Round LCD N16R8 Raspberry Pi screen Core3566104000 Raspberry Pi Zero Pi0 2W Digital Photo Frame 7.3 inch E6 Full Color E Paper Ink Calendar Art Poster luckfox-pico-ultra-w-WIFI-Bluetooth Capacitive Touchscreen Raspberry Pi PICO LoRa-HF E-Paper D-Robotics RDK X3 Development Board Horizon Sunrise Pi ARM Cortex-A53 5Tops 2GB/4GB RAM 5inch Raspberry Pi Touch Display 2 5-inch Official Original 720x1280 LCD DSI/MIPI Screen Laser 450nm 5W high precision module Adjustable focal length For Laser Carving / Laser 3D Printing X1003 Raspberry Pi 5 PCIe to M.2 NVMe SSD MINI Adapter Board HAT Pi5 2242 2230 Raspberry Pi 5 PCIe to 4 Port 2.5G Gigabit Ethernet Adapter RJ45 Ports