Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Wire.h>
- int tmp102Address = 0x48;
- void setup(){
- Serial.begin(9600); //Set our baud rate to 9600 for our serial connection
- Wire.begin(); // Initiate the Wire library and join the I2C bus
- }
- void loop(){
- float celsius = getTemperature(); // Get the temperature in C and save it as a float called celsius.
- Serial.print("Celsius: "); // Print our description of the temp
- Serial.println(celsius); // Print our temp in C
- float fahrenheit = (1.8 * celsius) + 32; // Get the temperature in C and save it as a float called fahrenheit after converting it from celsius.
- Serial.print("Fahrenheit: "); // Print our description of the temp.
- Serial.println(fahrenheit); // Print the temp in F
- delay(200); //just here to slow down the output. You can remove this
- }
- // Let’s take a look at our getTempurature function. Without it, nothing would work.
- float getTemperature(){
- Wire.requestFrom(tmp102Address,2); // We use our wire library’s get two readings from our TMP102. We pass it the address and the quantity.
- // You may have noticed that we didn’t do anything with the request. That’s because we have to read back we requested. We do that sequentially.
- byte MSB = Wire.read(); // The first byte we get, the Most Significant, get’s stored in MSB.
- byte LSB = Wire.read(); // The second byte we get, the Least Significant, get’s stored in LSB.
- int TemperatureSum = ((MSB << 8) | LSB) >> 4;
- float celsius = TemperatureSum*0.0625; // Convert our temp to celsius.
- return celsius; // Return our result to be printed.
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement