7/27/2011

Temperature-Sensor Network with RTC and LCD Part V

Wow. More than a month since my last entry. Time is flying by. About 50 days since I started the project and my rechargeable batteries (I had to change the first three after a few days, because one cell was dead) still work in my outside sensor. My RTC is wrong by about 1.6 seconds per day and the deviation is very constant so this error can be erased by software. To compare the temperatures I used a Clock with inside/ outside temperatures I bought some time ago. The temperatures were accurate and so I finally assembled a device which I can buy for about 20 Euros in any electronics store. You might ask why so much effort for such little outcome? Wait for adding an ethernet shield, more Xbees, door bell/ mail alert, light and rain indicators and so on. And everything is broadcasted in the network!

LCD display with date, time, inside- and outside-temperature
For the last part there is unfortunately a lot of code to sift through. I made as many comments as possible - if there are any questions just put it in a comment on the page.

void loop() {

  DateTime now = RTC.now(); // new RTC object named "now"
 
  // get time and date from the RTC DS1307

  int hour = now.hour();
  int minute = now.minute();
  int second = now.second();

  // print time and date on the first line of the LCD
  // and make some adjustments regarding leading zeros
  // time display will change every second

    lcd.setCursor(0, 0);

  if (hour < 10) {
    lcd.print("0");
    lcd.print(hour,DEC);
  }
  else {
    lcd.print(hour,DEC);
  }
  lcd.print(":");
  if (minute < 10){
    lcd.print("0");
    lcd.print(now.minute(), DEC);
  }
  else {
    lcd.print(now.minute(), DEC);
  }
  lcd.print(":");
  if (second < 10){
    lcd.print("0");
    lcd.print(now.second(), DEC);
  }
  else{
    lcd.print(second, DEC);
  }

  int month = now.month();

  lcd.print("  ");
  lcd.print(now.day(),DEC);
  lcd.print(".");
  if (month < 10) {
    lcd.print("0");
    lcd.print(now.month(), DEC);
    lcd.print(".");
  }
  else {
    lcd.print(now.month(), DEC);
    lcd.print(".");
  }

  // wait one second to show off as a "real" clock ;)
  delay (1000);

  // here we are reading the serial port, that means everything that comes from the XBee Coordinator
  // the packet size (I/O Data Sample) is expected to be at least 23 bytes
  if (Serial.available() >= 23) {

    // look for the start byte, in this case I chose part of XBee end device address for easy parsing
    // this is a quick and dirty solution and you are welcome to make a better code for this
    // and of course you have to adjust the code for your XBee end device
    if (Serial.read() == 0x6F) {

      // blink debug LED to indicate when data is received
      digitalWrite(debugLED, HIGH);
      delay(50);
      digitalWrite(debugLED, LOW);

      // read the variables that we're not using out of the buffer
      for (int i = 0; i<9; i++) {
        byte discard = Serial.read();
      }
      // read the two bytes for the analog value
      int analogHigh = Serial.read();
      int analogLow = Serial.read();

      // combine high and low analog value, because it's in two bytes
      analogValue =  analogLow + (analogHigh * 256);

      // calculate temperature from TMP 36
      // 1.2V/ 1024 (10 bit ADC), 0°C is 500 mV, 10mV/ °C temperature coefficient
      float temperature = (((1.171875*analogValue)-500)/10);

      // we take 10 analog temperature samples and calculate the average

      temperature_sample=0; // reset temperature_sample value to zero

      for (int i=0; i<10; i++) {
        // calculate the temperature from the AD22100K
        // 5V/ 1024 (10 bit ADC), 1.375 V at =0°C, 22.5mV temperature coefficient per °C
        temperature_read = (analogRead(0)*0.0048828125 - 1.375) / 0.0225;
        temperature_sample = temperature_sample + temperature_read;
      }
      temperatureInside = (temperature_sample/10);

      // print inside and outside temperature in the second row of the LCD
      // display will only change when data is received

      lcd.clear();

      lcd.setCursor(0, 1);
      lcd.print("IN ");
      lcd.print(temperatureInside,1);
      lcd.print(" OUT ");
      lcd.print(temperature,1);

    }
  }
}


There are still some things to consider and to change in the future. There is for example no adjustment for negative temperatures in the display, the parsing with the XBee address could be improved, the back light could be adjusted by software.

4 comments:

  1. I hope you return to blogging on all this. I'm curious how things have evolved since your last post. You seemed to have jumped to the MCP1700 regulator and 3 x AA, which is what I've been planning, so I feel more confident.

    Do you have a sensor net running full time now?

    It was kind of you to post in English, thanks.

    ReplyDelete
  2. The sensor net is running about one year now with the circuits and software presented in the blogs. I changed some hard- and software - substituted 16*2 with 20*4 LCD-display, added a timetable for the local traffic and improved some XBee settings.

    The next version will be with a 3V6 Lithium (AA or 1/2 AA) battery where no LDO-voltage regulator is needed and a self etched pcb to make the sensor much smaller.

    ReplyDelete
  3. Markus...Thanks for sharing all your great work. I'm curious why you don't use xbee.h in your sketches. You might now since its been a while. I'm attempting a similar project, but with different sensor types.

    Thanks

    ReplyDelete
  4. Hi u3sbsi2i,

    I like to program the µC/ XBees from scratch without any libraries. I think that's where you learn the most if you have the time to do so.

    Markus

    ReplyDelete