Previously, I added a USB cooling fan to the X96 Air TV box. The problem with this mod is that the fan is always running, and it runs at full speed. Ideally, the fan should kick in only when the CPU temperature is above a certain threshold. It would be even better if there is a way to control the fan speed.
Dan McDonald left me a comment pointing to his project on Github. He basically connected the fan to a USB relay that can be controlled by Python script. His project inspired me to make a similar mod that would make use of the spare D1 Mini boards I have lying around.
The plan is to hook up the fan to a MOSFET (2N7000) and control it via PWM. Here's the very simple circuit:
The code simply reads a single character from the serial port (0 - 9). 0 will turn the fan off, while 1 - 9 will generate a proportional PWM to drive the fan, with 1 being the lowest and 9 being the highest.#include <Arduino.h> void setup() { Serial.begin(9600); pinMode(D1, OUTPUT); } void loop() { if (Serial.available() > 0) { char cmd = Serial.read(); int level = (int)(cmd - '0'); if (level < 0 || level > 9) level = 0; analogWrite(D1, level/9.0*255); } delay(100); }
CoreELEC:~ # lsusb Bus 002 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub Bus 001 Device 004: ID 10c4:ea60 Silicon Labs CP210x UART Bridge Bus 001 Device 003: ID 1915:af11 Nordic Semiconductor ASA Wireless Receiver Bus 001 Device 002: ID 1a40:0101 Terminus Technology Inc. Hub Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
CoreELEC:~ # setserial /dev/ttyUSB0
setserial: can't get serial info: Inappropriate ioctl for device
CoreELEC:~ # installentware ... ... Would you like to reboot now to finish installation (recommended) [y/N]? y
After reboot, we can install the stty package:
CoreELEC:~ # opkg install coreutils-stty Installing coreutils-stty (9.1-1) to root... Downloading http://bin.entware.net/aarch64-k3.10/coreutils-stty_9.1-1_aarch64-3.10.ipk Installing coreutils (9.1-1) to root... Downloading http://bin.entware.net/aarch64-k3.10/coreutils_9.1-1_aarch64-3.10.ipk Configuring coreutils. Configuring coreutils-stty.
Then I created a shell script in the /storage folder called cpufan.sh:
#!/usr/bin/sh $(stty -F /dev/ttyUSB0 9600 cs8 -cstopb -parenb) val=$(/usr/bin/cputemp | /usr/bin/sed 's/[^0-9]*//g') if [ $val -ge 55 ] then echo -n 5 > /dev/ttyUSB0 else echo -n 0 > /dev/ttyUSB0 fi
At the moment, the script is very simple. It configures the serial port to 9600/8N1, and retrieves the CPU temperature using cputemp. If the CPU temperature is >= 55c, it sets the fan speed to 5. Otherwise, it turns it off.
Remember to make the script executable by using:
chmod a+x cpufan.sh
Finally, I run the script every minute by adding it to crontab:
*/1 * * * * /storage/cpufan.sh
This command is very helpful to check if the script is being run by cron, and whether there are any execution errors:
systemctl status cron
This command is useful for stressing the CPU quickly and raising its temperature:
stress-ng --matrix 0 -t 5m
Very good job : I will test this script soon.
ReplyDelete