Ex: Temperature Sensor

This circuit works like a thermostat. As soon as the temperature goes higher than the 'hi-threshold', the led lights up to let you know it's too hot.

I tried using the alert bit to control the led at first. It didn't work too well, and very unpredictably. I doubt it even was working through the alert bit at all. So, for now it's just a brute force led turning up when the temperature goes higher than the value stored in the T_high register.

Ex: Temperature Sensor from naureenm on Vimeo.



// ARDUINO CODE

#include "wire.h" // (where " "=angled brackets)

int led = 2;

int ptrTemp =  0b00000000;         // TMP102 register pointers
int ptrConf =  0b00000001;
int ptrTLow =  0b00000010;
int ptrTHigh = 0b00000011;
int i2cAddr;                        // used to set address on the chip

void setup()
{
  pinMode(led,OUTPUT);
  Serial.begin(9600);
  Wire.begin();                     // join i2c bus (address optional for master)
  i2cAddr = 0b1001001;              // ADD0 pin connection selects one of four possible addresses
}

void loop()

//  REQUESTING TEMPERATURES
 /*  TEMPERATURE REGISTER
 */
  Wire.beginTransmission(i2cAddr);  // select temperature register
  Wire.send(ptrTemp);               // (if only temperature is needed this can
  Wire.endTransmission();           // be done once in setup() )

  Wire.requestFrom(i2cAddr, 2);     // request temperature
  byte byte1 = Wire.receive();
  byte byte2 = Wire.receive();   
 
 /*  T_LOW REGISTER
 */
  Wire.beginTransmission(i2cAddr);  // select temperature register
  Wire.send(ptrTLow);
  Wire.send(24);  // (if only temperature is needed this can
  Wire.endTransmission();           // be done once in setup() )

  Wire.requestFrom(i2cAddr, 2);     // request temperature
  byte lowbyte1 = Wire.receive();
  byte lowbyte2 = Wire.receive();   
 
 /*  T_High REGISTER
 */
  Wire.beginTransmission(i2cAddr);  // select temperature register
  Wire.send(ptrTHigh); 
  Wire.send(28);    // (if only temperature is needed this can
  Wire.endTransmission();           // be done once in setup() )

  Wire.requestFrom(i2cAddr, 2);     // request temperature
  byte hibyte1 = Wire.receive();
  byte hibyte2 = Wire.receive();   
 
//  CALCULATING TEMPERATURES
  int tempint = byte1 << 8;         // shift first byte to high byte in an int
  tempint = tempint | byte2;        // or the second byte into the int
  tempint = tempint >> 4;           // right shift the int 4 bits per chip doc
  float tempflt = float( tempint ) * .0625; // calculate actual temperature per chip doc
 
 //  Low_temp 
  int lowtempint = lowbyte1 << 8;         // shift first byte to high byte in an int
  lowtempint = lowtempint | lowbyte2;        // or the second byte into the int
  lowtempint = lowtempint >> 4;           // right shift the int 4 bits per chip doc
  float lowtempflt = float( lowtempint ) * .0625; // calculate actual temperature per chip doc

//  Hi_temp
  int hitempint = hibyte1 << 8;         // shift first byte to high byte in an int
  hitempint = hitempint | hibyte2;        // or the second byte into the int
  hitempint = hitempint >> 4;           // right shift the int 4 bits per chip doc
  float hitempflt = float( hitempint ) * .0625; // calculate actual temperature per chip doc


 // PRINTING TEMPERATURES
  Serial.print( "Temp: C = ");
  Serial.print( int( tempflt ) );
  int tempF = (tempflt * 9/5) + 32;
  Serial.print( " F = ");
  Serial.print( int( tempF ) );

  Serial.print( "   Low:  C = ");
  Serial.print( int( lowtempflt ) );
  int lowF = (lowtempflt * 9/5) + 32;
  Serial.print( " F = ");
  Serial.print( int( lowF ) );
 
  Serial.print( "   Hi:  C = ");
  Serial.print( int( hitempflt ) );
  int hiF = (hitempflt * 9/5) + 32;
  Serial.print( " F = ");
  Serial.println( int( hiF ) );
 
 
 //  LIGHTING UP THE LED IF TOO HOT
  if ( tempflt > hitempflt )
    digitalWrite(led,HIGH);
  else
    digitalWrite(led,LOW);
   
  delay(500);
}

2 comments:

  1. Thanks, fixed it. But I think Vimeo's going to take some time to refresh the settings on their side.

    Note: the videos aren't all that great. Hopefully I'll have some better projects to upload soon.

    ReplyDelete