How to fix fan toggling issue in temperature control system?

So guys there's a piece of code, that involves temperature measurement. If the temperature is more than a certain value, a fan comes on, else it goes off. The sensor used is an NTC sensor, hooked up to a heat sink. This is the line of code: if (temperature > 30) { FANON; } else { FANOFF; } the bug I have observed is that once the temperature gets to 30 degree the fan comes on, after some few seconds, the Temperature drops, the fan goes off, few seconds again the fan comes on just like that, which is not good for the system, I love to know how we would address the issue, in a simple way @here
1 Reply
ebariaux
ebariaux2w ago
Use a hysteresis function, with 2 thresholds Something like if temp > 30 { fan on } if temp < 25 { fan off } temp between 25 and 30 will not change the state of the system

Did you find this page helpful?