[pi4, rpi4, 라즈베리파이4] ice tower fan speed control
https://wiki.52pi.com/index.php?title=ZP-0129-4wire
Raspberry Pi OS 다운로드 (구, Raspbian-라즈비안)에서 작동은 쉬웠습니다.
- 메뉴얼만 보고, 따라하면 잘 작동이 됩니다.
이번에 rpi4 에 우분투 22.04.2 LTS (64bit) 설치 하면서,
fan 컨트롤 하는법을 다시 익혀야 했습니다.
sudo apt-get update
sudo apt install python3-pip
sudo pip3 install RPi.GPIO
pip freeze |grep RPi.GPIO
feedback ; RPi.GPIO==0.7.1
sudo nano pwm-fan.py
아래 내용을 붙여 넣습니다.
import RPi.GPIO as GPIO
import time
import subprocess as sp
# initializing GPIO, setting mode to BOARD.
# Default pin of FAN Adapter is physical pin 32, GPIO12;
Fan = 8 #if you connect to pin txd physical pin 8, GPIO14,then set to :Fan = 8
GPIO.setmode(GPIO.BOARD)
GPIO.setup(Fan, GPIO.OUT)p = GPIO.PWM(Fan, 50)
p.start(0)try:
while True:
temp = sp.getoutput("vcgencmd measure_temp|egrep -o '[0-9]*\.[0-9]*'")
# print(temp)
if float(temp) < 58.0:
p.ChangeDutyCycle(0)
elif float(temp) > 58.0 and float(temp) < 68.0:
p.ChangeDutyCycle(50) #속도를 50으로 감소, 소음 제거됨
time.sleep(120) #120초 간격으로 다음명령어 실행
elif float(temp) > 68.0:
p.ChangeDutyCycle(100)
time.sleep(120)except KeyboardInterrupt:
pass
p.stop()
GPIO.cleanup()
python3 pwm-fan.py &
아래 사진처럼 좌측에 붙여서 정렬해야 합니다.
ubuntu 시작할때, 스크립트 실행하기 (안됨)
sudo crontab -e
sudo chmod +x /home/hs7/pwm-fan.py
@reboot sudo python /home/hs7/pwm-fan.py &
** ubuntu 시작할때, 스크립트 실행하기 (2023.12.10)
- pwn-fan.service 파일을 만듭니다.
sudo nano /etc/systemd/system/pwm-fan.service
- 아래 내용을 넣습니다.
[Unit]
Description=pwm-fan
After=network.target[Service]
Type=simple
User=hs7 #root 로 하니까 안됨(이부분삭제해야함)
WorkingDirectory=/home/hs7
ExecStart=/usr/bin/python3 /home/hs7/pwm-fan.py &
Restart=always[Install]
WantedBy=multi-user.target
- enable
$sudo systemctl enable pwm-fan.service
Created symlink /etc/systemd/system/multi-user.target.wants/pwm-fan.service → /etc/systemd/system/pwm-fan.service.
$sudo systemctl start pwm-fan.service
- 안된다면, 속성 확인
hs7@hcp:~$ ls -al
total 40
drwxr-x--- 5 hs7 hs7 4096 Dec 11 20:23 .
drwxr-xr-x 4 root root 4096 Dec 12 09:56 ..
-rw------- 1 hs7 hs7 1211 Dec 12 10:54 .bash_history
-rw-r--r-- 1 hs7 hs7 220 Jan 7 2022 .bash_logout
-rw-r--r-- 1 hs7 hs7 3771 Jan 7 2022 .bashrc
drwx------ 3 hs7 hs7 4096 Dec 11 20:22 .cache
drwxrwxr-x 3 hs7 hs7 4096 Dec 11 20:22 .local
-rw-r--r-- 1 hs7 hs7 807 Jan 7 2022 .profile
drwx------ 2 hs7 hs7 4096 Dec 11 18:50 .ssh
-rw-r--r-- 1 hs7 hs7 0 Dec 11 18:50 .sudo_as_admin_successful
-rw-r--r-- 1 root root 880 Dec 11 20:23 pwm-fan.py
$sudo chmod 755 pwm-fan.py