Monday, March 11, 2013

Building a SD Card Temperature logger (ATMEGA328 + SDCARD + LM35)

As we built an Arduino powered web server in previous post, let’s build a temperature logger using a SD card this time. I build this because I wanted to see how we can use a SD Card for our projects. The beauty of Arduino libraries are they have a FAT16/FAT32 library. So, we can use a large SD Card without any problem provided that the AVR you use has enough RAM & programming memory. Here I’m using ATMEGA328 which has 2kB RAM and 32kB program memory.

SD Card module:

Here I’m using a SD Card holding module I got a year back from ebay for $1.6. You can even use a microsd card with a microSD to SD card adaptor. Just you have to solder some wires to the adaptor.SD Card pin out is available at pinout.ru site. SD Cards are also using SPI protocol for the communication.

Arduino Board:

I’m using my Techduino Pro board which I build on an earlier post along with the my modified CP2102 USB to UART board. Please note that my Arduino is working at 3.3V.

LM35 Temperature Sensor:

This is a typical analog temperature and you can find the datasheet from here. It has 10mV/Celsius linear scale factor and works at minimum of 4V for full range. According to the datasheet it can be used at less voltages but the measuring temperature range will be very low (check the Minimum Supply Voltage vs. Temperature curve).

Connections:

I connected the two boards as follows to establish a SPI link between two,

SD Card Module Arduino Pro
3.3V 3.3V
GND GND
MOSI Pin 11
MISO Pin 12
SCK Pin 13
CS Pin 10



DSCF5755
DSCF5751

I connected LM35 to the Arduino to get temperature. I connected LM35’s Vout pin to Arduino’s analog input 1 (A0). I also gave power supply by connecting +5V and GND to LM35. I powered up this using the USB power itself and so common ground maintained between Arduino and LM35.

DSCF5752

Code:

I’m here going to use ATMEGA328’s built in 10bit ADC with the Vref=3.3V(Vcc of the uC). You can learn accessing ADC on Arduino by reading this article. The SD card is accessed using the built in SD card library on Arduino. You can learn more about that using this link. As I previously mentioned, LM35 has has 10mV/Celsius linear scale factor. So we have to do a little calculation to obtain the temperature. Here I’m using the full scale ADC method to get the ADC voltage.



So we get total factor as 0.32258 and we have to multiply is by ADC reading to get the temperature value.Here is the final code for the whole thing,

#include <SPI.h>
#include <SD.h>
File myFile;
int x=0;
int tempValue;
void setup()
{
pinMode(10, OUTPUT);
SD.begin(10);
}
void loop()
{
  tempValue=analogRead(A0);
myFile = SD.open("temp.txt", FILE_WRITE);
if (myFile) {
      myFile.print("Temperature ");
      myFile.print(x);
      myFile.print(":");
      myFile.println((long)tempValue*0.32258);
}
    myFile.close();
    x++;
    delay(1000);
  } 

Testing:

I hooked up everything and uploaded the compiled code. The SD card was inserted too. Then I checked the results by reading the SD Card using computer after some time. The results were impressive.

TempLog

2 comments: