Sunday, April 28, 2013

Temperature logger for Arduino to persistent memory

At work, we had some troubles to regulate our heating. So the need arose to measure the temperature for some time. Here we describe how to use an Arduino kit for measuring and logging multiple temperatures. Readings are saved to either your Arduino's persistent EEPROM memory, or to an optional SD card.


On the net you'll find of lot of similar projects. Our focus was on the following:
  • Use Arduino's persistent memory so your data won't get lost in case power is removed. Optionally log to a CSV file on a SD card, if available.
  • Efficient on local storage. I tried to cram as much samples to persistent memory as possible, as it's rather limited. The project stores two sample into three bytes with an accuracy of 0,1 °C. On a Arduino Uno, you can store about 700 samples. Of course, if you use an SD card, memory is virtually unlimited.
  • Source code available and highly customizable like:
    • number of sensors
    • use of a display or not
    • use an a real time clock
    • use of a SD card, or not

Hardware

Temperature sensor

The TMP36 temperature sensor needs a stable reference voltage. That's why we take the 3.3V provided by the Arduino board, which fluctuates less then the 5V. The analog output of the TMP36 is converted by Arduino's ADC at a resolution of 10 bits (1024 values). That's another reason why 3.3V is preferable over 5V.

Special caution has to be taken when you need to read out multiple sensors. The TMP36 has a high impedance output. Because Arduino's AD convertor is multiplexed over a its analog inputs, after switching ADC ports, your first reading won't be accurate. So you need to read the same sensor twice with a small delay of say 10 msec. This gives the circuit time to adjust to the sensor just selected.

You need to calibrate the sensor using ice water (0 degrees Celsius). If you own a accurate multi meter, use it to set the exact 3.3V value in the source code.

A more precise solution would be to use a digital temperature sensor.

To get a bit more robust sensor, we soldered the TMP36 to some cable, used hot melt glue to get the pins isolated and fixed (prevents short cuts), and some heat shrink to make it a bit more stiff. Pictures will follow. If you use a long cable, you need to take extra precaution to filter out noise.

Real time clock

For logging, it would be nice to store date/times also. For this you can use a RTC breakout board. A poor man's solution is to set the date of the board to the compilation that of your program. Check out the HAS_RTC define in our source code.

EEPROM

We pack samples into our (non SD card) memory. The TMP36 measures temperatures ranging from -40°C upto 150°C. We want to save temperatures at a resolution of 0.1°C (although the TMP36 is not that precise.)
As RAM and EEPROM storage is rather limited, we want to store temperatures as compact as possible. We need values up to (40 + 150) * 10 = 1900. For this we need 11 bits (2 ^ 11 = 2048). So we store two temperature readings in 3 bytes (24 bits.)
The first two (most significant) bits define the contents of the other 22 bits:
   11: free, unallocated memory
   10: a date time value (see below)
   01: both 11 bits slots are filled with temperatures
   00: only the first slot is filled with a temperature
To erase EEPROM, we fill it completely with 255 (0xff.)

We store dates from 2013-1-1. Suppose you want to store dates up to 2021-1-1, you need to sample no more than once a seconds in order to be able to save it in a 22 bit slot: 
   8 * 365 * 24 * 60 / (2 ^ 22) minutes = 1 minute


The EEPROM can be written (erased) relative few times (say 100.000) before it start to get unreliable. So to be nice to your EEPROM, we start filling our samples in memory at a random location. In case we have a real RTC, we use the time to initialize the random generator. Otherwise we use an unused (floating) analog input to get some random noise.

Version without SD card

This a circuit diagram of a simple setup with a single sensor and without SD card. It does use a RTC and display to show the current temperature. It can be build the Arduino Starter Kit.


Wired it like this:


Version with SD card

We also build a version with an SD card shield from Adafruit. This shield also has a RTC on board. Pictures will follow.

Code 

Source code is available from GitHub. At least you need the TempLogger subdirectory. Also you need the RTClib.h library, freely downloadable.

Although not much commented, it is well structured. Normally you only need to check and adjust a few defines of the first few pages.

We marked some functions as inlined as these functions are called only from one location in our code. That way we save a few bytes. Not really important actually.

1 comment:

Unknown said...

Hi,
This blog is very nice. It is more impressive blog because your blog is related temperature logger. Thanks a lot about giving more information related termperature logger.

temperature logger