Skip to content

Instantly share code, notes, and snippets.

@PyroSA
Last active June 21, 2024 22:45
Show Gist options
  • Save PyroSA/b19e5f2790d5e2805e25ce6b90b69278 to your computer and use it in GitHub Desktop.
Save PyroSA/b19e5f2790d5e2805e25ce6b90b69278 to your computer and use it in GitHub Desktop.
OrangePi5 PWM Fan Control
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <wiringPi.h>
#include <softPwm.h>
#define PIN 10
#define RANGE 100
int getVal (char *file) {
long int l;
FILE *ptr;
ptr = fopen(file, "r");
if (ptr == NULL) {
return 0;
}
fscanf(ptr, "%ld", &l);
fclose(ptr);
return l / 1000;
}
int main (int argc, char *argv[])
{
if (argc < 2) {
printf("Your files containing thermals eg: /sys/class/thermal/*/temp");
return -1;
}
wiringPiSetup ();
softPwmCreate (PIN, 0, RANGE);
int max = 0;
int pmax = 60;
int val = 0;
int i;
while (1==1) {
// Get Max thermal
max = 0;
for (i = 1; i < argc; i++) {
val = getVal(argv[i]);
max = max > val ? max : val;
}
// Adjust current thermal (slow backoff)
pmax = max >= pmax ? max : pmax - 1;
// Calc Fan speed
val = (pmax - 40) * 4; // 40-65
val = val > 100 ? 100 : val;
val = val < 0 ? 0 : val;
// Set Fan
printf("%3dC - %3dC - %3d%%\n", max, pmax, val);
softPwmWrite (PIN, val);
delay(1000);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment