temp sensor values are crazy

I made a project where a fan turns on after the temperature exceeds 73.5 F and turns off when it goes below 73.5 F.The problem is when the temp data is being sent to the serial monitor, it’s showing 140 F and 120 F ,etc.,when it’s actually about 70-80 F in the room.Sometimes it works when I start it up and shows:74 F,but mostly the serial monitor shows temperature values in the 100-160 range.Any reason or coding problem?Is it a bad sensor?I modified the love o meter code from the starter kit examples on the arduino ide.I think its a Celsius to Fahrenheit conversion problem because the love o meter sent values to the serial monitor in celsius but I used a formula to convert it to fahrenheit.A1 gets the data from the temp sensor.Digital Pin 3 is an output that controls the motor.What does the second line do? Here is the code:

const int sensorPin = A1;

const float baselineTemp = 20.0;

void setup(){

Serial.begin(9600);
pinMode(3,OUTPUT);

}

void loop(){

int sensorVal = analogRead(sensorPin);

Serial.print("sensor Value: ");
Serial.print(sensorVal);

float voltage = (sensorVal/1024.0) * 5.0;

Serial.print(", Volts: ");
Serial.print(voltage);

Serial.print(", degrees F: ");
float temperature = ((voltage - .5) * 100*1.8)+32;
Serial.println(temperature);
if(temperature>73.5)
{
digitalWrite(3,HIGH);
}
else
{
digitalWrite(3,LOW);
}

}

sorry the smiley face with glasses was an accident. :wink: It’s supposed to be 1.8.The whole Celsius to Fahrenheit conversion formula is:
float temperature = ((voltage - .5) * 100*1.8)+32;

Sorry again I forgot to tell you guys this.Originally,in the love o meter code the Celcius formula was float temperature = (voltage - .5) * 100;

For some reason the 1.8 keeps turning into a smiley glasses face.Just ignore it. Sorry.

You need to surround your code with [code] and [/code] tags. It then creates a box that's immune to the bbcode interpreter.

voila

  float temperature = ((voltage - .5) * 100*1.8)+32;

BTW this is just an example. It would be better to do the same for your entire code block.

Thanks for the tip! ;) Any ideas about why the temp values are off?

zack1544: Thanks for the tip! ;) Any ideas about why the temp values are off?

You don't say which temperature sensor you are using.

I'm using the tmp36.

Just checked the datasheet and your maths appears sound. Are your values consistently high or do they fluctuate wildly?

Which arduino are you using?

They are consistently high.I'm using an arduino uno rev.3.Sometimes the values are correct.Also, when I move the sensor the values that are sent are super high.(like 150-160 F)When I put my finger on the sensor it goes to the 150's

How about you copy and post some of the output from those Serial.prints (don't forget the [code] [/code] tags) Maybe we can go through the values being recieved more thoroughly to work out if it's an issue with the value recieved or the maths.

ok problem solved...kinda :( I figured it out.I don't know why but when the motor turns on after 73.5 F then the temp values suddenly bounce from 73.5-75 F to 120-140F.I uploaded the same code without the motor output and it measured the temp perfectly.Never exceeded 82 F.Ok so my problem was solved kinda.But I still need that motor or my project would be useless.So basically now my problem is measuring the temperature perfectly even when the motor is on.Is it because the motor pulls too much current?I connected two transistors to form a darlington pair and connected digital pin 3 to the base .Here is the Serial Monitor output before the motor turns on or when I delete the motor activation code(the if/else statement that activates the motor after 73.5 F and turns it off when the temp is below 73.5 F):

sensor Value: 147, Volts: 0.72, degrees F: 71.20
sensor Value: 148, Volts: 0.72, degrees F: 72.08
sensor Value: 147, Volts: 0.72, degrees F: 71.20
sensor Value: 148, Volts: 0.72, degrees F: 72.08
sensor Value: 148, Volts: 0.72, degrees F: 72.08
sensor Value: 148, Volts: 0.72, degrees F: 72.08
sensor Value: 148, Volts: 0.72, degrees F: 72.08
sensor Value: 148, Volts: 0.72, degrees F: 72.08
sensor Value: 147, Volts: 0.72, degrees F: 71.20
sensor Value: 148, Volts: 0.72, degrees F: 72.08
sensor Value: 148, Volts: 0.72, degrees F: 72.08

Here is the Serial output when the motor gets activated after the temp is above 73.5 F:

sensor Value: 209, Volts: 1.02, degrees F: 125.69
sensor Value: 209, Volts: 1.02, degrees F: 125.69
sensor Value: 209, Volts: 1.02, degrees F: 125.69
sensor Value: 210, Volts: 1.03, degrees F: 126.57
sensor Value: 210, Volts: 1.03, degrees F: 126.57
sensor Value: 210, Volts: 1.03, degrees F: 126.57
sensor Value: 210, Volts: 1.03, degrees F: 126.57
sensor Value: 211, Volts: 1.03, degrees F: 127.45
sensor Value: 211, Volts: 1.03, degrees F: 127.45
sensor Value: 210, Volts: 1.03, degrees F: 126.57
sensor Value: 210, Volts: 1.03, degrees F: 126.57
sensor Value: 210, Volts: 1.03, degrees F: 126.57
sensor Value: 209, Volts: 1.02, degrees F: 125.69
sensor Value: 209, Volts: 1.02, degrees F: 125.69
sensor Value: 209, Volts: 1.02, degrees F: 125.69
sensor Value: 208, Volts: 1.02, degrees F: 124.81
sensor Value: 209, Volts: 1.02, degrees F: 125.69
sensor Value: 209, Volts: 1.02, degrees F: 125.69
sensor Value: 210, Volts: 1.03, degrees F: 126.57
sensor Value: 210, Volts: 1.03, degrees F: 126.57
sensor Value: 210, Volts: 1.03, degrees F: 126.57
sensor Value: 210, Volts: 1.03, degrees F: 126.57
sensor Value: 211, Volts: 1.03, degrees F: 127.45

you should always add hysteresis or “dead zone” to these kind of code, so

 if(temperature>73.5)
  {
    digitalWrite(3,HIGH);
  }
  else
  {
    digitalWrite(3,LOW); 
  }

should become

  if (temperature > 74)
  {
    digitalWrite(3, HIGH);
  }
  else if (temperature < 72)
  {
    digitalWrite(3, LOW); 
  }

You should switch on the motor by means of a power transistor, not directly from the Arduino. That can cause powerdrops/spikes that damage the on board electonics.

google pictures "arduino motor transistor diode"

e.g. - http://www.tinkerhobby.com/arduino-motor-control-spinning-night-light/ -

thanks for the tip.But even with the new code you gave me the values are still off :’(

AH I totally missed the reference to a motor being involved. Here's what I suspect is happening.

When the motor starts drawing power, the 5v line is pulled low. Since this is also the reference voltage for your analogRead functions, they will then return a much higher value.

You don't have a problem with your temperature sensor, you have an issue with the way you're powering your motor.

So any ideas about how I should power the motor?I don't want to use external batteries.I have diodes,transistors, and the ti L293DNE motor driver chip.I also have a 5v regulator MOSFET transistor.I have way more basic components like capacitors,resistors etc but I just listed the ones commonly associated with motors.

Thanks for the reply ;)

I don’t know how much current the motor pulls.But it isn’t that fast even with the darlington pair and digital pin 3 running at full blast.It’s medium fast,but I connected the same motor directly to a 9 volt battery and it spins MUCH faster.

If you pull the 5v from the arduino, it doesn't much matter what you're using to drive it, that current has got to come from the 5v on the arduino. It WILL be dragged down. You really need an alternative power source.