树莓派——PWM温控风扇程序(C++、python)、开机自启动

news/2025/2/12 20:13:13/

原理

读取/sys/class/thermal/thermal_zone0/temp文件,返回树莓派温度,控制GPIO产生PWM,从而实现温控风扇的控制。

C/C++代码

命令行操作

cd /home/pi/code/basic/fan
nano fan.c
#复制代码
gcc fan.c -o fan -lwiringPi
./fan
#include <stdio.h>
#include <stdlib.h> 
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "wiringPi.h"     //添加库文件
#include "softPwm.h"     //添加库文件#define TEMP_PATH "/sys/class/thermal/thermal_zone0/temp"
#define MAX_SIZE 32#define PWM_Pin 25         //定义PWM_Pin引脚static int GetDuty(int temp);
unsigned int Duty = 0;int main(void) 
{int fd;double temp = 0;char buf[MAX_SIZE];wiringPiSetup();//初始化wiringPisoftPwmCreate(PWM_Pin,0,100);//当前pwmRange为100,频率为100Hz,若pwmRange为50时,频率为200,若pwmRange为2时,频率为5000。softPwmWrite(PWM_Pin,50);//占空比 = value/pwmRange,当前占空比 = 50/100 = 50%while(1){// 打开/sys/class/thermal/thermal_zone0/tempfd = open(TEMP_PATH, O_RDONLY);if (fd < 0) {fprintf(stderr, "failed to open thermal_zone0/temp\n");return -1;}// 读取内容if (read(fd, buf, MAX_SIZE) < 0) {fprintf(stderr, "failed to read temp\n");return -1;}// 转换为浮点数打印temp = atoi(buf) / 1000.0;// 关闭文件close(fd);Duty = GetDuty(temp);softPwmWrite(PWM_Pin,Duty);printf("temp: %.2f,Duty: %d\n", temp, Duty);delay(1000*10);}
}static int GetDuty(int temp)
{if(temp >= 60){return 100;}else if(temp >= 55){return 90;}else if(temp >= 53){return 80;}else if(temp >= 50){return 70;}else if(temp >= 45){return 60;}else{return 50;}
}

python程序

import RPi.GPIO as GPIO  #导入gpio口驱动模块
import time              #导入时间模块
pwm_pin = 26             #定义pwm输出引脚(注意BCM编码的对应关系)GPIO.setmode(GPIO.BCM)   #定义树莓派gpio引脚以BCM方式编号
GPIO.setup(pwm_pin,GPIO.OUT)  #使能gpio口为输出
pwm = GPIO.PWM(pwm_pin,100)   #定义pwm输出频率
pwm.start(50)   #占空比(范围:0.0 <=  >= 100.0)while(1):# 打开文件file = open("/sys/class/thermal/thermal_zone0/temp")# 读取结果,并转换为浮点数temp = float(file.read()) / 1000# 关闭文件file.close()if(temp >= 60):Duty = 100elif(temp >= 55):Duty = 90elif(temp >= 53):Duty = 80elif(temp >= 50):Duty = 70   elif(temp >= 45):Duty = 60else:Duty = 50pwm.ChangeDutyCycle(Duty)# 向控制台打印print("temp : %.1f,duty : %d" %(temp,Duty))time.sleep(10)

开机自启动

制作sh脚本

#!/bin/bash
/home/pi/code/basic/fan/fan

通过桌面自启动

mkdir .config/autostart
nano .config/autostart/fan.desktop#内容
[Desktop Entry]
Type=Application
Name=fan
NoDisplay=true
Exec=/home/pi/code/basic/fan/fan.shsudo reboot

参考:
[1]https://blog.csdn.net/xukai871105/article/details/38349209
[2]https://blog.csdn.net/weixin_42078116/article/details/119329536
[3]https://www.yahboom.com/build.html?id=4182&cid=308
[4]https://www.yahboom.com/build.html?id=2679&cid=308
[5]https://blog.csdn.net/qq_42714905/article/details/97131771


http://www.ppmy.cn/news/496722.html

相关文章

基于STM32的pwm温度风扇(带人体检测)

一.硬件方案 本次设计采用STM32单片机做主控芯片&#xff0c;通过DS18B20采集温度&#xff0c;将温度显示在 LCD1602上。通过人体传感器检测周围是否有人存在&#xff0c;并根据温度的不同&#xff0c;利用STM32对风扇进行调速。 主要由STM32单片机LCD1602液晶显示DS18B20温度…

GPIO模拟PWM驱动风扇Linux设备驱动

前言 内核版本 linux5_10&#xff1b;GPIO产生PWM方波&#xff0c;控制风扇转速&#xff1b;本驱动可在应用层 /sys/class/pwm/下生成文件接口&#xff0c;通过脚本进行配置&#xff1b;本驱动可将该 PWM 设备树节点用于 thermal 节点中&#xff0c;通过与传感器关联&#xff…

基于STM32单片机智能蓝牙PWM温控风扇带定时APP控制

实践制作DIY- GC0009-智能蓝牙PWM温控风扇 一、功能说明&#xff1a; 基于STM32单片机设计-智能蓝牙PWM温控风扇 功能介绍&#xff1a; 1.基于STM32F103C系列&#xff0c;OLED&#xff0c;DHT11温度湿度传感器&#xff0c;HC-05蓝牙模块&#xff0c;HSR04超声波测距。 2.P…

树莓派4b:PWM调速风扇+DIY亚克力板外壳的定制降温方案

宿舍里的校园网只能连接两台设备&#xff0c;于是我就给树莓派4B装上了openwrt&#xff0c;加上淘宝上买的1200M无限网卡&#xff08;&#xff01;买的时候注意型号和宽度大小&#xff09;作为路由器。但在官方外壳里&#xff0c;树莓派温度稳定在60~70度&#xff0c;所以就考虑…

51单片机 Proteus仿真 PID 电机转速调节 PWM 风扇转速调节

/* PID的参数设置可以参照以下来进行:   参数整定找最佳&#xff0c;从小到大顺序查;   先是比例后积分&#xff0c;最后再把微分加;   曲线振荡很频繁&#xff0c;比例度盘要放大;   曲线漂浮绕大湾&#xff0c;比例度盘往小扳;   曲线偏离回复慢&#xff0c;积分时…

python控制树莓派风扇_gpio控制风扇转速 pwm方案

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼 #最新的保持温度恒定的代码&#xff0c;把——改成空格或者\t # #!/usr/bin/env python # encoding: utf-8 import RPi.GPIO import time import sys import math RPi.GPIO.setwarnings(False) RPi.GPIO.setmode(RPi.GPIO.BCM) #这…

STM32PWM控制智能风扇

设计思路&#xff1a; 这个是一个STM32通过定时器产生PWM波控制小风扇的设计&#xff0c;首先STM32驱动ds18b20温度传感器采集环境温度&#xff0c;然后通过按键设置温度的阈值&#xff0c;不同的温度范围定时器产生的PWM波不同&#xff0c;相应的小风扇的转速也会不一样&…

四线直流风扇PWM调速探研

最近拿到个9cm风扇&#xff0c;试了下可以使用&#xff0c;这个风扇是常见的用于机柜或机箱的工业扇&#xff08;高转速高电流&#xff09;&#xff0c;因此在接通典型12V后非常暴力&#xff0c;怠速电压在4v左右。 这篇文章将说明电脑风扇如何调速&#xff0c;为自制控制器预研…