我是靠谱客的博主 拼搏钥匙,这篇文章主要介绍树莓派4B Ubuntu 21.04 自动温控开关风扇以及RPi.GPIO避坑指南,现在分享给大家,希望可以做个参考。

本人对树莓派有一些了解,虽然学过模电数电,但也只是学过,过于硬件的东西也不懂。

好了,首先说明,树莓派GPIO引脚都是3.3v的,一般风扇都是5v的,所以不能用GPIO直接供电。即使你的风扇额定电压为 3V, GPIO的电流也难以驱动它, 即使能驱动也不推荐这么干, 一个原因是可能会损坏树莓派, 根据RaspberryPi官方GPIO使用文档明确的警告指出, 不能将电机直接接在 GPIO 针脚上(风扇实际上就是由电机驱动的), 另一个是通过这么小电流驱动的风扇真的能起到散热作用吗?

树莓派可以查看cpu温度,Ubuntu的也一样,通过一直更新一个文件来记录温度,可以使用命令查看:

复制代码
1
cat /sys/class/thermal/thermal_zone0/temp

接着便是GPIO模块,本人在安装GPIO模块也是遇到了许多问题,网上找到的许多命令也都试过,使用apt-get报错无法定位软件包,也可能是我树莓派源的问题,(换源什么的都试过)因为安装别的东西也遇见过,了解的大佬可以评论告知,十分感谢。

这些都是没安装成功的命令:

复制代码
1
2
3
4
5
6
sudo apt-get install python-dev python-rpi.gpio sudo apt-get install python3-dev python3-rpi.gpio sudo apt-get install python3-rpi.gpio sudo apt-get -y install python3-rpi.gpio sudo apt-get -y install python3-rpi.gpio sudo apt-get install RPi.GPIO

使用pip安装也是很长的报错,最终在网上找到相同的问题,同样是Ubuntu的树莓派,也在评论找到了解决方法。https://askubuntu.com/questions/1290037/error-while-installing-rpi-gpio-as-error-command-errored-out-with-exit-status

加一行export命令就行了,具体不懂,感谢这位大佬!

之后依然有问题,python引入GPIO模块时会报错 

复制代码
1
RuntimeError: Not running on a RPi!

又是一番搜索,终于又找到了相同的问题,同为ubuntu,我不禁感叹,爱折腾的人还真不少,https://github.com/gpiozero/gpiozero/issues/837

也是在评论区,同样感谢这位大佬QAQ

不过貌似重启之后还要执行上述命令,否则还是会报错,因为我执行过这个命令,当时引入GPIO没问题,第二天运行报错了,执行之后就行了,但好像也没重启过树莓派,比较迷,没仔细研究过。

接着就OK了。上代码。

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import sys import time try: import RPi.GPIO as GPIO except RuntimeError: print("Error importing RPi.GPIO! This is probably because you need superuser privileges. You can achieve this by using 'sudo' to run your script") def cpu_temp(): with open("/sys/class/thermal/thermal_zone0/temp", 'r') as f: return float(f.read())/1000 def main(): channel = 18 GPIO.setmode(GPIO.BCM) GPIO.setwarnings(False) # close air fan first GPIO.setup(channel, GPIO.OUT, initial=GPIO.LOW) is_close = True while True: temp = cpu_temp() if is_close: if temp > 52.0: print(time.ctime(), temp, 'open air fan') GPIO.output(channel, GPIO.HIGH) is_close = False else: if temp < 45.0: print(time.ctime(), temp, 'close air fan') GPIO.output(channel, GPIO.LOW) is_close = True time.sleep(10.0) print(time.ctime(), temp) if __name__ == '__main__': main()

本代码来源https://testerhome.com/topics/8068,做了些修改,亲测可行。

最后就是硬件了,参考了https://lsun.net/posts/raspberry-pi-auto-fan/#%E8%8E%B7%E5%8F%96soc%E5%AE%9E%E6%97%B6%E6%B8%A9%E5%BA%A6

所需要的就是一个三极管(不重要,是三极管就行,我就是随便找的)和杜邦线,通过饱和/截至实现开关(补了三极管的课了qaq)

三极管中间基极接GPIO18,也就是代码中的channel,这个可以自己选,通过GPIO18的开关控制三极管工作状态实现开关,供电还是接树莓派5v,三极管旁边两个随便接(应该是)我是发射极接风扇负极,集电极接GPIO的GROUND,风扇正极接5v

附录:本人已亲测每次开机都要重新执行

复制代码
1
2
cd /dev chmod og+rwx gpio*

所以在auto_fan.py开头添加

复制代码
1
2
3
4
5
6
import subprocess # just to call an arbitrary command e.g. 'ls' import os sudoPassword = '123456' command = 'chmod og+rwx gpio*' os.chdir('/dev') os.system('echo %s|sudo -s %s' % (sudoPassword, command))

由于执行上述命令需要root权限,所以在脚本脚本中以root执行,但还是需要手动输入密码,而且开机自启什么的也没有实现(主要就是因为需要root权限),我对shell命令了解的并不多,也懒得去折腾了,就手动开启吧。

auto_fan.py后台运行的sh脚本

复制代码
1
2
3
4
#!/bin/bash #python.sh python -u /home/pi/auto_fan.py >/home/pi/auto_fan.log 2>&1 &

最后附上运行日志,真舒服!

初次写文章,也比较忙,没有好好整理思路,如有问题还请指出

所有参考:

https://testerhome.com/topics/8068

https://askubuntu.com/questions/1290037/error-while-installing-rpi-gpio-as-error-command-errored-out-with-exit-status

https://zhuanlan.zhihu.com/p/40594358

https://lsun.net/posts/raspberry-pi-auto-fan/#%E6%8E%A5%E7%BA%BF%E5%87%86%E5%A4%87

RuntimeError: Not running on a RPi! with Ubuntu for Raspberry Pi · Issue #837 · gpiozero/gpiozero · GitHub

 

最后

以上就是拼搏钥匙最近收集整理的关于树莓派4B Ubuntu 21.04 自动温控开关风扇以及RPi.GPIO避坑指南的全部内容,更多相关树莓派4B内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(56)

评论列表共有 0 条评论

立即
投稿
返回
顶部