I am trying to make a fan turn on at ~60 deg C and turn off at ~40 deg C to introduce to hysteresis. I am just using a potentiometer to get my logic correct first. I can get it to do this with digitalWrite ON/OFF commands but I would like to ramp up the PWM to 100% when the target temperture is reached and then ramp down to 0% PWM when the fan is no longer required to be on. I'm not sure why the PWM is not ramping up, it seems to be fixed at my pwmStep of 5? Thanks!
type o/*This program reads the thermister input and turns on the fans at ~60 deg C.
When the temperature has dropped below 40 deg C, the fans turn off. The process is repeated.
ATtiny85
-------u-------
RST - A0 - (D 5) --| 1 PB5 VCC 8 |-- +5V
| |
A3 - (D 3) --| 2 PB3 PB2 7 |-- (D 2) - A1 -->
| |
Thermister I/P A2 - (D 4) --| 3 PB4 PB1 6 |-- (D 1) - PWM --> Fan Mosfet
| |
Gnd ---| 4 GND PB0 5 |-- (D 0) - PWM -->
-----------------
V1.0
25.03.22
*/
int thermisterPin = A2;
int thermisterReading;
int fanPWMPin = 1;
int pwmLevelHigh = 255;
int pwmLevelOff = 0;
int pwmStep = 5;
boolean fanTriggered = false; //Require a first trigger to start controlling fan
void setup() {
pinMode (fanPWMPin, OUTPUT);
}
void loop() {
thermisterReading = analogRead(thermisterPin);
if (thermisterReading > 700)
{
if (pwmStep < 255)
{
{
if (pwmStep >= 245)
pwmStep = 255;
}
}
else
{
pwmStep++;
}
analogWrite (fanPWMPin, pwmStep);
delay(100);
fanTriggered = true;
}
else if ((thermisterReading > 300) && (fanTriggered == true))
{
analogWrite (fanPWMPin, pwmLevelHigh);
}
else if ((thermisterReading < 300) && (fanTriggered == true))
{
analogWrite (fanPWMPin, pwmLevelOff);
fanTriggered = false;
}
}
Can anyone see why the PWM is not incrementing beyond the first pwmStep? The code below appears to work fine when lower the voltage to the potentiometer.
I can't figure out how that is supposed to go. So... as the temperature rises, once it reaches 60C, the fan begins to run at X rpm? If the temp continues to rise, the rpm increases to maximum at Y degrees? Then if the temp drops, what should the rpm be at 60C and then at 40C when the fan switches off?
Should the fan rpm always be the same for a given temperature, regardless of whether the temp is increasing or decreasing?
So what you do is for the >60C bracket turn the fan on with just a digitalwrite (or if you must, pwm value 255).
For the bracket 40 <> 60 you could calculate the desired fan speed by converting the temperature into a pwm value. I understand you only do this if the temp has already reached >60 at some point and I see you also already have a boolean flag for this, so that should work. I'll not comment on the sensibility of only adjusting pwm values dynamically after the temperature has been high at some point; functionally this doesn't make much sense to me and the way I see it you'll just have a situation where temp rises to 60, fan goes on, temp drops a bit and then the fan starts modulating, keeping the temperature between 40 and 60 since you've essentially made a simple feedback loop controller, but OK.
Once temp is <40 you just turn it off with digitalWrite 0.
Of course you'll have to work out at what speed the fan spins up reliably; this will not be at pwm value 1, but considerably higher. There's also a difference between the pwm speed the fan will keep spinning if it's already turning at a higher speed, and the minimal pwm value at which the fan will spin up from standing still.
I have managed to get the code working with using digitalwrite previously.
Instead of the fan just being ON or OFF by using digitalwrite, I would like it to PWM up to 100% when turning ON and PWM back down to 0% when turning it off. I don't wish do dynamically change PWM with temperature.
I think you run into trouble because you're essentially making a state machine with if-statements and specifically you're not distinguishing between states such as '<40C and heating up' vs. '<40C but previously it was higher'. In principle your 'fanTriggered' boolean could catch this, but a proper finite state machine would probably be a conceptually more sound solution. This can also make room for states such as 'fan spinning up' and 'fan spinning down'.
It may sound like a more complicated approach at first, but in the end it's often simpler this way because you eliminate the problem of the code of multiple distinct states being executed parallel to each other.
How are you ascertaining that pwmStep is fixed at 5?
So far as I can see, if your temp sensor reading is > 700 you should always write out 6 to fanPWMPin because the code modifying pwmStep runs before the analogWrite.
Probably not an issue but you never reset pwmStep so far as I can see. So whatever value it has when the temp sensor drops below 700 is what it stays at for the next time the temperature goes above 700.
Yes, this is what is exactly what is happening. Once the sensor reads >700 the PWM is fixed on 6. I was expecting it to increment (pwmStep++) then analogWrite to fanPWMPin?
Note: You need a 'constrain()' because the output of a 'map()' is not constrained. If the input is outside the input range, the output will be outside the output range.