Arduino Temperature Data Logger with SD Card

This Arduino project shows how to build a temperature data logger using SD card, DS18B20 digital temperature sensor and DS3231 real time clock board.
The Arduino reads temperature from the DS18B20 sensor and saves them (with date and time) to a text file stored on the SD card. The DS3231 real time clock chip is used to get time and date information.
Time, date and temperature values are displayed on 20×4 LCD screen and sent to PC serial monitor.

To see how to interface the Arduino with SD card, visit the following post:
Arduino and SD card example – Read and write files

To see how to interface Arduino with DS18B20 sensor, take a look at this post:
Digital thermometer using Arduino and DS18B20 sensor

Hardware Required:
This is a list of all components required to build this project.

  • Arduino UNO board   —-> Atmega328P datasheet
  • micro SD card with FAT16 or FAT32 file system
  • micro SD card module
  • 20×4 LCD screen
  • DS18B20 temperature sensor    —->   datasheet
  • DS3231 module   —->  DS3231 datasheet
  • 4.7k ohm resistor
  • 330 ohm resistor
  • 2 x push button
  • 10k ohm variable resistor or potentiometer
  • 3V coin cell battery
  • Breadboard
  • Jumper wires

Arduino data logger with SD card, DS18B20 sensor, DS3231 RTC and 20x4 LCD

Arduino temperature data logger with SD card circuit:
The image below shows project hardware circuit diagram.

In this project I used micro SD card module, this module is supplied from circuit 5V source that comes from the Arduino UNO board. This module contains AMS1117-3V3 voltage regulator which is used to supply the SD card with 3.3V. Also this module contains an IC which is 74LVC125A and it’s used as level translator (from 5V to 3.3V).

SD card Arduino data logger DS18B20 DS3231 LCD

The microSD card module is connected to the Arduino as follows (from left to right):
The first pin of the micro SD card module (GND) is connected to Arduino GND.
The second pin of the micro SD card module (VCC) is connected to Arduino 5V.
The third pin of the micro SD card module (MISO) is connected to Arduino digital pin 12.
The fourth pin of the micro SD card module (MOSI) is connected to Arduino digital pin 11.
The fifth pin of the micro SD card module (SCK) is connected to Arduino digital pin 13.
The last pin of the micro SD card module (CS) is connected to Arduino digital pin 10.

The digital pins 10, 11, 12 and 13 are hardware SPI module pins of ATmega328P microcontroller (Arduino UNO microcontroller).

The DS3231 RTC module SDA (serial data) and SCL (serial clock) pins are respectively connected to Arduino A4 and A5 pins (ATmega328P hardware I2C module pins).
VCC is connected to Arduino 5V and GND is connected to Arduino GND.

The DS18B20 sensor has 3 pins (from right to left): VCC (or VDD), data and GND where:
VCC (VDD): sensor power supply pin, connected to Arduino 5V pin,
data pin: connected to Arduino analog pin 3 (A3) and
GND: connected to Arduino GND pin.
A pull-up resistor of 4.7k ohm is required because the DS18B20 output is open drain.

The 20×4 LCD screen (4 rows and 20 columns) is used to display time, date and temperature where:
RS —> Arduino digital pin 2
E   —> Arduino digital pin 3
D4 —> Arduino digital pin 4
D5 —> Arduino digital pin 5
D6 —> Arduino digital pin 6
D7 —> Arduino digital pin 7
VSS, RW, D0, D1, D2, D3 and K are connected to Arduino ground,
VEE to the 10k ohm variable resistor (or potentiometer) output,
VDD to Arduino 5V and A to Arduino 5V through 330 ohm resistor.

VEE pin is used to control the contrast of the LCD. A (anode) and K (cathode) are the back light LED pins.

In the circuit there are two push buttons: B1 and B2, they are respectively connected to Arduino analog pins 1 (A1) and 2 (A2), these buttons are used to set time and date of the real time clock.

Arduino temperature data logger with SD card code:
To be able to compile project Arduino code with no error, a driver (library) for the DS3231 RTC is required, download link is below:
Adafruit RTC library                   —->  direct link

After the download, go to Arduino IDE —> Sketch —> Include Library —> Add .ZIP Library … and browse for the .zip file (previously downloaded).

Programming hints:
There are 3 libraries included in the Arduino code as shown below.
The first library is for the SD card,
the 2nd one is for the LCD display,
and the last library is for the DS3231 RTC.

The connection of the 2 push buttons and the DS18B20 sensor data pin are defined in the code as:

The microcontroller reads temperature values from the DS18B20 sensor and saves them (with time and date) to the SD card every 10 seconds, for that I used the following if condition:

Functions used in the code:
SD Card functions:
SD.begin(): this function initializes the SD card as well as the file system (FAT16 or FAT32), it returns 1 (true) if OK and 0 (false) if error.

SD.exists(“Log.txt”): tests whether the file “Log.txt” exists on the SD card, returns 1 if the file already exists and 0 if not.

SD.open(“Log.txt”, FILE_WRITE): opens the file “Log.txt” and moves the cursor to the end of the file. This function will create the file if it doesn’t already exist. Returns 0 if error.

dataLog.close(): closes the file associated with dataLog.

DS3231 Functions:
bool debounce (): this function is for button B1 debounce, returns 1 if button is debounced.

void RTC_display(): displays day of the week, date and time on the display.

byte edit(byte parameter): this function is for setting the real time clock, returns the edited parameter.

DS18B20 Functions:
bool ds18b20_start(): used to know if the DS18B20 sensor is correctly connected to the circuit, returns 1 if OK and 0 if error.
ds18b20_write_bit(bool value): writes (sends) 1 bit to the DS18B20 sensor, the bit is ‘value‘ which may be 1 or 0.
ds18b20_write_byte(byte value): writes 1 byte (8 bits) to the DS18B20 sensor, this function is based on the previous function. This function writes LSB first.
bool ds18b20_read_bit(void): reads 1 bit from the DS18B20 sensor, returns the read value (1 or 0).
byte ds18b20_read_byte(void): reads 1 byte from the DS18B20 sensor, this function is based on the previous function. This function reads LSB first.
bool ds18b20_read(int *raw_temp_value): reads the temperature raw data which is 16-bit long (two 8-bit registers), the data is stored in the variable raw_temp_value, returns 1 if OK and 0 if error.

The value of the temperature in degree Celsius is equal to the raw value divided by 16 (in case of 12-bit resolution). The default resolution of the DS18B20 is 12 bits ( ==> thermometer resolution = 0.0625°C).

Full Arduino code:

This project was tested in real hardware circuit using original Samsung microSD card with capacity of 32GB. The following image shows data logger file (Log.txt) created by the hardware circuit:

Arduino DS18B20 datalogger sd card file

And the following one shows serial monitor output:

Arduino temperature logger serial monitor output

Proteus simulation:
This project could be simulated using Proteus software, the following video shows the result of the simulation.
Note that Proteus simulation circuit is not the same as real hardware circuit, project hardware circuit is shown above.

Proteus simulation file download link:
Arduino temperature datalogger

SD Card image file (FAT16_SD.ima) download link:
SD Card FAT16 image

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.