Skip to content

Commit 3cc9386

Browse files
authored
Merge pull request #1 from cgomesu/dev
Dev - new version
2 parents 98f654d + 2deeed0 commit 3cc9386

5 files changed

Lines changed: 583 additions & 96 deletions

File tree

README.md

Lines changed: 94 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,115 @@
11
# nanopim4-satahat-fan
2-
Bash scripts to control the **[2-PIN PH2.0 12v fan connector from the Nano Pi M4 SATA hat](http://wiki.friendlyarm.com/wiki/index.php/NanoPi_M4_SATA_HAT)** on **Armbian OS** (originally tested with *Kernel 5.4*). I added comments to each script that might help you change it according to your needs, especially the fan `period` and `duty_cycle`.
2+
A fan control script in Bash for the [**2-pin PH2.0 12v fan connector of the NanoPi M4 SATA hat**](http://wiki.friendlyarm.com/wiki/index.php/NanoPi_M4_SATA_HAT). By default, the script uses a bounded [logistic model](https://en.wikipedia.org/wiki/Logistic_function) with a moving mid-point (based on the average temperature over time) to set the fan speed.
3+
4+
Many of the variables use in this fan controller can be modified directly from the CLI, such as setting custom temperature thresholds (`-t`, `-T`) or disabling temperature monitoring altogether (`-f`). For a more detailed description, see [**Usage**](#usage).
5+
6+
There's arguably more code here than necessary to run a fan controller. This was a hobbie of mine (I wanted to revisit the first version which used a fixed table to set speed) and an opportunity to learn more about Bash and the sysfs interface.
7+
8+
If you have any issues or suggestions, open an issue or [send me an e-mail](mailto:me@cgomesu.com).
9+
10+
11+
# Requisites
12+
- GNU bash;
13+
- Access to the [pwm sysfs interface](https://www.kernel.org/doc/Documentation/pwm.txt);
14+
- Standard Linux commands.
15+
16+
You don't need to check any of this manually. The script will automatically check for everything it needs to run and will let you know if there's any errors or missing access to important commands.
17+
18+
The controller was developed with Armbian OS but you should be able to run it on any other Linux distro for the NanoPi M4. For reference, this script was originally developed with the following hardware:
19+
- NanoPi-M4 v2
20+
- M4 SATA hat
21+
- Generic 12V (0.2A) fan
22+
23+
And software:
24+
- Kernel: Linux 4.4.231-rk3399
25+
- OS: Armbian Buster (20.08.9) stable
26+
- GNU bash v5.0.3
27+
- bc v1.07.1
328

4-
# Authorship
5-
This is a slightly modified version of **[mar0ni's script](https://forum.armbian.com/topic/11086-pwm-fan-on-nanopi-m4/?tab=comments#comment-95180)**.
629

730
# Installation
831
```
932
apt-get update
1033
apt-get install git
1134
cd /opt
35+
36+
# From now on, if you're not running as root, append 'sudo' if you run into permission issues
1237
git clone https://github.com/cgomesu/nanopim4-satahat-fan.git
1338
cd nanopim4-satahat-fan
14-
# Allow the ON and OFF scripts to be executed
15-
chmod +x pwm-fan-on.sh
16-
chmod +x pwm-fan-off.sh
17-
# Test run the ON script
18-
bash pwm-fan-on.sh
19-
# Press ctrl+c after a few seconds to send a SIGINT and stop the script
20-
# Test run the OFF script to disable the fan
21-
bash pwm-fan-off.sh
39+
40+
# Allow the script to be executed
41+
chmod +x pwm-fan.sh
42+
43+
# Test the script
44+
./pwm-fan.sh
45+
46+
# Check for any error messages
47+
# When done, press Ctrl+C after to send a SIGINT and stop the script
48+
```
49+
50+
51+
# Usage
52+
```
53+
./pwm-fan.sh -h
54+
```
2255
```
56+
Usage:
57+
58+
./pwm-fan.sh [OPTIONS]
59+
60+
Options:
61+
-c st Name of the PWM CHANNEL (e.g., pwm0, pwm1). Default: pwm0
62+
-C st Name of the PWM CONTROLLER (e.g., pwmchip0, pwmchip1). Default: pwmchip1
63+
-d in Lowest DUTY CYCLE threshold (in percentage of the period). Default: 25
64+
-D in Highest DUTY CYCLE threshold (in percentage of the period). Default: 100
65+
-f Fan runs at FULL SPEED all the time. If omitted (default), speed depends on temperature.
66+
-F in TIME (in seconds) to run the fan at full speed during STARTUP. Default: 60
67+
-h Show this HELP message.
68+
-l in TIME (in seconds) to LOOP thermal reads. Lower means higher resolution but uses ever more resources. Default: 10
69+
-m st Name of the DEVICE to MONITOR the temperature in the thermal sysfs interface. Default: soc
70+
-p in The fan PERIOD (in nanoseconds). Default (30kHz): 30000000.
71+
-s in The MAX SIZE of the TEMPERATURE ARRAY. Interval between data points is set by -l. Default (store last 1min data): 6.
72+
-t in Lowest TEMPERATURE threshold (in Celsius). Lower temps set the fan speed to min. Default: 25
73+
-T in Highest TEMPERATURE threshold (in Celsius). Higher temps set the fan speed to max. Default: 75
74+
75+
If no options are provided, the script will run with default values.
76+
Defaults have been tested and optimized for the following hardware:
77+
- NanoPi-M4 v2
78+
- M4 SATA hat
79+
- Fan 12V (.08A)
80+
And software:
81+
- Kernel: Linux 4.4.231-rk3399
82+
- OS: Armbian Buster (20.08.9) stable
83+
- GNU bash v5.0.3
84+
- bc v1.07.1
85+
86+
Author: cgomesu
87+
Repo: https://github.com/cgomesu/nanopim4-satahat-fan
88+
89+
This is free. There is NO WARRANTY. Use at your own risk.
90+
91+
```
92+
93+
94+
# Examples
95+
- Run with a custom period and min/max temperature thresholds
96+
97+
```
98+
./pwm-fan.sh -p 25000000 -t 30 -T 60
99+
```
100+
101+
102+
# Run in the Background
103+
If you're running options different than the default values, first edit the `pwm-fan.service` file to include those options in the `ExecStart=` row.
23104

24-
# Run it as a service
25105
```
26106
# Copy the pwm-fan.service file to your systemd folder
27107
cp /opt/nanopim4-satahat-fan/pwm-fan.service /lib/systemd/system/
108+
28109
# Enable the service and start it
29110
systemctl enable pwm-fan.service
30111
systemctl start pwm-fan.service
112+
31113
# Check the service status to make sure it's running without issues
32114
systemctl status pwm-fan.service
33-
# Make sure the OFF script is running corrently when the service is stopped
34-
systemctl stop pwm-fan.service
35-
# Then start the service again
36-
systemctl start pwm-fan.service
37115
```
38-
If you have permission issues, use a sudo user and add `sudo` before each command

pwm-fan-off.sh

Lines changed: 0 additions & 16 deletions
This file was deleted.

pwm-fan-on.sh

Lines changed: 0 additions & 56 deletions
This file was deleted.

pwm-fan.service

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
11
[Unit]
2-
Description=A bash script to control the PWM1 fan from the NanoPi M4 SATA hat
2+
Description=Fan control for the NanoPi M4 SATA hat
33

44
[Service]
5-
# Edit the directory below if you placed the Exec scripts
65
Type=simple
76

8-
# Exec scripts
9-
ExecStart=/bin/bash /opt/nanopim4-satahat-fan/pwm-fan-on.sh
10-
ExecStop=/bin/bash /opt/nanopim4-satahat-fan/pwm-fan-off.sh && /bin/kill -2 $MAINPID
7+
# Edit the directory below if you placed the Exec scripts elsewhere
8+
ExecStart=/bin/bash /opt/nanopim4-satahat-fan/pwm-fan.sh
9+
ExecStop=/bin/kill -2 $MAINPID
1110

1211
# Restart options
13-
Restart=always
12+
Restart=on-failure
1413
RestartSec=15
1514

1615
[Install]
1716
WantedBy=multi-user.target
18-

0 commit comments

Comments
 (0)