Open StuartIanNaylor opened 1 year ago
Also the 30mm 3pin fan I got from PiHut seems to stall under 20% which might be the PWM rate is to slow and not the mark/space but not sure how to set that. I think fans usually run at 20 or 25 Khz normally just over hearing for any coil whine. So anyway I edited to just add 0 than min pwm but still have a min/max pwm so off below min temp but ramps up quicker.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <wiringPi.h>
#include <softPwm.h>
// Define
#define FAN_PIN 3
#define MIN_TEMP 40
#define MAX_TEMP 75
#define CPU_TEMP_FILE "/sys/class/thermal/thermal_zone0/temp"
#define MIN_PWM 25
#define MAX_PWM 100
int main() {
int temp;
float temp_ratio, pwm_ratio;
int pwm_value = 0;
if (wiringPiSetup() == -1) {
printf("wiringPiSetup failed\n");
return 1;
}
pinMode(FAN_PIN, OUTPUT);
softPwmCreate(FAN_PIN, MIN_PWM, MAX_PWM);
while(1) {
FILE* temp_file = fopen(CPU_TEMP_FILE, "r");
if (temp_file == NULL) {
printf("Failed to read CPU temperature file\n");
return 1;
}
fscanf(temp_file, "%d", &temp);
fclose(temp_file);
temp /= 1000;
if (temp < MIN_TEMP) {
pwm_value = 0;
} else if (temp > MAX_TEMP) {
pwm_value = MAX_PWM;
} else {
temp_ratio = (float)(temp - MIN_TEMP) / (MAX_TEMP - MIN_TEMP);
pwm_ratio = temp_ratio * (MAX_PWM - MIN_PWM);
pwm_value = (int)(pwm_ratio + MIN_PWM);
}
softPwmWrite(FAN_PIN, pwm_value);
printf("CPU: %d°C | PWM: %d%%\n", temp, pwm_value);
sleep(5);
}
@StuartIanNaylor my blabla fan starts from 50% as it is not 3 pin fan - just 2 pins ;-) but still better 50%-100% than 0% or 100%
@StuartIanNaylor my blabla fan starts from 50% as it is not 3 pin fan - just 2 pins ;-) but still better 50%-100% than 0% or 100%
Yeah 2 pin fans are not just DC motors as they would be perfect for PWM but likely have a built-in circuit, 3 pin usually are voltage controlled with a tacho. The 3pin GPIO fans are PWM 3.3v input control and so do work correct but likely not at 25Khz which you would prob have to dedicate a PWM to that GPIO and set the base frquency.
I cloned the v0.2 version and seemed to install ok
Any idea on what the errors are as all new to me.
After a Google I tried
gcc -o fan-control fan-control.c -lwiringPi -lcrypt -lm
and worked