Arduino Temperature Data Logger with SD Card Module

This post shows you how to create a temperature Arduino data logger. We’ll use the DHT11 to measure temperature, the real time clock (RTC) module to take time stamps and the SD card module to save the data on the SD card.

Recommended resources:

Parts required

Here’s a complete list of the parts required for this project:

Build 25 cool Arduino projects with our course even with no prior experience DOWNLOAD »

Build 25 cool Arduino projects with our course even with no prior experience DOWNLOAD »

Note: alternatively to the SD card module, you can use a data logging shield. The data logging shield comes with built-in RTC and a prototyping area for soldering connections, sensors, etc..

You can use the preceding links or go directly to MakerAdvisor.com/tools to find all the parts for your projects at the best price!

Schematics

The following figure shows the circuit’s schematics for this project.

Note: make sure your SD card is formatted and working properly. Read “Guide to SD card module with Arduino“.

Installing the DHT sensor library

For this project you need to install the DHT library to read from the DHT11 sensor.

  1. Click here to download the DHT-sensor-library. You should have a .zip folder in your Downloads folder
  2. Unzip the .zip folder and you should get DHT-sensor-library-master folder
  3. Rename your folder from DHT-sensor-library-master to DHT
  4. Move the DHT folder to your Arduino IDE installation libraries folder
  5. Finally, re-open your Arduino IDE

Code

Copy the following code to your Arduino IDE and upload it to your Arduino board.

/*
 * Rui Santos 
 * Complete Project Details https://randomnerdtutorials.com
 */

#include <SPI.h> //for the SD card module
#include <SD.h> // for the SD card
#include <DHT.h> // for the DHT sensor
#include <RTClib.h> // for the RTC

//define DHT pin
#define DHTPIN 2     // what pin we're connected to

// uncomment whatever type you're using
#define DHTTYPE DHT11   // DHT 11 
//#define DHTTYPE DHT22   // DHT 22  (AM2302)
//#define DHTTYPE DHT21   // DHT 21 (AM2301)

// initialize DHT sensor for normal 16mhz Arduino
DHT dht(DHTPIN, DHTTYPE);

// change this to match your SD shield or module;
// Arduino Ethernet shield and modules: pin 4
// Data loggin SD shields and modules: pin 10
// Sparkfun SD shield: pin 8
const int chipSelect = 4; 

// Create a file to store the data
File myFile;

// RTC
RTC_DS1307 rtc;

void setup() {
  //initializing the DHT sensor
  dht.begin();

  //initializing Serial monitor
  Serial.begin(9600);
  
  // setup for the RTC
  while(!Serial); // for Leonardo/Micro/Zero
    if(! rtc.begin()) {
      Serial.println("Couldn't find RTC");
      while (1);
    }
    else {
      // following line sets the RTC to the date & time this sketch was compiled
      rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
    }
    if(! rtc.isrunning()) {
      Serial.println("RTC is NOT running!");
    }
    
  // setup for the SD card
  Serial.print("Initializing SD card...");

  if(!SD.begin(chipSelect)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");
    
  //open file
  myFile=SD.open("DATA.txt", FILE_WRITE);

  // if the file opened ok, write to it:
  if (myFile) {
    Serial.println("File opened ok");
    // print the headings for our data
    myFile.println("Date,Time,Temperature ºC");
  }
  myFile.close();
}

void loggingTime() {
  DateTime now = rtc.now();
  myFile = SD.open("DATA.txt", FILE_WRITE);
  if (myFile) {
    myFile.print(now.year(), DEC);
    myFile.print('/');
    myFile.print(now.month(), DEC);
    myFile.print('/');
    myFile.print(now.day(), DEC);
    myFile.print(',');
    myFile.print(now.hour(), DEC);
    myFile.print(':');
    myFile.print(now.minute(), DEC);
    myFile.print(':');
    myFile.print(now.second(), DEC);
    myFile.print(",");
  }
  Serial.print(now.year(), DEC);
  Serial.print('/');
  Serial.print(now.month(), DEC);
  Serial.print('/');
  Serial.println(now.day(), DEC);
  Serial.print(now.hour(), DEC);
  Serial.print(':');
  Serial.print(now.minute(), DEC);
  Serial.print(':');
  Serial.println(now.second(), DEC);
  myFile.close();
  delay(1000);  
}

void loggingTemperature() {
  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  // Read temperature as Celsius
  float t = dht.readTemperature();
  // Read temperature as Fahrenheit
  //float f = dht.readTemperature(true);
  
  // Check if any reads failed and exit early (to try again).
  if  (isnan(t) /*|| isnan(f)*/) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }
  
  //debugging purposes
  Serial.print("Temperature: "); 
  Serial.print(t);
  Serial.println(" *C");
  //Serial.print(f);
  //Serial.println(" *F\t"); 
  
  myFile = SD.open("DATA.txt", FILE_WRITE);
  if (myFile) {
    Serial.println("open with success");
    myFile.print(t);
    myFile.println(",");
  }
  myFile.close();
}

void loop() {
  loggingTime();
  loggingTemperature();
  delay(5000);
}

View raw code

In this code we create a loggingTime() function and a loggingTemperature() function that we call in the loop() to log the time and temperature to the DATA.txt file in the SD card.

Open the Serial Monitor at a baud rate of 9600 and check if everything is working properly.

Getting the data from the SD card

Let this project run for a few hours to gather a decent amount of data, and when you’re happy with the data logging period, shut down the Arduino and remove the SD from the SD card module.

Insert the SD card on your computer, open it, and you should have a DATA.txt file with the collected data.

You can open the data with a text editor, or use a spreadsheet to analyse and process your data.

Download our Free eBooks and Resources

The data is separated by commas, and each reading is in a new line. In this format, you can easily import data to Excel or other data processing software.

Wrapping up

This is a great project to learn how to use the SD card module with Arduino to build a data logger. You can apply this concept in pretty much any project you’d like.

If you like Arduino projects, make sure you check our latest Arduino course: Arduino Step-by-step Projects – Build 23 Projects

We hope you’ve found this project useful.

Thanks for reading.


Learn how to program and build projects with the ESP32 and ESP8266 using MicroPython firmware DOWNLOAD »

Learn how to program and build projects with the ESP32 and ESP8266 using MicroPython firmware DOWNLOAD »


Enjoyed this project? Stay updated by subscribing our weekly newsletter!

19 thoughts on “Arduino Temperature Data Logger with SD Card Module”

  1. Hi, Earlier I have uploaded temperature data to cloud but this one I never tried. Will follow your tutorial first with Arduino then will do that with ESP.
    Thanks for your excellent posts.- Anupam

  2. Rui, I could not get this sketch to work initially. After a lot of ‘head scratching’, I discovered that only the RTClib library from Adafruit would work. Other versions could not find the RTC module.

    • Hi.
      Yes, you can add an RTC.
      We don’t have any tutorial with that specific RTC.
      But you can take a look at DS1302 RTC module tutorial on the Arduino official website: playground.arduino.cc/Main/DS1302
      I hope this helps 🙂
      Regards,
      Sara 🙂

  3. it was a great post, was just looking for something like this, can we get date & time as strings with a DS1307, thank u

  4. Hi and Obrigado for your project!
    Inadvertently I purchased the DHT22 which only has 3 pins whereas the DHT 11 has 4. Is it possible to use the DHT22 and if so could you tell me what connections I should make? This is what I bought:
    amazon.de/DHT11-Digitaler-Temperatursensor-und-Feuchtigkeitssensor/dp/B07MN7TMXH?SubscriptionId=AKIAJQUMBT74S4ZKCBTA&tag=makeradviso01-21&linkCode=xm2&camp=2025&creative=165953&creativeASIN=B07MN7TMXH

    • Hi Michael.
      Yes, you can use that one.
      Usually those modules have the following pinout from left to right:
      VCC – Data – GND

      You can connect the data pin directly to an Arduino pin. You don’t need the resistor, because these modules already have a resistor built-in.
      Regards,
      Sara

  5. Hi Sara,
    Everything is working perfectly (including measuring humidity) although my 3-pin sensor still seems to need the resistor.
    I have now removed the Uno and installed a Nano Every as I would like to design a circuit board (with Fritzing) and make it completely stand-alone.

    But … for my project I need to add another two sensors. I will be measuring temperature inside and outside (2 sensors) and water temperature (3rd sensor, probably DS18b20 Waterproof Temperature Sensor). So I have a couple of questions:

    1 – Can I add two extra sensors, and if so, which pins should I connect them to. Would 3 and 5 be OK? Is it critical?

    2 – What would you suggest is the maximum length cable for the sensors? The board will be in a swimming pool area with the water sensor nearby, so no problem there. But how far away can I put the outside temperature sensor – is 5 metres too far?

    3 – Would it be a good idea to use a 12v power supply (through VIN) rather than 5 volt through the USB port?

    Your project has inspired me and I’m learning a lot from it – thank you so much!

    Michael

    • Hi Michael.
      You should have no problem adding more sensors. Those pins seem good to me. But you need to experiment and see how the board behaves.
      As for the maximum length, that is something that you have to try yourself. Very long cables introduce noise in your readings. But you really need to experiment to see the results.
      You can power the Arduino through the VIN pin using 6 to 12V power source. So, you should have no problem.

      I’m glad you’re learning from our tutorials.
      Thank you so much for following our work.
      Regards,
      Sara

Leave a Comment

Download our Free eBooks and Resources

Get instant access to our FREE eBooks, Resources, and Exclusive Electronics Projects by entering your email address below.