Question about temp, relay and if statements

Hi all, this is my first post and I’m brand new but am having a blast learning about Arduino and have so many little projects in mind.

I am just playing around with a few things here. In the code below I have put it together from a few projects found online. And have a 12v fan hooked up via a seeed relay shield. I have used the seeed code to get the fan to click on and off. I also have a temp sensor (tmp36) hooked up.

They both work independently of one another, now I’m trying to get the fan to come on when the temperature is above 80F. below is my code.

right now it is turning on the fan as soon as I upload the code then running until the temp hits 80 then turning off, then not coming back on.

Any ideas? I’m sure this is a small one.

//TMP36 Pin Variables
int temperaturePin = 0; //the analog pin the TMP36's Vout (sense) pin is connected to
                        //the resolution is 10 mV / degree centigrade 
                        //(500 mV offset) to make negative temperatures an option

int MotorControl = 7;    // Digital Arduino Pin used to control the motor

int desiredtemp = 80;

 * setup() - this function runs once when you turn your Arduino on
 * We initialize the serial connection with the computer
void setup()
  Serial.begin(9600);  //Start the serial connection with the copmuter
                       //to view the result open the serial monitor 
                       //last button beneath the file bar (looks like a box with an antenae)
  pinMode(MotorControl, OUTPUT);
void loop()                     // run over and over again
 float temperature = getVoltage(temperaturePin);  //getting the voltage reading from the temperature sensor
 temperature = ((((temperature - .5) * 100) * 9) / 5) +32;          //converting from 10 mv per degree wit 500 mV offset
                                                  //to degrees ((volatge - 500mV) times 100)
if (temperature > desiredtemp){ 
  digitalWrite(MotorControl, HIGH);
   Serial.println(temperature);                     //printing the result
 delay(1000);                                     //waiting a second


 * getVoltage() - returns the voltage on the analog input defined by
 * pin
float getVoltage(int pin){
 return (analogRead(pin) * .004882814); //converting from a 0 to 1023 digital range
}                                        // to 0 to 5 volts (each 1 reading equals ~ 5 millivolts

You only have one line that controls the motor. The output is LOW at initialization. I would assume that turns the motor on.

    digitalWrite(MotorControl, HIGH); Turns it off.

So it’s just doing what you tell it to do.

Also for a practical thermostat, you should have some hysteresis.

My end goal here is to build a meat curing chamber for charcuterie. :slight_smile: but like I said, I'm brand new here.

So I think to begin I'm trying to have a fan run to cool only, until the temp is low enough then the fan turns off. I see that only one line controls the fan here. Do I need to re-do the 'if' statement to include a 'for' statement?

So I think to begin I'm trying to have a fan run to cool only, until the temp is low enough then the fan turns off.

Again, you need another test and statement to turn it off.
If you use an else clause, you will have no hysteresis. But it would work. Without hysteresis, the motor may "chatter" or "fidget" on and off, when it's close to the on/off temperature.

See, I told you I was new to this. Your last comment made me 'see it'. I was using a command to turn the fan on, but not off again, and vice-versa.

And, yes, once done with the project, since it will be hooked up to a mini fridge, I will have the code in there to control the clicking on and off.

Thanks for your help!!!