Subscribe To Our List

We help electronics hobbyists to create projects related to Micro-controllers. Join our newsletter to get access to new projects first.
Email address
Secure and Spam free...

In this article, we are going to make a Arduino data logger in which we will use the DHT22 temperature sensor for reading the temperature and the Arduino SD card shield for storing the data into the SD card. We will first read the data from the DHT22 temperature sensor and then we will store this data into the SD card using the Arduino SD Card shield. We will then access this file from the PC and we will make a chart in the Google spread sheet using the values stored in the SD card file.

Circuit Diagram and Explanation

Connections of Arduino with SD card shield are as follows

SD Card Shield To Arduino Uno Arduino Mega 2560
CS Pin 10 Pin 53
SCK Pin 13 Pin 52
MISO Pin 12 Pin 50
MOSI Pin 11 Pin 51
VCC 5V 5V
GND GND GND

To read more about SD Card Module interfacing with Arduino, read this tutorial | Arduino SD Card Shield Tutorial

The connections of the Arduino with the DHT22 temperature and humidity sensor are as follows

  • Pin 1 of DHT22 to VCC of Arduino
  • Pin 2 of DHT22 to pin 8 of Arduino
  • Pin 4 of DHT22 to GND of Arduino

To read more about DHT22, read this Tutorial | DHT22 Arduino Interfacing

Arduino data logger using arduino sd card module

Ardiuno Data logger

Code

Download the library for DHT22 from here. No need to download other libraries, Arduino IDE have these libraries when it is installed.

#include <SD.h>
#include <SPI.h>
#include "DHT.h"
#define DHTPIN 8
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
File data_file;

int CS_pin = 10;
long sec=00;
long mint=00;
long hr=00;

void setup() {
  Serial.begin(9600);
  pinMode(CS_pin, OUTPUT);
  dht.begin(); //This command will start to receive the values from dht22
  // SD Card Initialization
  if (SD.begin())
  {
    Serial.println("SD card is initialized and it is ready to use");
  } else
  {
    Serial.println("SD card is not initialized");
    return;
  }
  }

void loop() {
  data_file = SD.open("data.txt", FILE_WRITE);
  if (data_file) {   
      getData();
    }
  // if the file didn't open, print an error:
  else {
    Serial.println("error opening data.txt");
  }
  delay(1000);
  }

void getData(){
  for(long sec = 00; sec < 60; sec=sec+2){
float temp = dht.readTemperature(); //Reading the temperature as Celsius and storing in temp
data_file.print(hr);
data_file.print(":");
data_file.print(mint);
data_file.print(":");
data_file.print(sec);
data_file.print(",  ");
data_file.println(temp);
Serial.print(hr);
Serial.print(":");
Serial.print(mint);
Serial.print(":");
Serial.print(sec);
Serial.print(",    ");
Serial.println(temp);
if(sec>=58){
  mint= mint + 1;
}
if (mint>59){
  hr = hr + 1;
  mint = 0;
  }
data_file.flush(); //saving the file
delay(2000);
}
  data_file.close(); //closing the file
}

 Code Explanation

First of all, we have initialized the libraries for SD card, SPI communications and for the DHT22 temperature and humidity sensor. Then we have defined pin 8 for the DHT22 sensor and after that we have defined the type of DHT sensor (DHT22 or DHT11). Then we have declared two variables, one for DHT22 and the other for SD card.

In the setup function, Serial.begin() will start the serial communication and the dht.begin() will start to receive values from DHT22 sensor.

The below commands will check that if the SD card is initialized or not.

if (SD.begin())
  {
    Serial.println("SD card is initialized and it is ready to use");
  } else
  {
    Serial.println("SD card is not initialized");
    return;
  }

The below commands will open the ‘data.txt’ file in the SD card if it already exist and if it is not already there, then it will create it. If it will successfully open the file then it will go in the getdata() function and if it is not successfully opened, then it will show ‘error opening data.txt’ in the serial monitor.

data_file = SD.open("data.txt", FILE_WRITE);
  if (data_file) {   
      getData();
    }
  // if the file didn't open, print an error:
  else {
    Serial.println("error opening data.txt");
  }

In the getdata() function, we have started a loop which will count the time and will show the time in hours, minutes and seconds. After that, we will read the temperature from the temperature sensor and will store the data in the temp variable. Then we will send the time and the temperature value in the SD card.

The data in the SD card will be saved when we will use the flush() function which is used for saving the file or when we will close the file using the close() function.

void getData(){
  for(long sec = 00; sec < 60; sec=sec+2){
float temp = dht.readTemperature(); //Reading the temperature as Celsius and storing in temp
data_file.print(hr);
.
.
.
data_file.flush(); //saving the file
delay(2000);
}
  data_file.close(); //closing the file

 Creating a Chart in Google Sheet

Step 1

First of all, log in to your Google account and go to ‘google.com’. From there, open the options and tab and click on drive. It will take you to the Google drive.

Step 2

In Google drive, from the My Drive menu, click on the Google Sheets. It will open a Google sheet.

Step 3

From the file menu, click on import.

Upload the file there from you SD card.

Step 4

After uploading the file, select the options as shown below.

Step 5

After that, go to the insert menu and click on chart.

Select the options as shown below and insert the chart.

If you have any questions, feel free to ask us in the comment section.

JOIN OUR NEWSLETTER
We help electronics hobbyists to create projects related to Micro-controllers. Join our newsletter to get access to new projects first.
We hate spam. Your email address will not be sold or shared with anyone else.

2 Responses

    • Aqib

      I have made the counter in the code. When the code will be uploaded in the Arduino, the counter will start.

      Reply

Leave a Reply

Your email address will not be published.

Subscribe To Our List

We help electronics hobbyists to create projects related to Micro-controllers. Join our newsletter to get access to new projects first.
Email address
Secure and Spam free...
Skip to toolbar