Log Temperature, Humidity, and Heat Data with Arduino to an SD Card

By Ali Hamza

In this article, we are going to make a data-logger using an Arduino Ethernet shield. The Arduino Ethernet shield has a Micro SD card slot which is used to insert the SD card in it and store the data in it. Sometimes we need to use graphics in our program like images so in that case, the SD card will come in handy for us. In our project, we are going to read the temperature, humidity, and the heat index from the DHT22 sensor and store these values in the SD card.

 

The Ethernet Shield sitting on top of our Arduino

 

Required Components

The components required for this project are as follows

 

How Does it Work?

The DHT22 sensor can measure the temperature and humidity. We will read the temperature and humidity values from the DHT22 sensor and then we will calculate the heat index using these values.

Then we will send these values to the SD card using the library functions. First, we will check to see if the SD card has initialized properly or not. If the SD card has not initialized properly, then we need to create a file in the SD card in which we will send the data. Then we will write the sensor readings one by one in the SD card file.

The file name you are going to use for the SD card file should be in the 8.3 format. An example of a file name using the 8.3 format is  “file1234.txt”. File names longer than that will create an error. If you are using the SD card for the first time, then it is recommended to format it before using it.

 

Circuit Diagram

First of all, place the Ethernet shield on the Arduino and insert the SD card in the SD card slot on the Arduino. Then make the connections for the DHT22 sensor with the Arduino as follows

  • Connect the First pin on the DHT22 which is the VCC pin to 5V on the Arduino
  • Connect the Second pin on the DHT22 which is the Data pin to pin 8 on the Arduino
  • Connect the Last pin of DHT22 which is the GND pin to GND on the Arduino

 

You can find a full-sized diagram here

Arduino Code

#include "DHT.h"

#include <SD.h>

#define DHTPIN 8

#define DHTTYPE DHT22

const int chipSelect = 4;

DHT sensor(DHTPIN, DHTTYPE);




void setup()

{

  Serial.begin(9600);

  sensor.begin(); 

  Serial.print("Initializing SD card...");

  if (!SD.begin(chipSelect)) {

    Serial.println("Card failed, or not present");

    // don't do anything more:

    return;

  }

  Serial.println("card initialized.");

  }




void loop()

{

float humidity = sensor.readHumidity(); 

float temperature_C = sensor.readTemperature();

float temperature_F = sensor.readTemperature(true);

if (isnan(humidity) || isnan(temperature_C) || isnan(temperature_F)) {

    Serial.println("Failed to read from DHT sensor!");

    return;

}

  float heat_indexF = sensor.computeHeatIndex(temperature_F, humidity);

  float heat_indexC = sensor.convertFtoC(heat_indexF);

  

  File sdcard_file = SD.open("data.txt", FILE_WRITE);

  // if the file is available, write to it:

  if (sdcard_file) {

    sdcard_file.print("Temperature in C: ");

    sdcard_file.print(temperature_C);

    sdcard_file.print("  Temperature in Fah: ");

    sdcard_file.print(temperature_F);

    sdcard_file.print("  Humidity: ");

    sdcard_file.print(humidity);

    sdcard_file.print("  Heat Index in F: ");

    sdcard_file.print(heat_indexF);

    sdcard_file.print("  Heat Index in C: ");

    sdcard_file.println(heat_indexC);

    sdcard_file.close();

    // print to the serial port too:

    Serial.print("Temperature in C: ");

    Serial.print(temperature_C);

    Serial.print("  Temperature in Fah: ");

    Serial.print(temperature_F);

    Serial.print("  Humidity: ");

    Serial.print(humidity);

    Serial.print("  Heat Index in F: ");

    Serial.print(heat_indexF);

    Serial.print("  Heat Index in C: ");

    Serial.println(heat_indexC);

    }  

  // if the file isn't open, pop up an error:

  else {

    Serial.println("error opening datalog.txt");

  } 

delay(1000);

}

 

 

Code Explanation

First of all, we included the libraries for the DHT22 temperature sensor and the SD card. Then we defined the pin where we have connected the DHT22 sensor and the type of DHT sensor. There are different types of DHT sensors available so we need to define which sensor type we are using. Then we declare a variable for the type of DHT we are using for the DHT library functions.

 

#include "DHT.h"

#include <SD.h>

#define DHTPIN 8

#define DHTTYPE DHT22

const int chipSelect = 4;

DHT sensor(DHTPIN, DHTTYPE);

 

Then in the setup function, we checked if the SD card has initialized correctly or if the SD card is inserted or not. If the card is not initialized correctly, then it will show an error on the serial monitor. Otherwise, it will print “card initialized” if it has initialized correctly.

 

if (!SD.begin(chipSelect)) {

    Serial.println("Card failed, or not present");

    // don't do anything more:

    return;

  }

 

Then we get the humidity and temperature from the D22 sensor. If the sensor doesn’t give us the values, then “Failed to read from DHT sensor!” will be shown on the serial monitor. After that, we will calculate heat index using the temperature and humidity values.

 

float humidity = sensor.readHumidity(); 

float temperature_C = sensor.readTemperature();

float temperature_F = sensor.readTemperature(true);

if (isnan(humidity) || isnan(temperature_C) || isnan(temperature_F)) {

    Serial.println("Failed to read from DHT sensor!");

    return;

}

  float heat_indexF = sensor.computeHeatIndex(temperature_F, humidity);

  float heat_indexC = sensor.convertFtoC(heat_indexF);

 

In the end, we opened the SD card using the SD.open() function and created a file there. Then we printed the temperature, humidity, and heat index in the memory card file.

After storing all the data in the SD card, it is important to close the file using the library functions. If you do not close the file, the data in it will not be saved, and the data on SD card will be lost when the card is removed.

 

File sdcard_file = SD.open("data.txt", FILE_WRITE);

  // if the file is available, write to it:

  if (sdcard_file) {

    sdcard_file.print("Temperature in C: ");

    sdcard_file.print(temperature_C);

    sdcard_file.print("  Temperature in Fah: ");

    sdcard_file.print(temperature_F);

    sdcard_file.print("  Humidity: ");

    sdcard_file.print(humidity);

    sdcard_file.print("  Heat Index in F: ");

    sdcard_file.print(heat_indexF);

    sdcard_file.print("  Heat Index in C: ");

    sdcard_file.println(heat_indexC);

Recommended Posts
Comments
  • eduardo
    Reply

    Hi! I’ve come across your project looking for ideas to implement a similar solution to monitor the greenhouse of the lab I work. I have two questions, I you can spare the time to answer of course. The first is is it possible to change the ethernet module for another one with just the SD/micro SD card slot? My idea is to download the logged data in regular intervals to generate Temperature an humidity plots over time. With that comes the second question: is it possible to program the Arduino to log the data in different files? For example, if I want to monitor the parameters over the course of 7 days, it would be easer to store the data from one week and the just download it instead of look for the files from that period of time in the middle of all the data stored.

Leave a Comment

− 1 = 1

Start typing and press Enter to search