Showing posts with label AD22100. Show all posts
Showing posts with label AD22100. Show all posts

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.

6/24/2011

Temperature-Sensor Network with RTC and LCD Part IV

Are you ready to continue? I have to give an advance warning because the project becomes a bit confusing regarding the Fritzing-Schematics.
Nevertheless I will post the final add-on for the base station just to be complete: the 16x2 backlight LCD. If you are not sure about how to connect a 16x2 LCD there is a fantastic site which will give you full information about this topic: How to connect Arduino with a character LCD. In the meantime this is the current version of the breadboard. 
If you followed up to this point it will be no big effort to connect the last item!

Although we haven't built the remote sensor yet, I will start to show the sketch so you have a break building circuits.

Let's start with some information about the project and the configuration of the Xbees - I already mentioned the base station XBee configuration here.

Sketch Part I: project description, explanation how to configure the XBees (this is done via X-CTU and/ or terminal program):

/* REMOTE AND LOCAL TEMPERATURE SENSOR WITH:
ARDUINO UNO, TMP36, AD22100KT, RTC DS1307, TWO SERIES 2 XBEES
AND 16*2 LCD-DISPLAY
Ver 0.1
by Markus Ulsass
http://lookmanowire.blogspot.com/
*/

/*
*** XBEE CONFIGURATION ***
 

 RECEIVER: (BASE STATION)
 COORDINATOR
 ATID 2001 (PAN ID)
 ATDH 0
 ATDL 0
 ATAP 1 API mode enable
 ATSP AF0 sleep period according to longest sleep period on end device

 SENDER: (REMOTE SENSOR )
 END DEVICE
 ATID 2001 (PAN ID)
 ATDH 0
 ATDL 0
 ATD0 2   pin 0 in analog in mode with TMP36
 ATIR 3E8 sample rate 1000 millisecs (hex 3E8)
 ATSM 4   sleep mode cyclic sleep mode
 ATSP AFO sleep period (AFO = 2800 ms * 10 = 28 seconds)
 ATST 7D0 time before sleep 2 seconds (hex 7D0 = 2000 ms)
 
*/

As mentioned before you should already be familiar with the configuration of the base station ("RECEIVER"), I will now explain the settings for the remote sensor:
  • ATID 2001: We select the same PAN ID for the end device, else we would likely have serious communication problems between coordinator and end device
  • ATDH 0, ATDL 0:  We leave both destination addresses at 0 (no broadcast) to send any information directly to the coordinator
  • ATD0 2: Analog Pin 0 (physical pin 20) is set to analog input mode. Remember the XBee will look for a maximum of 1.2 volt input so you might have to use a voltage divider depending on your analog sensor
  • ATIR 3E8: The periodic I/O Sampling Rate is set to 1000 milliseconds (that's 3E8 in HEX), this means a sample is sent every second from the remote sensor to the coordinator, given that the end device is awake
  • ATSM 4: We set the Sleep Mode to "4" which means we enable the cyclic sleep mode for the XBee end device
  • ATSP AF0: The Sleep Period is set to the maximum of 28 seconds hence we set the parameter to AF0 (HEX) or 2800 milliseconds. Because the sleep period is multiplied by 10 we get our 28 seconds.
  • ATST 7D0: This value sets the Sleep Timer which decrements the given parameter before the end device falls asleep again provided there is no RF signal received. For testing purposes we set this value to 7D0 (HEX) which is 2000 milliseconds. Later we could decrease this value to save energy. For the time being I will keep this value this high, because we could easier get access to the XBee if something goes wrong.
Sketch Part II: including libraries, declaring and initializing values:

// include the library for liquid crystal code:
#include <LiquidCrystal.h>

// include the library for DS1307 RTC connected via I2C and Wire Library
#include <Wire.h>
#include <RTClib.h>

RTC_DS1307 RTC;

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(7, 6, 5, 4, 3, 2);

int debugLED = 13;            // for debugging purposes only
int analogValue = 0;          // declare and initialize analogValue for remote sensor
float temperatureInside = 0;  // declare and initialize temperatureInside for local temperature

Not a lot to explain, just the settings for the several devices connected and some values set for later use.

Sketch Part III:Setup:

void setup() {
 
  pinMode(debugLED,OUTPUT); //debugging LED set to output

  Serial.begin(9600); // start serial transmission with 9600 baud
  Serial.flush();     // flush the serial port

  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);

  // start Wire and RTC
  Wire.begin();
  RTC.begin();

  // check if RTC is running
 
  if (! RTC.isrunning())
    Serial.println("RTC is NOT running!");

Again the comments should be sufficient to explain the code. If there are any open questions regarding the language check the reference on the Arduino homepage.

I think that might be enough for this time. In the next part we will continue with the sketch and will start with the remote temperature sensor.

6/18/2011

Temperature-Sensor Network with RTC and LCD Part III

We continue with the base station and start to implement the temperature sensor.

There are a lot of temperature sensors available. You could use simple thermistors, analog temperature ICs (like TMP36 or LM335) or digital ones like the Dallas DS18X20 (X stands for S/ B). They all have their pros and cons and I like to experiment with different sensors.

Analog Devices AD22100KT
This time I chose the Analog Devices AD22100KT in a TO-92 package. It's a "Voltage Output Temperature Sensor with Signal Conditioning" and has a large temperature span of 200°C (-50°C to +150°C) a nice linearity and accuracy (according to the datasheet). The datasheet declares an initial error of 0.5°C (at Ta = 25°C, AD22100KT) and a maximum error of 2°C. That sounds much better than the LM335 (at Ta = 25° it's 2°C inital error for LM335, max. 6°C) I worked with at some higher cost for the part and with 60°C more temperature range. Voltage supply is at least 4 (up to 6) volts and quiescent current ist 650µA maximum. As we connect the sensor directly to the Arduino both values are not that important this time (it will be if we connect a temperature sensor directly to the XBee - more on that later).

Datasheet Analog Devices AD22100
You will need a 1k ohm resistor and a 0.1µF capacitor between Vout and the analog input to drive the temperature sensor.
The datasheet also gives insight into the formula we later need to calculate the temperature from our analog readout: Vout = (V+/ 5V) * [1.375V + (22.5mV/ °C) * Ta]. It's the formula where we see the sensor has a temperature coefficient of 22.5mV/ °C (that's techspeak and means the voltage changes by 22.5 millivolts per degree celsius).




The next module ist the Real Time Clock (RTC) DS1307. It's one of Dallas Semiconductor (now Maxim Integrated Products) famous clock-ICs and it's quite easy to build and program. You connect the IC via analog inputs 4 and 5 to the Arduino. That are the I2C pins of the microcontroller and you can use one of the RTC libraries that are available (you can find a great tutorial here).You only need two resistors for the SDA and SCL lines, the 32,768kHz crystal and the 3 volts backup battery (like a Lithium 2032 3 volts coin cell).

Well so far we are almost done with the base station. Please be aware that the left and right power rails of the breadboard are not connected, because the left power rails have 3.3 volts and the right rails are supplied by the 5 volt output from the Arduino UNO.

Arduino UNO with XBee, RTC DS1307 and AD22100KT
In the next part we will finish the base station by connecting the LCD and having a look into the code for the Arduino UNO. In the meantime have fun!

6/12/2011

Offroad: XBee Coordinator API + Arduino UNO + RTC +Temperature Sensor + LCD-Display AND Remote XBee (Sleep Mode with low power consumption) + Temperature Sensor - PART I

What a long headline you might think - but this actually describes very well my longer absence to the blog.

I'm sorry to be again off-road to the book, but before starting with the last Connect-Port experiment in Chapter 7 I still have some things on my to-do list that developed from the preceding chapters of the book.

With the knowledge of all the things learned until now I was eager to implement my first own little project to get a simple temperature sensor network (one outside/ one inside temperature) with a real time clock (RTC) and displaying everything on a LCD-display working.
Base Station (breadboard version)

It's consisiting of:


Base Station:
  • XBee in Coordinator API mode
  • connected to an Arduino UNO
  • Temperature sensor connected to Arduino (AD22100)
  • LCD-Display (16*2) connected to Arduino
  • Real-Time-Clock (RTC DS1307) connected to Arduino 
 Remote Sensor:
Remote Sensor
  • XBee End Device Sleep Mode
  • TMP36 temperature sensor connected
  • Voltage regulator MCP1700 (with very low quiescent current <2µA)
  • running on three AA rechargeable batteries



Part II will be on describing the Base Station.