Last active
November 12, 2019 20:07
-
-
Save bboyho/96ef90fd2c51f021b8174264ef7c7a8c to your computer and use it in GitHub Desktop.
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
| /****************************************************************************** | |
| SparkFun_TMP117_Breakout_Example.ino | |
| Example for the TMP117 I2C Temperature Sensor | |
| Madison Chodikov @ SparkFun Electronics | |
| May 29 2019 | |
| ~ | |
| This sketch configures the TMP117 temperature sensor and prints the | |
| alert state of the temperature sensor. | |
| Resources: | |
| Wire.h (included with Arduino IDE) | |
| SparkFunTMP117.h (included in the src folder) | |
| Development environment specifics: | |
| Arduino 1.8.9+ | |
| Hardware Version 1.0.0 | |
| This code is beerware; if you see me (or any other SparkFun employee) at | |
| the local, and you've found our code helpful, please buy us a round! | |
| Distributed as-is; no warranty is given. | |
| ******************************************************************************/ | |
| /* | |
| NOTE: For the most accurate readings: | |
| - Avoid heavy bypass traffic on the I2C bus | |
| - Use the highest available communication speeds | |
| - Use the minimal supply voltage acceptable for the system | |
| - Place device horizontally and out of any airflow when storing | |
| For more information on reaching the most accurate readings from the sensor, | |
| reference the "Precise Temperature Measurements with TMP116" datasheet that is | |
| linked on Page 35 of the TMP117's datasheet | |
| */ | |
| #include <Wire.h> // Used to establish serial communication on the I2C bus | |
| #include <SparkFun_TMP117.h> // Used to send and recieve specific information from our sensor | |
| // The default address of the device is 0x48 (GND) | |
| TMP117 sensor; // Initalize sensor | |
| void setup() | |
| { | |
| Wire.begin(); | |
| Serial.begin(115200); // Start serial communication at 115200 baud | |
| Wire.setClock(400000); // Set clock speed to be the fastest for better communication (fast mode) | |
| Serial.println("TMP117 Example 2: Alert Statuses"); | |
| if (sensor.begin() == true) // Function to check if the sensor will correctly self-identify with the proper Device ID/Address | |
| { | |
| Serial.println("Begin"); | |
| } | |
| else | |
| { | |
| Serial.println("Device failed to setup- Freezing code."); | |
| while (1); | |
| } | |
| //Get Current High Limit | |
| Serial.println(""); | |
| Serial.print("Current High Limit: "); | |
| Serial.print(sensor.getHighLimit()); | |
| Serial.println("°C"); | |
| //Get Current Low High Limit | |
| Serial.print("Current Low Limit: "); | |
| Serial.print(sensor.getLowLimit()); | |
| Serial.print("°C"); | |
| Serial.println(""); | |
| /*Note: Uncomment 1 of the lines below by remobing the `//` | |
| to set to alert or therm mode*/ | |
| sensor.setAlertFunctionMode(0);//set to alert mode | |
| //sensor.setAlertFunctionMode(1);//set to therm mode | |
| /*Get "Alert Function Mode" Bit from configuration register | |
| Note: Depending on the mode, this affects the way HIGH and | |
| LOW Alert Fields behave in the Configuration Register. For more | |
| information, check out the following sections of the datasheet: | |
| 7.4.4.1 Alert Mode (pg 14) | |
| 7.4.4.2 Therm Mode (pg 16) | |
| 7.6.1.2 Configuration Register (pg 26)*/ | |
| delay(500);//wait a little before grabbing current mode | |
| Serial.print("Alert Function Mode = "); | |
| Serial.println(sensor.getAlertFunctionMode()); | |
| Serial.println(""); | |
| } | |
| /* Alert statuses below for high or low temperature reading | |
| possibilities: High Alert = 256°C, Low Alert = -256°C*/ | |
| void loop() | |
| { | |
| // Data Ready is a flag for the conversion modes - in continous conversion the dataReady flag should always be high | |
| if (sensor.dataReady() == true) // Function to make sure that there is data ready to be printed, only prints temperature values when data is ready | |
| { | |
| /*Note: If you are in Alert Mode (T/nA = 0), the high and low alert | |
| flags will clear whenever you read the configuration register. You | |
| can comment out the following line when reading the sensor or add | |
| a delay to to perform another temperature conversion to trigger the | |
| flags again. The delay depends on the conversion cycle time so make | |
| sure to adjust as necessary to check the if the flags are triggered.*/ | |
| Serial.print("Current Temperature: "); | |
| Serial.print(sensor.readTempC()); | |
| Serial.println("°C"); | |
| delay(1500); | |
| if (sensor.getHighAlert() == true) | |
| { | |
| /*Alert when the temperature is over the HIGH limit: | |
| - In Alert Mode (T/nA = 0) this flag will clear | |
| when the configuration register is read. | |
| - In Therm mode (T/nA = 1), this flag will clear ONLY | |
| when we have reached the lower limit. This high and | |
| low limits act as a hystersis.*/ | |
| Serial.println("High Alert"); | |
| } | |
| if (sensor.getLowAlert() == true) | |
| { | |
| /*Alert when the temperature is over the LOW limit: | |
| - In Alert Mode (T/nA = 0) this flag will clear | |
| when the configuration register is read. | |
| - In Therm mode (T/nA = 1), this flag is always 0*/ | |
| Serial.println("Low Alert"); | |
| } | |
| else | |
| { | |
| Serial.println("No Alert"); | |
| } | |
| delay(500); // Delay for a 1/2 second before printing again if the data is ready | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment