Problem with Temperature Sensor/Motor code

I am trying to build a fan that senses the temperature in the room and turns on a motor after the temperature sensor surpasses the room temperature. However, the temperature sensor continues to give fluctuating readings and often times the arduino serial monitor reads temperature values even when the temperature sensor is not present in the circuit. Here is the code, any assistance helpful

int led = 13; //led attached to pin 13
int motorPin = 9; //motor attached to pin 9
const int tempPin = A0; //temperature sensor attached to pin A0
float baselineTemp = 0; //Base temperature is 21 degrees celsius
int CaltempVal;
int sensorVal;
int tempVal;
float temp;

void setup() {
pinMode(led,OUTPUT); //voltage coming out of pin 13
pinMode(motorPin, OUTPUT);//votlage coming out of pin 9
Serial.begin(9600); //open serial connection to display values
Serial.println("begin");
while(millis() < 5000) { // calibrate for 5 secs
CaltempVal= analogRead(A0);
if (CaltempVal > baselineTemp){ //if the temp in the room is higher than baseline temp than temp value is new baseline temp
baselineTemp = CaltempVal;
}
}
float volt = (baselineTemp / 1024.0)* 5.0; //convert the ADC reading to voltage

float temp = (volt - .5)* 100;
Serial.print("finished, roomtemp is:");
Serial.println(temp);
}
void loop() {
int tempVal = analogRead(tempPin);
Serial.print("sensor Value:"); //print out the name serial print
Serial.print(tempVal); //send out the voltage

float voltage = (tempVal / 1024.0)* 5.0; //convert the ADC reading to voltage

Serial.print(", Volts: ");
Serial.print(voltage); //send the voltage level tout the Serial port

Serial.print(",degrees C:");
float temperature = (voltage - .5)* 100;
Serial.println(temperature); //convert the voltage in to temperature in degrees C

if (temperature <= temp +4){ // if temperature is lower or equal to room temp, the fan is off
digitalWrite(led, LOW);
digitalWrite(motorPin, LOW);
}
else if (temperature > temp + 4) { //if temperature is higher than room temp, the fan will turn on
digitalWrite(led, HIGH);
digitalWrite(motorPin,HIGH);
}

delay(500);
}
//end of loop

Please edit your post and add code tags ("</>" button).

often times the arduino serial monitor reads temperature values even when the temperature sensor is not present in the circuit.

You are reading the voltage on an analog pin. That pin will have a voltage value, regardless of whether a temperature sensor is attached to it.

paulinom99:
I am trying to build a fan that senses the temperature in the room and turns on a motor after the temperature sensor surpasses the room temperature. However, the temperature sensor continues to give fluctuating readings and often times the arduino serial monitor reads temperature values even when the temperature sensor is not present in the circuit. Here is the code, any assistance helpful

int led = 13;  //led attached to pin 13

int motorPin = 9; //motor attached to pin 9
const int tempPin = A0; //temperature sensor attached to pin A0
float baselineTemp = 0; //Base temperature is 21 degrees celsius
int CaltempVal;
int sensorVal;
int tempVal;
float temp;

void setup() {
 pinMode(led,OUTPUT); //voltage coming out of pin 13
 pinMode(motorPin, OUTPUT);//votlage coming out of pin 9
 Serial.begin(9600); //open serial connection to display values
 Serial.println("begin");
  while(millis() < 5000) { // calibrate for 5 secs
 CaltempVal= analogRead(A0);
if (CaltempVal > baselineTemp){ //if the temp in the room is higher than baseline temp than temp value is new baseline temp
 baselineTemp = CaltempVal;
 }
}
 float volt = (baselineTemp / 1024.0)* 5.0;     //convert the ADC reading to voltage

float temp = (volt - .5)* 100;
Serial.print("finished, roomtemp is:");
Serial.println(temp);
}
void loop() {
 int tempVal = analogRead(tempPin);
 Serial.print("sensor Value:"); //print out the name serial print
 Serial.print(tempVal); //send out the voltage
 
 float voltage = (tempVal / 1024.0)* 5.0;     //convert the ADC reading to voltage
 
 Serial.print(", Volts: ");
 Serial.print(voltage); //send the voltage level tout the Serial port
 
 Serial.print(",degrees C:");
 float temperature = (voltage - .5)* 100;
 Serial.println(temperature); //convert the voltage in to temperature in degrees C

if (temperature <= temp +4){ // if temperature is lower or equal to room temp, the fan is off
 digitalWrite(led, LOW);
 digitalWrite(motorPin, LOW);
}
else if (temperature > temp + 4) { //if temperature is higher than room temp, the fan will turn on
 digitalWrite(led, HIGH);
 digitalWrite(motorPin,HIGH);
}

delay(500);
}
//end of loop

Your code has a few problems:
First, you have a global variable named temp, then you create a local variable in setup() also named temp. Then you use the the uninitialized global temp to decide wheither or not to turn on the fan.

Remove the local declaration of temp in setup(). Just use the global temp, like this:

// origional code in setup()

  float temp = (volt - .5)* 100; // declaring a local float variable named temp

// corrected Code

  temp = (volt - .5)* 100;  // using the globally defined variable named temp

Also, we could give you more specific answers if you included a schematic of your circuit, and actually identified which Arduino you are using (Uno,Mega,mini, Pro Mini, ... etc).

Please use the [code] and [/code] markup tags before and after your code, it makes it easier to read.

Chuck.