Hello smart people
I'm working on a small PC fan controller project on ESP32. Here's the setup. I have 4 fan channels with 3 pins each.
- PWR/PWM pin, for DC control and voltage regulation for 3 pin computer fan.
- PWM pin for actual PWM control for 4 pin fans. The PWR pin in this case is set to max always
- Read pin for reading of rising edge of fan hall sensor. Attached as an interrupt.
Here's what I want. When the PC powers on it also turns on the ESP32 controller that when it starts sets all the variables it needs and then starts the following function:
void fansetup(){
//Set the pulses to 0
FANBUS_1_NB_PULSE=0;
FANBUS_2_NB_PULSE=0;
FANBUS_3_NB_PULSE=0;
FANBUS_4_NB_PULSE=0;
//Write max speed to all the fan buses
analogWrite(FANBUS_1_PWM,MAX_SPEED);
analogWrite(FANBUS_2_PWM,MAX_SPEED);
analogWrite(FANBUS_3_PWM,MAX_SPEED);
analogWrite(FANBUS_4_PWM,MAX_SPEED);
analogWrite(FANBUS_1_PWR,MAX_SPEED);
analogWrite(FANBUS_2_PWR,MAX_SPEED);
analogWrite(FANBUS_3_PWR,MAX_SPEED);
analogWrite(FANBUS_4_PWR,MAX_SPEED);
//Wait a bit for the fans to spin up
delay(SPINUP_TIME);
//Read the fan speeds
sei();
delay(DELAY_TIME);
cli();
//Check if NB of pulses is greater than 0 and calculate the rpm for each fan
if(FANBUS_1_NB_PULSE =! 0){
FAN_1=1;
int FAN_1_SPEED_1=speedcalculation(FANBUS_1_NB_PULSE , FAN_1_HALL_NUMBER);
}
if(FANBUS_2_NB_PULSE =! 0){
FAN_2=1;
int FAN_2_SPEED_1=speedcalculation(FANBUS_2_NB_PULSE , FAN_2_HALL_NUMBER);
}
if(FANBUS_3_NB_PULSE =! 0){
FAN_3=1;
int FAN_3_SPEED_1=speedcalculation(FANBUS_3_NB_PULSE , FAN_3_HALL_NUMBER);
}
if(FANBUS_4_NB_PULSE =! 0){
FAN_4=1;
int FAN_4_SPEED_1=speedcalculation(FANBUS_4_NB_PULSE , FAN_4_HALL_NUMBER);
}
//Write to the PWM controler 50%
analogWrite(FANBUS_1_PWM,HALF_SPEED);
analogWrite(FANBUS_2_PWM,HALF_SPEED);
analogWrite(FANBUS_3_PWM,HALF_SPEED);
analogWrite(FANBUS_4_PWM,HALF_SPEED);
//Wait for the RPM to stabilise
delay(SPINUP_TIME);
//Start listening to interrupts
sei();
delay(DELAY_TIME);
cli();
//Read the RPM from the second run
if(FAN_1 =! 0){
int FAN_1_SPEED_2=speedcalculation(FANBUS_1_NB_PULSE , FAN_1_HALL_NUMBER);
}
if(FAN_2 =! 0){
int FAN_2_SPEED_2=speedcalculation(FANBUS_2_NB_PULSE , FAN_2_HALL_NUMBER);
}
if(FAN_3 =! 0){
int FAN_3_SPEED_2=speedcalculation(FANBUS_3_NB_PULSE , FAN_3_HALL_NUMBER);
}
if(FAN_4 =! 0){
int FAN_4_SPEED_2=speedcalculation(FANBUS_4_NB_PULSE , FAN_4_HALL_NUMBER);
}
}
What I want to achieve is to be able to check if the fans are alive and if they are, are they PWM controllable and to make some kind of definition so I don't have to run whole blocks of code for controlling fans that aren't connected. This would be achieved by having the controller check the speed after reducing the PWM to half and checking if the fan speed now is below a certain reasonable RPM to make sure that they are PWM controllable.
Something like:
#DEFINE FAN1BUS 1
#ifdef FAN1BUS 1
analogWrite(FANBUS_1_PWR,MAX_SPEED);
#endif
#DEFINE FAN1BUS 2
#ifdef FAN1BUS 2
analogWrite(FANBUS_1_PWM,MAX_SPEED);
#endif
But have this kind of thing during runtime. This reduces code size the controller has to go through considerably.
Any help would be great. ![]()