Devices & Components
Arduino Uno Rev3
SparkFun Digital Temperature Sensor Breakout - TMP102
Male/Female Jumper Wires
Software & Tools
Arduino IDE
Project description
Code
Code:
arduino
1#include <Wire.h> // Used to establied serial communication on the I2C bus 2#include <SparkFunTMP102.h> // Used to send and recieve specific information from our sensor 3 4// Connections 5// VCC = 3.3V 6// GND = GND 7// SDA = A4 8// SCL = A5 9const int ALERT_PIN = A3; 10 11TMP102 sensor0; 12 13void setup() { 14 Serial.begin(115200); 15 Wire.begin(); //Join I2C Bus 16 17 pinMode(ALERT_PIN,INPUT); // Declare alertPin as an input 18 19 /* The TMP102 uses the default settings with the address 0x48 using Wire. 20 21 Optionally, if the address jumpers are modified, or using a different I2C bus, 22 these parameters can be changed here. E.g. sensor0.begin(0x49,Wire1) 23 24 It will return true on success or false on failure to communicate. */ 25 if(!sensor0.begin()) 26 { 27 Serial.println("Cannot connect to TMP102."); 28 Serial.println("Is the board connected? Is the device ID correct?"); 29 while(1); 30 } 31 32 Serial.println("Connected to TMP102!"); 33 delay(100); 34 35 // Initialize sensor0 settings 36 // These settings are saved in the sensor, even if it loses power 37 38 // set the number of consecutive faults before triggering alarm. 39 // 0-3: 0:1 fault, 1:2 faults, 2:4 faults, 3:6 faults. 40 sensor0.setFault(0); // Trigger alarm immediately 41 42 // set the polarity of the Alarm. (0:Active LOW, 1:Active HIGH). 43 sensor0.setAlertPolarity(1); // Active HIGH 44 45 // set the sensor in Comparator Mode (0) or Interrupt Mode (1). 46 sensor0.setAlertMode(0); // Comparator Mode. 47 48 // set the Conversion Rate (how quickly the sensor gets a new reading) 49 //0-3: 0:0.25Hz, 1:1Hz, 2:4Hz, 3:8Hz 50 sensor0.setConversionRate(2); 51 52 //set Extended Mode. 53 //0:12-bit Temperature(-55C to +128C) 1:13-bit Temperature(-55C to +150C) 54 sensor0.setExtendedMode(0); 55 56 //set T_HIGH, the upper limit to trigger the alert on 57 sensor0.setHighTempF(82.0); // set T_HIGH in F 58 //sensor0.setHighTempC(29.4); // set T_HIGH in C 59 60 //set T_LOW, the lower limit to shut turn off the alert 61 sensor0.setLowTempF(81.0); // set T_LOW in F 62 //sensor0.setLowTempC(26.67); // set T_LOW in C 63} 64 65void loop() 66{ 67 float temperature; 68 boolean alertPinState, alertRegisterState; 69 70 // Turn sensor on to start temperature measurement. 71 // Current consumtion typically ~10uA. 72 sensor0.wakeup(); 73 74 // read temperature data 75 temperature = sensor0.readTempF(); 76 //temperature = sensor0.readTempC(); 77 78 // Check for Alert 79 alertPinState = digitalRead(ALERT_PIN); // read the Alert from pin 80 alertRegisterState = sensor0.alert(); // read the Alert from register 81 82 // Place sensor in sleep mode to save power. 83 // Current consumtion typically <0.5uA. 84 sensor0.sleep(); 85 86 // Print temperature and alarm state 87 Serial.print("Temperature: "); 88 Serial.print(temperature); 89 90 Serial.print("\ Alert Pin: "); 91 Serial.print(alertPinState); 92 93 Serial.print("\ Alert Register: "); 94 Serial.println(alertRegisterState); 95 96 delay(1000); // Wait 1000ms 97} 98
Downloadable files
Schematics
Schematics

Schematics
Schematics

Comments
Only logged in users can leave comments