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


Discover more from Simple Circuit

Subscribe to get the latest posts sent to your email.

18 thoughts on “Arduino Temperature Data Logger with SD Card”

  1. Hello,
    Does anyone know what could be the reason that the temperature value is not shown on the display nor in the serial monitor? everything else seems to be working correctly.
    It only shows Temp: in the display and TEMPERATURE in the serial monitor.

  2. Hi James,
    Your welcome. We are all beginners, so we must help each other. It is correct for this program. When you first run, you must open the serial monitor to see if sd card is okay or not.

    Don’t forget please, While writing codes, serial monitor is using for printing some knowledge on monitor screen instead of lcd, led display, etc. We can just write codes without using circuit.
    Code line 42 is for initializing sd card and line 43 is to show result of sd card on serial monitor. For example, i will change the codes later for my project. I will rewrite “serial.print” commands to “print” commands to see on lcd / ST7735 display while first running (initializing sd card) stand alone.
    I hope i helped you. Best regards.

  3. Hi James,
    I think you don’t need to connect your project to the computer, just connect extra power supply and run your project. Serial monitor is just helping you. Also you can convert serial monitor print commands to lcd print commands and you can use without serial monitor.

    1. James Reginald RAFTER

      Hi Onder. Thanks so much for your help. I already had an external supply connected. The problem was me not
      understanding the process. It appears that I need to open the serial monitor to initialise the sd card. After that I can close the monitor and disconnect the project from my laptop and it will continue to record data as a stand alone unit.
      Is that correct. Regards James(Jim)

  4. James Reginald RAFTER

    Hi. Good project. However to write data to the SD card the project must be connected
    to my computer and the serial monitor must be open. Is there a code change that
    would fix this? Cheers.

  5. I get error in the code help me pls :

    C:\Users\marou\Desktop\sketch_apr17a\sketch_apr17a.ino: In function ‘void loop()’:
    C:\Users\marou\Desktop\sketch_apr17a\sketch_apr17a.ino:92:22: warning: invalid conversion from ‘unsigned int*’ to ‘int*’ [-fpermissive]
         if( ds18b20_read(&ds18b20_temp) )
                          ^~~~~~~~~~~~~
    C:\Users\marou\Desktop\sketch_apr17a\sketch_apr17a.ino:282:6: note:   initializing argument 1 of ‘bool ds18b20_read(int*)’
     bool ds18b20_read(int *raw_temp_value)
          ^~~~~~~~~~~~

  6. I figured out how to display degrees F . All it took was some time to understand. I am a beginner still.

    (ds18b20_temp/16)*9/5+32 buffer1[10] = ‘C’; // put ‘C’ letter change the C to F

  7. I build it and it works like charm.
    But I would like to connect 3 temp sensors. How do I change the sketch so it writes the values of 3 sensors to the SD card?
    I don’t mind if the display only displays the value of one sensor.
    I’m new to programming.

    Henk

    1. This is cool and fun to build. I like paling with stats and graphs and am seeing if the temperature in my apartment is conducive to brewing (fermenting) beer by logging the temperature inside a 5 gallon bottle of water over time. I am using a DS18B20 Temperature Sensor Waterproof Digital Thermal Probe Sensor. How can I program this for degrees Fahrenheit ?

  8. hello, this code is absolutely awesome but the time and date being saved doesn’t match the actual time and date. please help me solve this probem

Leave a Comment

Your email address will not be published. Required fields are marked *

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

Scroll to Top