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

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.

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.

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.

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.