Maker Pro
Arduino

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

March 21, 2018 by A .
Share
banner

Use an Arduino to make a data logger to collect temperature, humidity, and heat data.

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 

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

View the full-size schematic

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);

Author

Avatar
A .

For custom projects, hire me at https://www.freelancer.pk/u/Muhammadaqibdutt

Related Content

Comments


You May Also Like