PWM fan control

Post Reply
marecki_l
Posts: 58
Joined: Sun May 29, 2016 7:27 pm
languages_spoken: Polish, English
ODROIDs: Odroid C2
Has thanked: 4 times
Been thanked: 0
Contact:

PWM fan control

Post by marecki_l »

Hiello. I would like to control CPU fan with pwm depending on temperature. I am running DietPi on my board and it seems that this system doesn't support hardware pwm. So idea is to use software pwm using one of GPIO's doesn't matter which. I do have NPN transistor key to switch fan on/of right now base of transistor is connested to 5V trough 1 kohm resistor so it runs constantly and even when not needed. I am not advanced enough to make such program myself, so if anybody can help that will be very appreciated.

User avatar
tobetter
Posts: 11362
Joined: Mon Feb 25, 2013 10:55 am
languages_spoken: Korean, English
ODROIDs: Many
Location: Paju, South Korea
Has thanked: 777 times
Been thanked: 1930 times
Contact:

Re: PWM fan control

Post by tobetter »

Isn't DietPi is based on Armbian? What's the kernel version name, output of uname -a?

marecki_l
Posts: 58
Joined: Sun May 29, 2016 7:27 pm
languages_spoken: Polish, English
ODROIDs: Odroid C2
Has thanked: 4 times
Been thanked: 0
Contact:

Re: PWM fan control

Post by marecki_l »

Yes it is Armbian kernel:

root@DietPi:~# uname -a
Linux DietPi 5.10.123-meson64 #22.05.3 SMP PREEMPT Wed Jun 22 07:23:04 UTC 2022 aarch64 GNU/Linux

User avatar
mctom
Posts: 2574
Joined: Wed Nov 11, 2020 4:44 am
languages_spoken: english, polish
ODROIDs: OGA, XU4, C2, M1, H3+, SP3, Vu8M
Location: Gdansk, Poland
Has thanked: 347 times
Been thanked: 441 times
Contact:

Re: PWM fan control

Post by mctom »

Unfortunately I do not own Odroid C2 at the moment, so I hope you will be comfortable with testing on your own.

1k resistor on the npn transistor base sounds like a small value, but should work. Perhaps 2k or 3k3 would be safer. What kind of transistor is it, and what's the fan rating (voltage, current)?

About the GPIO, let's see if we can do a Bash script first for a test.
Please install "gpiod" package and try command "sudo gpioinfo", and paste the full result here.
Punk ain't no religious cult, punk means thinking for yourself!

Maintainer of PiStackMon

marecki_l
Posts: 58
Joined: Sun May 29, 2016 7:27 pm
languages_spoken: Polish, English
ODROIDs: Odroid C2
Has thanked: 4 times
Been thanked: 0
Contact:

Re: PWM fan control

Post by marecki_l »

Transistor BD175, Fan 5V 0.5A

Code: Select all

root@DietPi:~# gpioinfo
gpiochip0 - 15 lines:
        line   0:    "UART TX"       unused   input  active-high
        line   1:    "UART RX"       unused   input  active-high
        line   2:    "VCCK En"       unused   input  active-high
        line   3: "TF 3V3/1V8 En" "TF_IO" output active-high [used]
        line   4: "USB HUB nRESET" "usb-hub-reset" output active-high [used]
        line   5: "USB OTG Power En" "regulator-usb-pwrs" output active-high [us                                                                                                                                                             ed]
        line   6: "J7 Header Pin2" unused input active-high
        line   7:      "IR In"       unused   input  active-high
        line   8: "J7 Header Pin4" unused input active-high
        line   9: "J7 Header Pin6" unused input active-high
        line  10: "J7 Header Pin5" unused input active-high
        line  11: "J7 Header Pin7" unused input active-high
        line  12:   "HDMI CEC"       unused   input  active-high
        line  13:    "SYS LED" "c2:blue:alive" output active-low [used]
        line  14:      unnamed       unused   input  active-high
gpiochip1 - 119 lines:
        line   0:   "Eth MDIO"       unused   input  active-high
        line   1:    "Eth MDC"       unused   input  active-high
        line   2: "Eth RGMII RX Clk" unused input active-high
        line   3:  "Eth RX DV"       unused   input  active-high
        line   4:  "Eth RX D0"       unused   input  active-high
        line   5:  "Eth RX D1"       unused   input  active-high
        line   6:  "Eth RX D2"       unused   input  active-high
        line   7:  "Eth RX D3"       unused   input  active-high
        line   8: "Eth RGMII TX Clk" unused input active-high
        line   9:  "Eth TX En"       unused   input  active-high
        line  10:  "Eth TX D0"       unused   input  active-high
        line  11:  "Eth TX D1"       unused   input  active-high
        line  12:  "Eth TX D2"       unused   input  active-high
        line  13:  "Eth TX D3"       unused   input  active-high
        line  14: "Eth PHY nRESET" "PHY reset" output active-low [used]
        line  15: "Eth PHY Intc" unused input active-high
        line  16:   "HDMI HPD"       unused   input  active-high
        line  17: "HDMI DDC SDA" unused input active-high
        line  18: "HDMI DDC SCL" unused input active-high
        line  19:      unnamed       unused   input  active-high
        line  20:    "eMMC D0"       unused   input  active-high
        line  21:    "eMMC D1"       unused   input  active-high
        line  22:    "eMMC D2"       unused   input  active-high
        line  23:    "eMMC D3"       unused   input  active-high
        line  24:    "eMMC D4"       unused   input  active-high
        line  25:    "eMMC D5"       unused   input  active-high
        line  26:    "eMMC D6"       unused   input  active-high
        line  27:    "eMMC D7"       unused   input  active-high
        line  28:   "eMMC Clk"       unused   input  active-high
        line  29: "eMMC Reset"      "reset"  output   active-low [used]
        line  30:   "eMMC CMD"       unused   input  active-high
        line  31:      unnamed       unused   input  active-high
        line  32:      unnamed       unused   input  active-high
        line  33:      unnamed       unused   input  active-high
        line  34:      unnamed       unused   input  active-high
        line  35:      unnamed       unused   input  active-high
        line  36:      unnamed       unused   input  active-high
        line  37:      unnamed       unused   input  active-high
        line  38:  "SDCard D1"       unused   input  active-high
        line  39:  "SDCard D0"       unused   input  active-high
        line  40: "SDCard CLK"       unused   input  active-high
        line  41: "SDCard CMD"       unused   input  active-high
        line  42:  "SDCard D3"       unused   input  active-high
        line  43:  "SDCard D2"       unused   input  active-high
        line  44: "SDCard Det"         "cd"   input   active-low [used]
        line  45:      unnamed       unused   input  active-high
        line  46:      unnamed       unused   input  active-high
        line  47:      unnamed       unused   input  active-high
        line  48:      unnamed       unused   input  active-high
        line  49:      unnamed       unused   input  active-high
        line  50:      unnamed       unused   input  active-high
        line  51:      unnamed       unused   input  active-high
        line  52:      unnamed       unused   input  active-high
        line  53:      unnamed       unused   input  active-high
        line  54:      unnamed       unused   input  active-high
        line  55:      unnamed       unused   input  active-high
        line  56:      unnamed       unused   input  active-high
        line  57:      unnamed       unused   input  active-high
        line  58:      unnamed       unused   input  active-high
        line  59:      unnamed       unused   input  active-high
        line  60:      unnamed       unused   input  active-high
        line  61:      unnamed       unused   input  active-high
        line  62:      unnamed       unused   input  active-high
        line  63:      unnamed       unused   input  active-high
        line  64:      unnamed       unused   input  active-high
        line  65:      unnamed       unused   input  active-high
        line  66:      unnamed       unused   input  active-high
        line  67:      unnamed       unused   input  active-high
        line  68:      unnamed       unused   input  active-high
        line  69:  "I2C A SDA"       unused   input  active-high
        line  70:  "I2C A SCK"       unused   input  active-high
        line  71:  "I2C B SDA"       unused   input  active-high
        line  72:  "I2C B SCK"       unused   input  active-high
        line  73:      "PWM D"       unused   input  active-high
        line  74:      "PWM B"       unused   input  active-high
        line  75: "Revision Bit0" unused input active-high
        line  76: "Revision Bit1" unused input active-high
        line  77:      unnamed       unused   input  active-high
        line  78: "J2 Header Pin35" unused input active-high
        line  79:      unnamed       unused   input  active-high
        line  80:      unnamed       unused   input  active-high
        line  81:      unnamed       unused   input  active-high
        line  82: "J2 Header Pin36" unused input active-high
        line  83: "J2 Header Pin31" unused input active-high
        line  84:      unnamed       unused   input  active-high
        line  85:      unnamed       unused   input  active-high
        line  86:      unnamed       unused   input  active-high
        line  87:  "TF VDD En" "regulator-tflash_vdd" output active-high [used]
        line  88: "J2 Header Pin32" unused input active-high
        line  89: "J2 Header Pin26" unused input active-high
        line  90:      unnamed       unused   input  active-high
        line  91:      unnamed       unused   input  active-high
        line  92: "J2 Header Pin29" unused input active-high
        line  93: "J2 Header Pin24" unused input active-high
        line  94: "J2 Header Pin23" unused input active-high
        line  95: "J2 Header Pin22" unused input active-high
        line  96: "J2 Header Pin21" unused input active-high
        line  97: "J2 Header Pin18" unused input active-high
        line  98: "J2 Header Pin33" unused input active-high
        line  99: "J2 Header Pin19" unused input active-high
        line 100: "J2 Header Pin16" unused input active-high
        line 101: "J2 Header Pin15" unused input active-high
        line 102: "J2 Header Pin12" unused input active-high
        line 103: "J2 Header Pin13" unused input active-high
        line 104: "J2 Header Pin8" unused input active-high
        line 105: "J2 Header Pin10" unused input active-high
        line 106:      unnamed       unused   input  active-high
        line 107:      unnamed       unused   input  active-high
        line 108:      unnamed       unused   input  active-high
        line 109:      unnamed       unused   input  active-high
        line 110:      unnamed       unused   input  active-high
        line 111: "J2 Header Pin11" unused input active-high
        line 112:      unnamed       unused   input  active-high
        line 113: "J2 Header Pin7" unused input active-high
        line 114:      unnamed       unused   input  active-high
        line 115:      unnamed       unused   input  active-high
        line 116:      unnamed       unused   input  active-high
        line 117:      unnamed       unused   input  active-high
        line 118:      unnamed       unused   input  active-high

User avatar
joerg
Posts: 1752
Joined: Tue Apr 01, 2014 2:14 am
languages_spoken: german, english, español
ODROIDs: C1, C1+, C2, N1, N2, C4
Location: Germany
Has thanked: 157 times
Been thanked: 341 times
Contact:

Re: PWM fan control

Post by joerg »

I think the BD175 is a little big for it?
But anyway, if it is already working as on/off I would say the resistor must be even smaller (560) to drive about 5mA to the base. Calculating with about hfe of 100. But more then 5mA I would not drive with a GPIO pin.
About PWM: what lists /sys/class/pwm/?
Maybe there is already hardware PWM enabled. Then it needs to be initialized and controlled as on wiki page. But the frequency must not be very big for this configuration with BD175. About 1kHz, I would say.

marecki_l
Posts: 58
Joined: Sun May 29, 2016 7:27 pm
languages_spoken: Polish, English
ODROIDs: Odroid C2
Has thanked: 4 times
Been thanked: 0
Contact:

Re: PWM fan control

Post by marecki_l »

When I will have working pwm I can ajust/change transistor and resistor for better ones.
/sys/class/pwm/ is empty

User avatar
mctom
Posts: 2574
Joined: Wed Nov 11, 2020 4:44 am
languages_spoken: english, polish
ODROIDs: OGA, XU4, C2, M1, H3+, SP3, Vu8M
Location: Gdansk, Poland
Has thanked: 347 times
Been thanked: 441 times
Contact:

Re: PWM fan control

Post by mctom »

Yes, the transistor is mismatched. Have you got any other npn or pnp transistors? I assume you prefer through-hole components for easy soldering. In that case, perhaps building a Darlington or Sziklai pair would be more reasonable.

Hardware PWM module would be perfect, but if it doesn't work, we shall continue exploring soft PWM possibility.
Anyway, here are the commands to set GPIO Pin 7 into low and high state:

Code: Select all

gpioset gpiochip1 113=0
gpioset gpiochip1 113=1
If this works for controlling a fan, then we'll see if looping it in Bash will produce a bearable PWM effect.

By the way, please use "code" formatting for long outputs from the terminal. :)
Last edited by mctom on Thu Aug 25, 2022 3:37 am, edited 1 time in total.
Punk ain't no religious cult, punk means thinking for yourself!

Maintainer of PiStackMon

marecki_l
Posts: 58
Joined: Sun May 29, 2016 7:27 pm
languages_spoken: Polish, English
ODROIDs: Odroid C2
Has thanked: 4 times
Been thanked: 0
Contact:

Re: PWM fan control

Post by marecki_l »

Well If I am doing it well I've connected my transistor base to PIN 7 (circled one) and nothing happened, when I measure voltage on it it shows 0.5V on both stages.
So basically it didn't toggle pin level

Image
Attachments
ODROID-C2_J2_2.PNG
ODROID-C2_J2_2.PNG (128.1 KiB) Viewed 2025 times

marecki_l
Posts: 58
Joined: Sun May 29, 2016 7:27 pm
languages_spoken: Polish, English
ODROIDs: Odroid C2
Has thanked: 4 times
Been thanked: 0
Contact:

Re: PWM fan control

Post by marecki_l »

But I did test with Pin 33 (gpiochip1 98) and it does something fan didn't start because wrong transistor key but it moved a bit when I set pin to high.
What transistor would be best?

marecki_l
Posts: 58
Joined: Sun May 29, 2016 7:27 pm
languages_spoken: Polish, English
ODROIDs: Odroid C2
Has thanked: 4 times
Been thanked: 0
Contact:

Re: PWM fan control

Post by marecki_l »

So I have changed resistor to 500 ohm and now I am able to turn on/off my fan

Code: Select all

gpioset gpiochip1 98=0
gpioset gpiochip1 98=1

marecki_l
Posts: 58
Joined: Sun May 29, 2016 7:27 pm
languages_spoken: Polish, English
ODROIDs: Odroid C2
Has thanked: 4 times
Been thanked: 0
Contact:

Re: PWM fan control

Post by marecki_l »

I do have transistors: C1213A, BC107, c558B, pn2222, JC337, bc211,bc313,bc108, and some power transistors. if non of them is suitable for this case, let me know what should I buy, please.

User avatar
mctom
Posts: 2574
Joined: Wed Nov 11, 2020 4:44 am
languages_spoken: english, polish
ODROIDs: OGA, XU4, C2, M1, H3+, SP3, Vu8M
Location: Gdansk, Poland
Has thanked: 347 times
Been thanked: 441 times
Contact:

Re: PWM fan control

Post by mctom »

Yes, my bad, I gave you a wrong command (for Pin 7, but on another header J7). I edited my last post so it contains correct commands now.

About the transistor, it's hard to find a good match in through hole package. Modern transistors (with much better specs) are tiny.
One proven solution is right on ODROID XU4 schematic.
Note this particular transistor model has base resistor built-in.
https://www.nexperia.com/products/bipol ... 123YT.html
2022-08-24-210957_706x477_scrot.png
2022-08-24-210957_706x477_scrot.png (28.82 KiB) Viewed 2003 times
I see you even have a few transistors from CEMI... :shock:

None of these compares to modern transistors, but you could try using two JC337 in Darlngton Pair configuation, to compensate for its low hfe.
ojx06.jpg
ojx06.jpg (8.66 KiB) Viewed 2003 times
The resistor could be 1k or maybe less.
Punk ain't no religious cult, punk means thinking for yourself!

Maintainer of PiStackMon

marecki_l
Posts: 58
Joined: Sun May 29, 2016 7:27 pm
languages_spoken: Polish, English
ODROIDs: Odroid C2
Has thanked: 4 times
Been thanked: 0
Contact:

Re: PWM fan control

Post by marecki_l »

Yes I am old prick :) and have some stock from old "good" times.
But I can say those commands works fine.

User avatar
mctom
Posts: 2574
Joined: Wed Nov 11, 2020 4:44 am
languages_spoken: english, polish
ODROIDs: OGA, XU4, C2, M1, H3+, SP3, Vu8M
Location: Gdansk, Poland
Has thanked: 347 times
Been thanked: 441 times
Contact:

Re: PWM fan control

Post by mctom »

Now, I have created a super simple bash script, that should toggle a GPIO at 10Hz, and work for 10s (or 100 loops, to be precise).
I have no idea how this will perform with a fan, and with gpioset overhead. You're going to be a pioneer of this approach!

If this test gives bad results, I'll just make C++ code and get it over with.

Code: Select all

#!/bin/bash

for i in {0..100};
do
	sleep 0.05
	gpioset gpiochip1 98=1
	sleep 0.05
	gpioset gpiochip1 98=0
done
show us the execution time: time ./test.sh, or however you call this file :)
Punk ain't no religious cult, punk means thinking for yourself!

Maintainer of PiStackMon

marecki_l
Posts: 58
Joined: Sun May 29, 2016 7:27 pm
languages_spoken: Polish, English
ODROIDs: Odroid C2
Has thanked: 4 times
Been thanked: 0
Contact:

Re: PWM fan control

Post by marecki_l »

So I exchanged transistor to Darlington pair and tested Bash script and it works somehow. I can observe some kind of waving on fan rotation.

Code: Select all

real    0m12.291s
user    0m0.648s
sys     0m1.596s

User avatar
mctom
Posts: 2574
Joined: Wed Nov 11, 2020 4:44 am
languages_spoken: english, polish
ODROIDs: OGA, XU4, C2, M1, H3+, SP3, Vu8M
Location: Gdansk, Poland
Has thanked: 347 times
Been thanked: 441 times
Contact:

Re: PWM fan control

Post by mctom »

But at least your Darlington pair seems to work fine. Is it getting hot? If it doesn't get noticeably warm then I think it could stay the way it is.

Perhaps 10Hz isn't quite enough to drive a fan. :D
And the method isn't the most efficient one, seems like it uses some 18% of single core CPU time to execute.

Here's a C code that should do the same. you'll need to install gcc and libgpiod-dev to build it.
Except now it should work at 1000Hz for 10s. Try timing this program just like before. :)

Code: Select all

#include <gpiod.h>
#include <unistd.h>

int main(int argc, char **argv)
{
  const char *chipname = "gpiochip1";
  struct gpiod_chip *chip;
  struct gpiod_line *fan;
  
  chip = gpiod_chip_open_by_name(chipname);
  fan = gpiod_chip_get_line(chip, 98);
  
  gpiod_line_request_output(fan, "PWM Fan", 0);
  
  for (int i = 0; i < 10000; i++) {
    usleep(500);
    gpiod_line_set_value(fan, 1);
    usleep(500);
    gpiod_line_set_value(fan, 0);
  }
  
  gpiod_line_release(fan);
  gpiod_chip_close(chip);
  
  return 0;
}

save this code as test.c, and build it with:
gcc test.c -lgpiod -o test
It will produce an executable file test.

If it works fine, we'll add a CPU temperature monitoring on top of it, and we'll be done. :)
See if this command works for you to read the CPU temperature:
cat /sys/devices/virtual/thermal/thermal_zone0/temp
Punk ain't no religious cult, punk means thinking for yourself!

Maintainer of PiStackMon

marecki_l
Posts: 58
Joined: Sun May 29, 2016 7:27 pm
languages_spoken: Polish, English
ODROIDs: Odroid C2
Has thanked: 4 times
Been thanked: 0
Contact:

Re: PWM fan control

Post by marecki_l »

Ok compiled, tested, works, I can read temperature.

User avatar
mctom
Posts: 2574
Joined: Wed Nov 11, 2020 4:44 am
languages_spoken: english, polish
ODROIDs: OGA, XU4, C2, M1, H3+, SP3, Vu8M
Location: Gdansk, Poland
Has thanked: 347 times
Been thanked: 441 times
Contact:

Re: PWM fan control

Post by mctom »

Excellent! And how's the result of time ./test?

I'm finishing a full program for your needs. Gonna take me sometime, but should be ready today.
These users thanked the author mctom for the post:
marecki_l (Thu Aug 25, 2022 11:34 pm)
Punk ain't no religious cult, punk means thinking for yourself!

Maintainer of PiStackMon

marecki_l
Posts: 58
Joined: Sun May 29, 2016 7:27 pm
languages_spoken: Polish, English
ODROIDs: Odroid C2
Has thanked: 4 times
Been thanked: 0
Contact:

Re: PWM fan control

Post by marecki_l »

Code: Select all

real    0m14.229s
user    0m0.256s
sys     0m1.485s

User avatar
mctom
Posts: 2574
Joined: Wed Nov 11, 2020 4:44 am
languages_spoken: english, polish
ODROIDs: OGA, XU4, C2, M1, H3+, SP3, Vu8M
Location: Gdansk, Poland
Has thanked: 347 times
Been thanked: 441 times
Contact:

Re: PWM fan control

Post by mctom »

Well, that's an improvement, some 12% of CPU time, compared to 18% before.

I'll give it a brief test on my M1.
Punk ain't no religious cult, punk means thinking for yourself!

Maintainer of PiStackMon

User avatar
mctom
Posts: 2574
Joined: Wed Nov 11, 2020 4:44 am
languages_spoken: english, polish
ODROIDs: OGA, XU4, C2, M1, H3+, SP3, Vu8M
Location: Gdansk, Poland
Has thanked: 347 times
Been thanked: 441 times
Contact:

Re: PWM fan control

Post by mctom »

Okay, the daemon is ready. :D

Code: Select all

git clone https://github.com/tomek-szczesny/gpio-fan-controller.git
cd gpio-fan-controller
at this point, open gpio-fan-controller.cpp and inspect first 30 lines (Configuration constants), especially if you changed the GPIO, or want a different target temperature.
This daemon will gradually change the PWM so the CPU temperature stays at target temperature. Not exactly a PID control, but should work just fine. :)
Then:

Code: Select all

sudo apt install make
make build
sudo make install
From now on, it should just work. It will be installed as a service, and will work after reboots too.

My measured CPU consumption across 3 minutes was ~1%. It's the frequency that matters, so I lowered it to 100Hz. :)

EDIT: You can use stress -c 4 to heat up your CPU and check if it's working. :)
EDIT: you may also need to install git, make and g++. I switched the language to C++ at some point. :)
These users thanked the author mctom for the post:
marecki_l (Fri Aug 26, 2022 1:00 am)
Punk ain't no religious cult, punk means thinking for yourself!

Maintainer of PiStackMon

marecki_l
Posts: 58
Joined: Sun May 29, 2016 7:27 pm
languages_spoken: Polish, English
ODROIDs: Odroid C2
Has thanked: 4 times
Been thanked: 0
Contact:

Re: PWM fan control

Post by marecki_l »

Great thanks, installed, CPU consumption around 1%. I will make stress test later today and I'll let you know how it works. Ince again you are great.

User avatar
mctom
Posts: 2574
Joined: Wed Nov 11, 2020 4:44 am
languages_spoken: english, polish
ODROIDs: OGA, XU4, C2, M1, H3+, SP3, Vu8M
Location: Gdansk, Poland
Has thanked: 347 times
Been thanked: 441 times
Contact:

Re: PWM fan control

Post by mctom »

I'm glad it works. :)
Punk ain't no religious cult, punk means thinking for yourself!

Maintainer of PiStackMon

marecki_l
Posts: 58
Joined: Sun May 29, 2016 7:27 pm
languages_spoken: Polish, English
ODROIDs: Odroid C2
Has thanked: 4 times
Been thanked: 0
Contact:

Re: PWM fan control

Post by marecki_l »

Well I couldn't wait :) tested it on stress, works perfectly, once again great thanks.

User avatar
mctom
Posts: 2574
Joined: Wed Nov 11, 2020 4:44 am
languages_spoken: english, polish
ODROIDs: OGA, XU4, C2, M1, H3+, SP3, Vu8M
Location: Gdansk, Poland
Has thanked: 347 times
Been thanked: 441 times
Contact:

Re: PWM fan control

Post by mctom »

So, for the future generations, I made a new open source project, lol

https://github.com/tomek-szczesny/gpio-fan-controller
These users thanked the author mctom for the post (total 2):
marecki_l (Fri Aug 26, 2022 5:04 am) • odroid (Fri Aug 26, 2022 9:00 am)
Punk ain't no religious cult, punk means thinking for yourself!

Maintainer of PiStackMon

User avatar
rooted
Posts: 9968
Joined: Fri Dec 19, 2014 9:12 am
languages_spoken: english
Location: Gulf of Mexico, US
Has thanked: 777 times
Been thanked: 580 times
Contact:

Re: PWM fan control

Post by rooted »

Nicely commented which is always useful for anyone who isn't a coder.

marecki_l
Posts: 58
Joined: Sun May 29, 2016 7:27 pm
languages_spoken: Polish, English
ODROIDs: Odroid C2
Has thanked: 4 times
Been thanked: 0
Contact:

Re: PWM fan control

Post by marecki_l »

I'm baack ;) What about to add fan speed reading to this program?

User avatar
mctom
Posts: 2574
Joined: Wed Nov 11, 2020 4:44 am
languages_spoken: english, polish
ODROIDs: OGA, XU4, C2, M1, H3+, SP3, Vu8M
Location: Gdansk, Poland
Has thanked: 347 times
Been thanked: 441 times
Contact:

Re: PWM fan control

Post by mctom »

rooted wrote:
Fri Aug 26, 2022 8:58 am
Nicely commented which is always useful for anyone who isn't a coder.
I appreciate well commented code, so that's what I'm doing. After all, I often point people to my code when they struggle with GPIO. :)
marecki_l wrote:
Fri Aug 26, 2022 2:57 pm
I'm baack ;) What about to add fan speed reading to this program?
EDIT:
Okay, I misunderstood you. What you want is reading a tacho signal from a fan? But that's a separate program then. :)
Punk ain't no religious cult, punk means thinking for yourself!

Maintainer of PiStackMon

marecki_l
Posts: 58
Joined: Sun May 29, 2016 7:27 pm
languages_spoken: Polish, English
ODROIDs: Odroid C2
Has thanked: 4 times
Been thanked: 0
Contact:

Re: PWM fan control

Post by marecki_l »

Yes exactly

User avatar
mctom
Posts: 2574
Joined: Wed Nov 11, 2020 4:44 am
languages_spoken: english, polish
ODROIDs: OGA, XU4, C2, M1, H3+, SP3, Vu8M
Location: Gdansk, Poland
Has thanked: 347 times
Been thanked: 441 times
Contact:

Re: PWM fan control

Post by mctom »

Quick math says this is not the best idea..

Let's say the maximum fan speed is some 4000 RPM, that's 15ms pulse duration together with a space between pulses. If the duty cycle is, say, 10%, that's 1.5ms pulse that needs to be detected reliably.
libgpiod leaves us with no other choice than polling (reading a pin value repeatedly), and we've already seen what happens to CPU usage when you manipulate a pin at 1000Hz.
These users thanked the author mctom for the post:
marecki_l (Fri Aug 26, 2022 4:27 pm)
Punk ain't no religious cult, punk means thinking for yourself!

Maintainer of PiStackMon

marecki_l
Posts: 58
Joined: Sun May 29, 2016 7:27 pm
languages_spoken: Polish, English
ODROIDs: Odroid C2
Has thanked: 4 times
Been thanked: 0
Contact:

Re: PWM fan control

Post by marecki_l »

Ok, got it. It was just an idea.

User avatar
MichaIng
Posts: 78
Joined: Wed Apr 10, 2019 4:44 am
languages_spoken: english german
Has thanked: 15 times
Been thanked: 21 times
Contact:

Re: PWM fan control

Post by MichaIng »

Twonotes from my end:
  • We started working on a fan control script a long time ago, but stopped shortly after since the large number of different sysfs APIs wasn't maintainable for us. However, the script was working on Odroid C2 that time, though we used the vendor kernel then, now Armbian kernel. However, still worth to give it a try: https://github.com/MichaIng/DietPi/blob ... an_control
  • In the meantime, especially with mainline/Armbian kernel, one might try to use the generic fancontrol package:

    Code: Select all

    apt install fancontrol lm-sensors

User avatar
mctom
Posts: 2574
Joined: Wed Nov 11, 2020 4:44 am
languages_spoken: english, polish
ODROIDs: OGA, XU4, C2, M1, H3+, SP3, Vu8M
Location: Gdansk, Poland
Has thanked: 347 times
Been thanked: 441 times
Contact:

Re: PWM fan control

Post by mctom »

Wasn't aware of that package, quick google returned nothing like it when I was searching for a similar solution. :)
Well, fancontrol is a bash script relying on sysfs which is already deprecated for a while now, so one day many fans will stop around the world.

But then again, my project got 1 star on github, so I guess it must be s#!t :D
Punk ain't no religious cult, punk means thinking for yourself!

Maintainer of PiStackMon

pelukessss
Posts: 5
Joined: Fri Jul 19, 2019 5:10 pm
languages_spoken: english, italian
ODROIDs: C2, N2 (soon)
Has thanked: 0
Been thanked: 0
Contact:

Re: PWM fan control

Post by pelukessss »

MichaIng wrote:
Sat Oct 22, 2022 9:21 pm
Twonotes from my end:
  • We started working on a fan control script a long time ago, but stopped shortly after since the large number of different sysfs APIs wasn't maintainable for us. However, the script was working on Odroid C2 that time, though we used the vendor kernel then, now Armbian kernel. However, still worth to give it a try: https://github.com/MichaIng/DietPi/blob ... an_control
  • In the meantime, especially with mainline/Armbian kernel, one might try to use the generic fancontrol package:

    Code: Select all

    apt install fancontrol lm-sensors
Hi Micha, I am running DietPi 8.11 on Odroid C2 and I'm trying to properly set up a 3 pins pwm fan...
This is my "sensors" command output:

Code: Select all

scpi_sensors-isa-0000
Adapter: ISA adapter
aml_thermal:  +42.0°C
And this is pwmconfig command output:

Code: Select all

# pwmconfig version 3.6.0
This program will search your sensors for pulse width modulation (pwm)
controls, and test each one to see if it controls a fan on
your motherboard. Note that many motherboards do not have pwm
circuitry installed, even if your sensor chip supports pwm.

We will attempt to briefly stop each fan using the pwm controls.
The program will attempt to restore each fan to full speed
after testing. However, it is ** very important ** that you
physically verify that the fans have been to full speed
after the program has completed.

/usr/sbin/pwmconfig: There are no pwm-capable sensor modules installed
What should I do? I'm using this kind of fan https://wiki.52pi.com/index.php?title=F-0011 with the blue wire connected to the PWM0 pin https://wiki.odroid.com/odroid-c2/appli ... _odroid-c2

Thank you

User avatar
MichaIng
Posts: 78
Joined: Wed Apr 10, 2019 4:44 am
languages_spoken: english german
Has thanked: 15 times
Been thanked: 21 times
Contact:

Re: PWM fan control

Post by MichaIng »

Code: Select all

/usr/sbin/pwmconfig: There are no pwm-capable sensor modules installed
Seems to not work OOTB with lm-sensors. Calling

Code: Select all

pwmconfig
just fails? It's then probably easier to try with the dietpi-fan_control script I posted above, which makes direkt use of the C2's PWM sysfs API.

pelukessss
Posts: 5
Joined: Fri Jul 19, 2019 5:10 pm
languages_spoken: english, italian
ODROIDs: C2, N2 (soon)
Has thanked: 0
Been thanked: 0
Contact:

Re: PWM fan control

Post by pelukessss »

MichaIng wrote:
Fri Dec 30, 2022 3:04 pm

Code: Select all

/usr/sbin/pwmconfig: There are no pwm-capable sensor modules installed
Seems to not work OOTB with lm-sensors. Calling

Code: Select all

pwmconfig
just fails? It's then probably easier to try with the dietpi-fan_control script I posted above, which makes direkt use of the C2's PWM sysfs API.
Yes, executing "pwmconfig" (I launched it with the command "sudo sh pwmconfig") fails and returns the error message I posted on the previous post... What should I do in order to use your script? Sorry, this is all rather new for me :)
Thank you

User avatar
MichaIng
Posts: 78
Joined: Wed Apr 10, 2019 4:44 am
languages_spoken: english german
Has thanked: 15 times
Been thanked: 21 times
Contact:

Re: PWM fan control

Post by MichaIng »

pelukessss wrote:
Tue Jan 10, 2023 10:05 pm
What should I do in order to use your script? Sorry, this is all rather new for me :)
Thank you
Sorry for the late reply. Try this:

Code: Select all

curl -sSfo '/usr/local/bin/dietpi-fan_control' 'https://raw.githubusercontent.com/MichaIng/DietPi/master/.meta/dietpi-fan_control'
chmod +x /usr/local/bin/dietpi-fan_control
dietpi-fan_control

Post Reply

Return to “Hardware and peripherals”

Who is online

Users browsing this forum: No registered users and 2 guests