Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
Arduino
120 min
Share

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.

Project Video

Overview

In this tutorial we will get the temperature and humidity values from the DHT11 sensor. 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.

Getting the Items

Arduino Uno R3 (Voltaat Version)
Get Item
DHT11 Temperature & Humidity Sensor
Get Item
MicroSD Card Module
Get Item
Half-size Breadboard
Get Item
Jumper Wires – Male to Female (40 Pack)
Get Item
Jumper Wires - Male to Male (40 Pack)
Get Item

Steps

Wiring it Up

Connect the wires between the soil moisture sensor and the Arduino, as shown in the image below.


 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




/*
 Voltaat learn (http://learn.voltaat.com/)
 Link for full tutorial:
 SD Library:
 DHT11 Library:
 Time Library:

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

Testing it Out

know when your plant needs watering

Now access the serial monitor on your Arduino IDE by clicking on the magnifying glass icon at the top right corner.

know when your plant needs watering

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.

know when your plant needs watering

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.

know when your plant needs watering

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.

know when your plant needs watering

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.

know when your plant needs watering

know when your plant needs watering

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.

Resources

No items found.