问小白 wenxiaobai
资讯
历史
科技
环境与自然
成长
游戏
财经
文学与艺术
美食
健康
家居
文化
情感
汽车
三农
军事
旅行
运动
教育
生活
星座命理

树莓派4B散热性能测试方法详解

创作时间:
作者:
@小白创作中心

树莓派4B散热性能测试方法详解

引用
CSDN
1.
https://m.blog.csdn.net/orDream/article/details/140878052

树莓派4B作为一款功能强大的单板计算机,在运行高负载任务时容易产生大量热量,因此散热性能成为影响其稳定性和使用寿命的关键因素。本文将详细介绍如何通过Python脚本监测树莓派4B在负载下的温度变化,帮助用户更好地了解和优化其散热性能。

树莓派引脚

树莓派 40Pin 引脚对照表

rpi-pins-40-0

注:本表格适用于各版本,并且兼容26Pin的树莓派B,树莓派B为26Pin,其引脚对应于上表的前26Pin。

使用下面这款 GPIO 参考卡片,让引脚功能一目了然,接线操作起来更方便。

性能测试代码

要在树莓派4B上测试其性能并监测温度变化,可以编写一个Python脚本,利用stress命令来生成负载,使用vcgencmd命令来监测温度。以下是一个简单的例子:

  1. 安装所需的软件包:
sudo apt update
sudo apt install stress
  1. 编写Python脚本来执行性能测试并监控温度变化:
import os
import time
import subprocess

def get_temperature():
    result = subprocess.run(['vcgencmd', 'measure_temp'], capture_output=True, text=True)
    temp_str = result.stdout
    temp_value = float(temp_str.split('=')[1].split("'")[0])
    return temp_value

def run_stress_test(duration):
    # Start stress test
    stress_process = subprocess.Popen(['stress', '--cpu', '4', '--timeout', str(duration)], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    return stress_process

def monitor_temperature(interval, duration):
    end_time = time.time() + duration
    while time.time() < end_time:
        temp = get_temperature()
        print(f"Temperature: {temp}°C")
        time.sleep(interval)

if __name__ == "__main__":
    test_duration = 300  # Test duration in seconds (e.g., 5 minutes)
    monitoring_interval = 10  # Interval in seconds to monitor temperature
    print("Starting stress test...")
    stress_process = run_stress_test(test_duration)
    
    print("Monitoring temperature...")
    monitor_temperature(monitoring_interval, test_duration)
    stress_process.wait()
    print("Stress test completed.")

说明:

  1. 安装依赖包:确保安装了stress命令来生成负载。
  2. 获取温度:get_temperature函数使用vcgencmd measure_temp命令来获取当前的CPU温度。
  3. 运行压力测试:run_stress_test函数使用stress命令来生成CPU负载,持续指定的时间。
  4. 监测温度变化:monitor_temperature函数定期(每隔指定的时间)获取并打印温度,持续整个测试期间。
  5. 主程序:设置测试的持续时间和监测间隔,运行压力测试,并在测试过程中监测温度。

将上述代码保存为stress_test.py并运行:

python3 stress_test.py

这将会开始性能测试并每隔指定的时间打印树莓派的温度。你可以根据需要调整测试的持续时间和监测间隔。

此外,也可以通过以下命令查看温度:

watch -n 1 "awk '{print \"当前温度为: \" int(\$1/1000) \"°C\"}' /sys/class/thermal/thermal_zone0/temp"
# 或者
watch -n 1 "awk '{printf \"当前温度为: %.1f°C\\n\", \$1/1000}' /sys/class/thermal/thermal_zone0/temp"

reference

@misc{BibEntry2024Aug,
title = {{树莓派 40Pin 引脚对照表 {∣ \vert∣} 树莓派实验室}},
year = {2024},
month = aug,
urldate = {2024-08-02},
language = {chinese},
note = {[Online; accessed 2. Aug. 2024]},
url = {https://shumeipai.nxez.com/raspberry-pi-pins-version-40}
}

© 2023 北京元石科技有限公司 ◎ 京公网安备 11010802042949号