Created
July 4, 2013 08:28
-
-
Save dpslwk/5925934 to your computer and use it in GitHub Desktop.
Arduino TMP100 Test Sketch
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Display TMP100 readout to serial | |
// Fork Robotics 2012 | |
// | |
// Set the TMP Address and Resolution here | |
int tmpAddress = 0x48; | |
int ResolutionBits = 10; | |
#include <Wire.h> | |
void setup() | |
{ | |
//power the board | |
pinMode(A2,OUTPUT); | |
digitalWrite(A2,LOW); | |
pinMode(A3,OUTPUT); | |
digitalWrite(A3,HIGH); | |
Wire.begin(); // join i2c bus (address optional for master) | |
Serial.begin(9600); // start serial for output | |
SetResolution(); | |
} | |
void loop(){ | |
getTemperature(); | |
delay(200); | |
} | |
float getTemperature(){ | |
Wire.requestFrom(tmpAddress,2); | |
byte MSB = Wire.read(); | |
byte LSB = Wire.read(); | |
int TemperatureSum = ((MSB << 8) | LSB) >> 4; | |
float celsius = TemperatureSum*0.0625; | |
float fahrenheit = (1.8 * celsius) + 32; | |
Serial.print("Celsius: "); | |
Serial.println(celsius); | |
Serial.print("Fahrenheit: "); | |
Serial.println(fahrenheit); | |
} | |
void SetResolution(){ | |
if (ResolutionBits < 9 || ResolutionBits > 12) exit; | |
Wire.beginTransmission(tmpAddress); | |
Wire.write(B00000001); //addresses the configuration register | |
Wire.write((ResolutionBits-9) << 5); //writes the resolution bits | |
Wire.endTransmission(); | |
Wire.beginTransmission(tmpAddress); //resets to reading the temperature | |
Wire.write((byte)0x00); | |
Wire.endTransmission(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment