Save values to SD card using Arduino
A memory card is a small chip that allows us to store data. It comes with different storage capacities and is used in many applications in our daily lives, in smartphones, and cameras.
In some projects, we may need to save the values or the readings that we obtain from the different sensors on the memory card in order to benefit from them in operations later.
Overview
In this tutorial we will get the temperature and humidity values from the DHT11 sensor (we explained it earlier in a tutorial, which you can see here).
Then we will use the SD card module to save the values in a TXT file. We will also draw a graph of the temperature and humidity through the Excel program, and we will learn how to display time and date using Arduino.
Let’s find out more together!

Getting the items
For this project you will need the following components you can buy them from our store.
Make sure you also have a breadboard and jumper wires to connect everything together
Wiring it up
To set up the SD card module in the correct way, follow the instructions below. The image demonstrates how to connect the wires between the SD card module, DHT11 and the Arduino. Once the SD card module, DHT111 and the Arduino are connected to each other, connect the Arduino to your computer using the USB cable.

Connections from the Arduino to the breadboard:
• Arduino GND pin → Breadboard ground line
• Arduino 5V pin → Breadboard 5V line
Connections from the DHT11:
• positive pin→ Breadboard 5V line
• Out pin → Arduino pin 3
• Negative pin → Breadboard ground line
Connections from the SD card module:
• GND pin → Breadboard ground line
• VCC pin → Breadboard 5V line
• MISO pin→ 12
• MOSI pin→ 11
• SCK pin → 13
• CS pin → Arduino pin 4
Coding
The purpose of this sketch is to read the temperature in Celsius degrees as well as the humidity of the air in percentage from the DHT11 sensor. Then save the values in a TXT file in the SD card memory every 30 seconds.
Using a time library, you will be able to save the date and time of each reading. With the help of this library, you can obtain the time and date. However, you will have to reset it every time you disconnect the power from the Arduino.
In order for the code to work correctly, you need to download some libraries. Libraries are files that you can download and copy to the Arduino IDE software files so the Arduino can recognize different sensors. You can download the library files from the resources section and then install it by following this tutorial.
Let's look at the code, and then you should be able to understand it once you follow the instructions and read the comments.
/*
Voltaat learn (http://learn.voltaat.com/)
Link for full tutorial: https://bit.ly/3FQzjJL
SD Library: https://bit.ly/3tsiei1
DHT11 Library: https://bit.ly/3DK4YK5
Time Library: https://bit.ly/3DLoiGP
Tutorial: Save values to SD card using Arduino
The purpose of this sketch is to read the temperature
in Celsius degrees as well as the humidity of the air
in percentage from the DHT11 sensor.
Then save the values in a TXT file
in the SD card memory every 30 seconds.
Connections from the Arduino to the breadboard:
• Arduino GND pin → Breadboard ground line
• Arduino 5V pin → Breadboard 5V line
Connections from the DHT11:
• positive pin→ Breadboard 5V line
• Out pin → Arduino pin 3
• Negative pin → Breadboard ground line
Connections from the SD card module:
• GND pin → Breadboard ground line
• VCC pin → Breadboard 5V line
• MISO pin→ 12
• MOSI pin→ 11
• SCK pin → 13
• CS pin → Arduino pin 4
*/
//This library allows you to communicate with SPI devices
#include < SPI.h >
//include the SD library
#include < SD.h >
//include the time library
#include < TimeLib.h >
//include the DHT sensor Library
#include < DHT.h >
//Define Parameters to DHT function, 3 Refers to digital pin 3 in arduino which you can change with any other digital pin, DHT11 is the sensor type.
DHT dht(3, DHT11);
//Define two variables for Temperature and Humidity.
float Temperature, Humidity;
//Define SD card module cs to arduino digital pin 4
const int cs =4;
// Commands inside void setup run once.
void setup()
{
//Initialize the DHT sensor with the begin() method.
dht.begin();
//Start the serial monitor at 9600 baud rate (9600 bits per second)
Serial.begin(9600);
//Set your current time so that arduino can start from it as real time clock
//SetTime(hr,min,sec,day,mnth,yr);
setTime(9,38,50,1,8,2022);
//Function we made to see if the card is present and can be initialized
Initialize_SDcard();
}
//Commands inside void loop run forever
void loop()
{
//Fuction we made to print time and date at each line besides values from DHT11 sensor
dateAndTime();
//Function we made to read values from DHT11 sensor and print them to serial monitor
Read_DHT11();
//Function we made to create a TXT file and save the output to it in the SD card memory
Write_SDcard();
//Function we made to see if the card is present and can be initialized
Initialize_SDcard();
//delay to get reading from the sensor every 30 seconds
delay(30000);
}
void dateAndTime()
{
//Set the system time to the give time t
time_t t =now();
//Print to the serial monitor
Serial.print("Time now: ");
Serial.print(hour());
Serial.print(":");
Serial.print(minute());
Serial.print(":");
Serial.print(second());
Serial.print(" Date: ");
Serial.print(day());
Serial.print("/");
Serial.print(month());
Serial.print("/");
Serial.print(year());
}
void Read_DHT11()
{
//Read the Temperature value from the sensor
//You can use Temperature = dht.readTemperature(True); to print the temperature value in Fahrenheit.
Temperature = dht.readTemperature();
//Read the Humidity value from the sensor
Humidity = dht.readHumidity();
//Print to the serial monitor
Serial.print(" Temperature = ");
Serial.print(Temperature);
Serial.print(" ,Humidity = ");
Serial.print(Humidity);
Serial.println("%");
}
void Initialize_SDcard()
{
// see if the card is present and can be initialized
if(!SD.begin(cs))
{
Serial.println(" Card failed, or not present");
// don't do anything more:
return;
}
}
void Write_SDcard()
{
//Open the file Keep in mind that only one file can be opened at once
//Consequently, you must close current one before starting a new one
File dataFile =SD.open("LoggerCD.txt", FILE_WRITE);
//If the file is available, write to it
if(dataFile)
{
//Set the system time to the give time t
time_t t =now();
//Write on the TXT file
dataFile.print("Time now: ");
dataFile.print(hour());
dataFile.print(":");
dataFile.print(minute());
dataFile.print(":");
dataFile.print(second());
dataFile.print(" Date: ");
dataFile.print(day());
dataFile.print("/");
dataFile.print(month());
dataFile.print("/");
dataFile.print(year());
dataFile.print(" Temperature: ");
dataFile.print(Temperature);//Store date on SD card
dataFile.print(" ,");//Move to next column using a ","
dataFile.print("Humidity ");
dataFile.print(Humidity);//Store date on SD card
dataFile.print("%");
dataFile.println();//End of Row move to next row
dataFile.close();//Close the file
}
else
//Print to serial monitor
Serial.println(" SD card writing failed");
}
The serial monitor is a great tool that can facilitate communication between the computer and the Arduino. It allows us to send and receive different commands as well as view information directly from the Arduino.
Now, as we see in the following image, the serial monitor displays the current time and date, along with temperature and humidity values. The value is updated and printed every 30 seconds because of the delay we added in our code.
You should also make sure you have chosen the right baud rate (9600) as specified in the code.

To obtain some values, you can wait for a period of time, like 30 minutes, and then connect the micro-SD card to your computer.
You will find a TXT file on it with the name we specified on the code, LOGGERCD.txt. When you open it, you will see the same data we have printed through the serial monitor.

If you remove your SD card while the code is running, you will get an error message with the status of the SD card through the serial monitor.

We can import the TXT file into Excel to create a chart between time and temperature and also between time and humidity. This will be very useful to see and understand what really happens to temperature and humidity over time, We can also recognize the relationship between temperature and humidity, which is an inversely proportional relationship.


If you have data for several hours or days, it will be quite useful since you may use it to do some analysis, such as finding the warmest day or the average daily temperature.
Related Tutorials
In this simple tutorial, we will use the Arduino to print the temperature and humidity values on your computer using a suitable sensor called the DHT11.
LCDs are used in a range of everyday applications, including the automobile radio and the house air conditioning remote. They display data and let you control it through menus.
A soil moisture sensor is a device that measures the amount of water in the soil. The sensor consists of two metal probes that can be inserted into the soil to measure the moisture levels.