Tmp102 & making an output high upon certain temperature.

Hello, I am new to arduino and c+ but was hoping someone could guide me how to make my uno have a high output once a certain temperature is reached with the tmp102 sensor. Ive got a couple books from sparksfun on the way but til then Im on my own. This program came from sparkfun and would like to make a relay trigger when the temp goes above, say 80. Any help would be greatly appreciated.

#include <Wire.h>
int tmp102Address = 0x48;

void setup(){
 Serial.begin(9600);
 Wire.begin();
}

void loop(){

 float celsius = getTemperature();
 Serial.print("Celsius: ");
 Serial.println(celsius);


 float fahrenheit = (1.8 * celsius) + 32;  
 Serial.print("Fahrenheit: ");
 Serial.println(fahrenheit);

 delay(200); //just here to slow down the output. You can remove this
}

float getTemperature(){
 Wire.requestFrom(tmp102Address,2); 

 byte MSB = Wire.read();
 byte LSB = Wire.read();

 //it's a 12bit int, using two's compliment for negative
 int TemperatureSum = ((MSB << 8) | LSB) >> 4; 

 float celsius = TemperatureSum*0.0625;
 return celsius;
}
[code]

[/code]

Welcome. Please read the "how to post to this forum". Specifically, the section about posting code inside code tags, then go back and edit your original post so that the code is inside code tags.

It should look like this when it's correct:

   ... code here

Then, you need to look at how to hook up your relay. Some background information is http://www.seeedstudio.com/wiki/Relay_Shield_V2.0

but you don't have to use that shield.

Other than that, just put an "if" in your loop and toggle the arduino pin the relay is attached to.

Thank you for responding. Obviously I need to learn c+ because Im not sure where the if goes. I had something of that nature on a code earlier but it didnt work correct. I assumed I didnt identify the variable correctly. But I did have a if and a else statement.
fahrenheit > 80
digitalWrite (pin#, HIGH)

I apologize again for not knowing anything, just bored and wanted to get my new arduino board doing something.

Well, you're on the right track.

Put your if back in your code, then post that code and we'll see if we can help you get that working.

But yes, look up "if" for C++ on the internet.

#include <Wire.h>
int tmp102Address = 0x48;
int ppin = 8;

void setup(){
  Serial.begin(9600);
  Wire.begin();
}

void loop(){
  pinMode(ppin, OUTPUT);
  float celsius = getTemperature();
  Serial.print("Celsius: ");
  Serial.println(celsius);


  float fahrenheit = (1.8 * celsius) + 32;  
  Serial.print("Fahrenheit: ");
  Serial.println(fahrenheit);

  delay(200); //just here to slow down the output. You can remove this
}

float getTemperature(){
  Wire.requestFrom(tmp102Address,2); 

  byte MSB = Wire.read();
  byte LSB = Wire.read();

  //it's a 12bit int, using two's compliment for negative
  int TemperatureSum = ((MSB << 8) | LSB) >> 4; 

  float celsius = TemperatureSum*0.0625;
  return celsius;
  if (fahrenheit > 80);
  {
  digitalWrite (ppin, HIGH);
  }

}

thanks again.

OK, so yeah, you have to learn a bit more C++.

The "return" keyword returns from a subroutine. No more lines of the subroutine will be executed.

Also, logically, you really should put your "if" in the main loop, instead of in a function called "getTemperature()".

Get the value using getTemperature, and when it returns, do the "if" using the value that it returned. Try to keep subroutines only doing the functions that the name of the subroutines say they will do.

arduinodlb, your awesome. Its working. Ill post the code if someone else may need it. Thanks again, and yes I need much more experience with c++.

Heres the working code. Still playing with it but if someone else can use it please do. Thanks for the help.

//Arduino 1.0+ Only
//Arduino 1.0+ Only

//////////////////////////////////////////////////////////////////
//©2011 bildr
//Released under the MIT License - Please reuse change and share
//Simple code for the TMP102, simply prints temperature via serial
//////////////////////////////////////////////////////////////////

#include <Wire.h>
int tmp102Address = 0x48;
int ppin = 8;
void setup(){
  Serial.begin(9600);
  Wire.begin();
}

void loop(){
  pinMode(ppin, OUTPUT);
  float celsius = getTemperature();
  Serial.print("Celsius: ");
  Serial.println(celsius);


  float fahrenheit = (1.8 * celsius) + 32;  
  Serial.print("Fahrenheit: ");
  Serial.println(fahrenheit);

  delay(200); //just here to slow down the output. You can remove this
   if (getTemperature() < 30)
  {
  digitalWrite (ppin, HIGH);
  }
  else if (getTemperature() > 30)
  {
  digitalWrite (ppin, LOW);
   }
   }
float getTemperature(){
  Wire.requestFrom(tmp102Address,2); 

  byte MSB = Wire.read();
  byte LSB = Wire.read();

  //it's a 12bit int, using two's compliment for negative
  int TemperatureSum = ((MSB << 8) | LSB) >> 4; 

  float celsius = TemperatureSum*0.0625;
  return celsius;
  {
 

}
}

you don't really need the "else if" as you compared the same number. else {digitalwrite.........} would work

Nothing wrong with doing what you did because In the future you will probably have to change the "else if" to a different number to stop the relay being turned on and off as the temp hovers around the set point.

you will also need to check your compares ( < ) as your relay is going to be active when the temp is lower than 30

great start and welcome to learning.