i was wondering if any one knows how to convert this to output farenheit instead of celcius . i have tried but havent gotten it to work .
i have tried adding TempHi*1.8+32 but i dont think that is correct
#include <Wire.h>
byte TempHi; // Variable hold data high byte
byte TempLo; // Variable hold data low byte
boolean P_N; // Bit flag for Positive and Negative
unsigned int Decimal; // Variable hold decimal value
void Cal_Temp();
void setup()
{
Serial.begin(9600);
Wire.begin(); // join i2c bus (address optional for master)
delay(1000);
}
void loop()
{
const int I2C_address = 55; // I2C write address
delay(100);
Wire.beginTransmission(I2C_address);
Wire.send(1); // Setup configuration register
Wire.send(0x60); // 12-bit
Wire.endTransmission();
Wire.beginTransmission(I2C_address);
Wire.send(0); // Setup Pointer Register to 0
Wire.endTransmission();
while (1)
{
delay(10);
// Read temperature value
Wire.requestFrom(I2C_address, 2);
while(Wire.available()) // Checkf for data from slave
{
TempHi = Wire.receive(); // Read temperature high byte
TempLo = Wire.receive(); // Read temperature low byte
}
Cal_Temp ();
// Display temperature
Serial.print("The temperature is ");
if (P_N == 0)
Serial.print("-");
Serial.print(TempHi,DEC);
Serial.print(".");
Serial.print(Decimal,DEC);
Serial.println(" degree C");
}
}
void Cal_Temp()
{
if (TempHi&0x80) // If bit7 of the TempHi is HIGH then the temperature is negative
P_N = 0;
else // Else the temperature is positive
P_N = 1;
TempHi = TempHi & 0x7F; // Remove sign
TempLo = TempLo & 0xF0; // Filter out last nibble
TempLo = TempLo >>4; // Shift right 4 times
Decimal = TempLo;
Decimal = Decimal * 625; // Each bit = 0.0625 degree C
}
but it adds 2 decimal points and when i take out Serial.print("."); it looks like its off but i dont have a digital temp gun but i just think im messing it up.
I am not sure that Serial.print() can handle expressions.
After looking at your code (correct me if I am wrong) but it looks like the temperature data is read in two bytes, TempHi which is the integer value (left of the decimal point) of the temp in Celsius and TempLo which is the decimal value (right of the decimal) in integer multiples of .0625 degrees C.
not sure about this code though
TempHi = TempHi & 0x7F; // Remove sign
TempLo = TempLo & 0xF0; // Filter out last nibble
TempLo = TempLo >>4; // Shift right 4 times
What I would do is to first combine them into one floating point variable.
float combined_temp_F; //create a new variable
......
combined_temp_F = combined_temp_c*1.8+32; //set new variable to expression
.....
Serial.printIn( newvariable, 2); //where 2 means print up to 2 decimal places, but can be whatever you want
thanx alot man that worked perfect i didnt think to try it like that but im still learning to mess with the arduino THANX alot man and heres my code if anyone else is interested . its for TMP175 from ti.com and turns a fan connected to a transistor on or off depending on the temp
/******************************************************************************
Example program I2C-TMP interface with Arduino.
SETUP: I2C-TMP => Arduino
PIN3 => ground, PIN4 => A5(SCL), PIN5 => A4(SDA), PIN6 => +5V
Note: The program is written for address 0x90 (Arduino address 0x48).
This program was tested using Arduino Nano
Document: TMP175 datasheet
Updated: September 4, 2008
E-mail: support@gravitech.us
Gravitech
(C) Copyright 2008 All Rights Reserved
*******************************************************************************/
#include <Wire.h>
float combined_temp_c;
float combined_temp_F; //create a new variable
byte TempHi; // Variable hold data high byte
byte TempLo; // Variable hold data low byte
boolean P_N; // Bit flag for Positive and Negative
unsigned int Decimal; // Variable hold decimal value
int ventpin = 9;
void Cal_Temp();
/*******************************************************************************
Setup
*******************************************************************************/
void setup()
{
pinMode(ventpin,OUTPUT);
Serial.begin(9600);
Wire.begin(); // join i2c bus (address optional for master)
delay(1000);
}
/*******************************************************************************
Main Loop
*******************************************************************************/
void loop()
{
const int I2C_address = 55; // I2C write address
delay(100);
Wire.beginTransmission(I2C_address);
Wire.send(1); // Setup configuration register
Wire.send(0x60); // 12-bit
Wire.endTransmission();
Wire.beginTransmission(I2C_address);
Wire.send(0); // Setup Pointer Register to 0
Wire.endTransmission();
while (1)
{
delay(10);
// Read temperature value
Wire.requestFrom(I2C_address, 2);
while(Wire.available()) // Checkf for data from slave
{
TempHi = Wire.receive(); // Read temperature high byte
TempLo = Wire.receive(); // Read temperature low byte
}
Cal_Temp ();
// Display temperature
Serial.print("The temperature is ");
if (P_N == 0)
Serial.print("-");
Serial.println(combined_temp_F,DEC);
if (TempHi*1.8+32 > 80 ){
analogWrite(ventpin, 255);
}
if (TempHi*1.8+32 < 80 ){
analogWrite(ventpin, 0);
}
}
}
void Cal_Temp()
{
if (TempHi&0x80) // If bit7 of the TempHi is HIGH then the temperature is negative
P_N = 0;
else // Else the temperature is positive
P_N = 1;
TempHi = TempHi & 0x7F; // Remove sign
TempLo = TempLo & 0xF0; // Filter out last nibble
TempLo = TempLo >>4; // Shift right 4 times
Decimal = TempLo;
Decimal = Decimal * 625; // Each bit = 0.0625 degree C
combined_temp_c= TempHi + TempLo*.0625;
combined_temp_F = combined_temp_c*1.8+32;
}