In this article, you are going to learn about Arduino SD card data logging. We will use the LM35 temperature sensor to get the temperature value, and the DS3231 module to get the time and date. Then we will use the SD card module to open the SD card, and we will enter the date, time, and temperature in the SD card file.

Circuit Diagram

First of all, connect the SD card module to the Arduino. The SD card module works with the Arduino through the SPI communication. The SPI pins on the Arduino are pins 10, 11, 12, and 13.

The connections of the OLED with the Arduino are as follows:

Connect CS of SD card module to pin 10 of Arduino

Connect MOSI of SD card module to pin 11 of Arduino

Connect MISO of SD card module to pin 12 of Arduino

Connect SCK of SD card module to pin 13 of Arduino

Connect VCC of SD card module to 5V pin of Arduino

Connect GND of SD card module to GND pin of Arduino

After that, connect the DS3231 module with the Arduino. The DS3231 module works with the Arduino through the I2C communication. The pins for I2C communication on the Arduino are SDA and SCL.

Connect the DS3231 module with the Arduino as follows:

Connect GND of DS3231 to GND pin of Arduino

Connect VCC of DS3231 to 5V pin of Arduino

Connect SDA of OLED to A4 pin of Arduino

Connect SCL of OLED to A5 pin of Arduino

In the end, connect the LM35 sensor with the Arduino. Connect the left pin of LM35 to 5V of Arduino, middle pin to A0 of Arduino, and the left pin to ground of the Arduino.

Code Explanation

First, include the libraries for the SD card and for the DS3231 RTC module. The SD card works with the Arduino through the SPI communication, so we have included the SPI library.

#include <SD.h>
#include <SPI.h>
#include <DS3231.h>

Then we initialize an object “sdcard_file” of type File, which will help us use the functions of the library.

After that, we define the pins where we have connected the CS pin of SD card module and the lm35 sensor pin.