树莓派使用GPIO控制风扇转速方法

Python 同时被 2 个专栏收录
4 篇文章 0 订阅
1 篇文章 0 订阅

        最近购置了一套树莓派3B+,各种折腾。尝试安装了官方的raspbian系统、游戏模拟器laka系统、家庭多媒体系统libreELEC等,其实安装方法大同小异,基本都是网上先下载镜像,然后用Etcher烧录到SD卡或U盘。官方建议一般烧录到SD卡,但各种原因还是觉得烧录到U盘比较稳妥。安装raspbian不需做任何修改,烧录完成直接插到USB,上电启动即可。烧录非官方系统可能要修改一些配置,一般是修改cmdline.txt配置文件。网上有很多教程,这里就不赘述了。

        下面重点讲下散热问题,当前的室温是25度左右,如果不安装风扇,仅仅靠贴上去的散热片散热,系统没跑任何任务,CPU温度为56左右。直接插上风扇全速运行,温度在39度左右。总感觉树莓派如果7*24小时运行的话,风扇挺耗电的。所以考虑用一个GPIO控制风扇的转速。主要硬件部分就是加一个普通的三极管就可以了。然后使用GPIO输出PWM占空比控制风扇转速。

具体代码如下:

# -*- coding: utf-8 -*-

import RPi.GPIO as GPIO
import time

# 控制风扇的GPIO
FAN_GPIO = 11
# 低温阈值,低于它则关闭风扇
MIN_TEMP = 35
# 高温阈值,高于它则全速运转
MAX_TEMP = 55
# 多长时间读取一次CPU温度,单位秒
SAMPLING = 30


# 单位为千分之一度
def get_cpu_temp():
    with open('/sys/class/thermal/thermal_zone0/temp') as f:
        cpu_temp = int(f.read())
    return cpu_temp


def main():
    GPIO.setwarnings(False)
    GPIO.setmode(GPIO.BOARD)
    GPIO.setup(FAN_GPIO, GPIO.OUT)

    # 初始化PWM的频率,frequency=50Hz
    pwm = GPIO.PWM(FAN_GPIO, 50)
    # 为防止风扇卡死,开机全速运行n秒
    pwm.start(100)
    time.sleep(5)
    pwm.stop()

    try:
        while 1:
            temp = get_cpu_temp()
            print('CPU temperature:', temp)
            if temp < MIN_TEMP * 1000:
                # 低于低温阈值,则关闭风扇
                print('fan to stop running')
                pwm.stop()
            elif temp > MAX_TEMP * 1000:
                # 超过高温阈值,则全速运行
                print('full-speed operation')
                pwm.start(100)
            else:
                # 在低温阈值和高温阈值之间时,则根据占空比使用PWM控制风扇转速。
                dc = (temp - MIN_TEMP * 1000) * 100 / ((MAX_TEMP - MIN_TEMP) * 1000)
                pwm.start(dc)
                print('PWM duty cycle:', dc)
            # 设置采样频率
            time.sleep(SAMPLING)
    except KeyboardInterrupt:
        pass

    pwm.stop()
    GPIO.cleanup()


if __name__ == '__main__':
    main()

最后给自己写的小游戏打下广告,一个释放你压力的小游戏,一个你敢叫它敢动的小游戏。敢不敢来试下?

点击下载,或在 Google Play Store 搜索 Make a Noise

  • 6
    点赞
  • 7
    评论
  • 31
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

笔记本的风扇控制 ---------------------------------------- 09 November 2006. Summary of changes for version 20061109: 1) ACPI CA Core Subsystem: Optimized the Load ASL operator in the case where the source operand is an operation region. Simply map the operation region memory, instead of performing a bytewise read. (Region must be of type SystemMemory, see below.) Fixed the Load ASL operator for the case where the source operand is a region field. A buffer object is also allowed as the source operand. BZ 480 Fixed a problem where the Load ASL operator allowed the source operand to be an operation region of any type. It is now restricted to regions of type SystemMemory, as per the ACPI specification. BZ 481 Additional cleanup and optimizations for the new Table Manager code. AcpiEnable will now fail if all of the required ACPI tables are not loaded (FADT, FACS, DSDT). BZ 477 Added #pragma pack(8/4) to acobject.h to ensure that the structures in this header are always compiled as aligned. The ACPI_OPERAND_OBJECT has been manually optimized to be aligned and will not work if it is byte-packed. Example Code and Data Size: These are the sizes for the OS- independent acpica.lib produced by the Microsoft Visual C++ 6.0 32- bit compiler. The debug version of the code includes the debug output trace mechanism and has a much larger code and data size. Previous Release: Non-Debug Version: 78.1K Code, 17.1K Data, 95.2K Total Debug Version: 155.4K Code, 63.1K Data, 218.5K Total Current Release: Non-Debug Version: 77.9K Code, 17.0K Data, 94.9K Total Debug Version: 155.2K Code, 63.1K Data, 218.3K Total 2) iASL Compiler/Disassembler and Tools: Fixed a problem where the presence of the _OSI predefined control method within complex expressions could cause an internal compiler error. AcpiExec: Implemented full region support for multiple address spaces. SpaceId is now part of the REGION object. BZ 429 ---------------------------------------- 11 Oc
©️2021 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值