TMP36 Sensor

Hello,

I'll get straight to the point, I'm trying to create a relay to turn on and off a light bulb - controlled by a TMP36 temperature sensor. I am a complete beginner. My sensor is outputting negative values, and are incredibly in accurate.

My code is as follows, I've also attached my schematic below.

int sensorPin = A0;
int relay = 6;
void setup()
{

  • Serial.begin(9600);*
  • pinMode(relay, OUTPUT);*
    }

*void loop() *
{

  • int reading = analogRead(sensorPin); *
    _ float voltage = reading * 5.0;_
  • voltage /= 1024.0;*
  • Serial.print(voltage); Serial.println(" volts");*
    _ float temperatureC = (voltage - 0.5) * 100 ;_
  • Serial.print(temperatureC); Serial.println(" degrees C");*
  • if (temperatureC <= 25)*
  • {*
  • digitalWrite(relay, HIGH);*
  • }*
  • else*
  • {*
  • digitalWrite(relay, LOW);*
  • }*
    delay(1000);
    }

The temperature sensor does not seem to be working well with the relay, and I'm not sure whether I have the code correct for that.

How do I get the sensor to output the correct values, and is my code correct for it to communicate with the relay?

Thank you anyone who can help.

What temperature are you getting (after 5 minutes of hands-off)? :slight_smile:

My sensor is outputting negative values, and are incredibly in accurate.

Unfortunately incredibly inaccurate doesn't say much, a few well defined numbers would help considerably.

This is a good place to get some sample code. Take note of both sample code examples with a focus on getting better precision using a 3.3 volt reference. I have used the latter with good results.

I would start with the first sample example and then enter your "If" statements. Another good example of using the If Else statements may be found here. That last example includes the use of buttons to increment and decrement a set point Up & Down including putting it all on a display. You can modify the code to work for your application.

Ron

I'm getting a pretty constant -29.98 degrees C and 0.20 volts. Shifts slightly sometimes to -29.29 and 0.21 volts. The sensor is working - when I hold it the readings do change.

If you wired it as shown in your figure, the power polarity is backwards.

DaveEvans:
If you wired it as shown in your figure, the power polarity is backwards.

HA! I glanced at the pic and saw red wire on the left end and black on the right, CORRECT, IF the red wire is + and black -, but they're connected to Arduino azz- back'ards.

Using your code if I apply 750 mV to pin A0 I read right about 25.68 degrees C where I would expect 25.00 so that is normal. Your If statement works on my end too. So what I have is everything works.

Ron

Comparing sensor value to the potentially unstable 5volt supply of the Arduino (default Aref) is like measuring distance with a rubber band. Powering a relay from that 5volt supply is going to make things worse.

Switching to 1.1volt Aref is usually the solution for analogue sensors like this (LM35, TMP36).
See this sketch.
Leo..

const byte tempPin = A0; // connect TPM36 to 3.3volt A0 and (not-shared) ground
float calibration = 0.1039; // calibrate temp by changing the last digit(s) of "0.1039"
float tempC;

void setup() {
  Serial.begin(9600);
  analogReference(INTERNAL); // use the stable internal 1.1volt Aref
}

void loop() {
  tempC = (analogRead(tempPin) * calibration) - 50.0;
  
  Serial.print("Temperature:  ");
  Serial.print(tempC, 1); // one meaningful decimal place is all you get
  Serial.println(" C");
  
  delay(1000); // use a non-blocking delay when combined with other code
}