{"id":42334,"date":"2017-09-08T12:05:27","date_gmt":"2017-09-08T12:05:27","guid":{"rendered":"http:\/\/randomnerdtutorials.com\/?p=42334"},"modified":"2019-04-02T10:23:27","modified_gmt":"2019-04-02T10:23:27","slug":"arduino-temperature-data-logger-with-sd-card-module","status":"publish","type":"post","link":"https:\/\/randomnerdtutorials.com\/arduino-temperature-data-logger-with-sd-card-module\/","title":{"rendered":"Arduino Temperature Data Logger with SD Card Module"},"content":{"rendered":"<p>This post shows you how to create a temperature Arduino data logger. We\u2019ll use the DHT11 to measure temperature, the real time clock (RTC) module to take time stamps and the SD card module to save the data on the SD card.<!--more--><\/p>\n<p><img data-recalc-dims=\"1\" fetchpriority=\"high\" decoding=\"async\" class=\"aligncenter size-full wp-image-42344\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/08\/data-logger-circuit.jpg?resize=700%2C516&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" width=\"700\" height=\"516\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/08\/data-logger-circuit.jpg?w=700&amp;quality=100&amp;strip=all&amp;ssl=1 700w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/08\/data-logger-circuit.jpg?resize=300%2C221&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 700px) 100vw, 700px\" \/><\/p>\n<p><strong>Recommended resources:<\/strong><\/p>\n<ul>\n<li><a href=\"https:\/\/randomnerdtutorials.com\/guide-for-real-time-clock-rtc-module-with-arduino-ds1307-and-ds3231\/\" target=\"_blank\" rel=\"noopener noreferrer\">Guide for Real Time Clock (RTC) Module with Arduino (DS1307 and DS3231)<\/a><\/li>\n<li><a href=\"https:\/\/randomnerdtutorials.com\/complete-guide-for-dht11dht22-humidity-and-temperature-sensor-with-arduino\/\" target=\"_blank\" rel=\"noopener noreferrer\">Complete Guide for DHT11\/DHT22 Humidity and Temperature Sensor With Arduino<\/a><\/li>\n<li><a href=\"https:\/\/randomnerdtutorials.com\/guide-to-sd-card-module-with-arduino\/\">Guide to SD Card module with Arduino<\/a><\/li>\n<\/ul>\n<h2>Parts required<\/h2>\n<p>Here\u2019s a complete list of the parts required for this project:<\/p>\n<p><img data-recalc-dims=\"1\" decoding=\"async\" class=\"aligncenter\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/08\/data-logger-parts.jpg?resize=700%2C518&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" width=\"700\" height=\"518\" \/><\/p>\n<ul>\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/compatible-arduino-uno-r3-board\/\" target=\"_blank\" rel=\"noopener noreferrer\">Arduino UNO<\/a>\u00a0\u2013 read\u00a0<a href=\"https:\/\/makeradvisor.com\/best-arduino-starter-kits\/\" target=\"_blank\" rel=\"noopener noreferrer\">Best Arduino Starter Kits<\/a><\/li>\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/sd-card-module\/\" target=\"_blank\" rel=\"noopener noreferrer\">SD card module\u00a0<\/a><\/li>\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/microsd-card-raspberry-pi-16gb-class-10\/\" target=\"_blank\" rel=\"noopener noreferrer\">Micro SD card<\/a><\/li>\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/dht11-temperature-humidity-sensor\/\" target=\"_blank\" rel=\"noopener noreferrer\">DHT11 temperature and humidity sensor<\/a><\/li>\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/real-time-clock-module-ds1307\/\" target=\"_blank\" rel=\"noopener noreferrer\">RTC module\u00a0<\/a><\/li>\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/mb-102-solderless-breadboard-830-points\/\" target=\"_blank\" rel=\"noopener noreferrer\">Breadboard\u00a0<\/a><\/li>\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/jumper-wires-kit-120-pieces\/\" target=\"_blank\" rel=\"noopener noreferrer\">Jumper wires<\/a><\/li>\n<\/ul>\n<p>Note: alternatively to the SD card module, you can use a <a href=\"https:\/\/makeradvisor.com\/tools\/data-logging-shield\/\" target=\"_blank\" rel=\"noopener noreferrer\">data logging shield<\/a>. The data logging shield comes with built-in RTC and a prototyping area for soldering connections, sensors, etc..<\/p>\n<p>You can use the preceding links or go directly to <a href=\"https:\/\/makeradvisor.com\/tools\/?utm_source=rnt&utm_medium=post&utm_campaign=post\" target=\"_blank\">MakerAdvisor.com\/tools<\/a> to find all the parts for your projects at the best price!<\/p><p style=\"text-align:center;\"><a href=\"https:\/\/makeradvisor.com\/tools\/?utm_source=rnt&utm_medium=post&utm_campaign=post\" target=\"_blank\"><img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/10\/header-200.png?w=1200&#038;quality=100&#038;strip=all&#038;ssl=1\"><\/a><\/p>\n<h2>Schematics<\/h2>\n<p>The following figure shows the circuit\u2019s schematics for this project.<\/p>\n<p><a href=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/08\/temperature-datalogger_bb.png?quality=100&#038;strip=all&#038;ssl=1\"><img data-recalc-dims=\"1\" decoding=\"async\" class=\"aligncenter wp-image-42337 size-full\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/08\/temperature-datalogger_bb.png?resize=1200%2C533&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" width=\"1200\" height=\"533\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/08\/temperature-datalogger_bb.png?w=2154&amp;quality=100&amp;strip=all&amp;ssl=1 2154w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/08\/temperature-datalogger_bb.png?resize=300%2C133&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/08\/temperature-datalogger_bb.png?resize=768%2C341&amp;quality=100&amp;strip=all&amp;ssl=1 768w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/08\/temperature-datalogger_bb.png?resize=1024%2C454&amp;quality=100&amp;strip=all&amp;ssl=1 1024w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" \/><\/a><\/p>\n<p><strong>Note:<\/strong> make sure your SD card is formatted and working properly. Read &#8220;<a href=\"https:\/\/randomnerdtutorials.com\/guide-to-sd-card-module-with-arduino\/\">Guide to SD card module with Arduino<\/a>&#8220;.<\/p>\n<h4>Installing the DHT sensor library<\/h4>\n<p>For this project you need to install the DHT library to read from the DHT11 sensor.<\/p>\n<ol>\n<li><a href=\"https:\/\/github.com\/adafruit\/DHT-sensor-library\/archive\/master.zip\" target=\"_blank\" rel=\"noopener noreferrer\">Click here to download the DHT-sensor-library<\/a>. You should have a .zip folder in your Downloads folder<\/li>\n<li>Unzip the .zip folder and you should get DHT-sensor-library-master folder<\/li>\n<li>Rename your folder from <del>DHT-sensor-library-master<\/del> to <strong>DHT <\/strong><\/li>\n<li>Move the DHT folder to your Arduino IDE installation libraries folder<\/li>\n<li>Finally, re-open your Arduino IDE<\/li>\n<\/ol>\n<h2>Code<\/h2>\n<p>Copy the following code to your Arduino IDE and upload it to your Arduino board.<\/p>\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-c\">\/*\n * Rui Santos \n * Complete Project Details http:\/\/randomnerdtutorials.com\n *\/\n\n#include &lt;SPI.h&gt; \/\/for the SD card module\n#include &lt;SD.h&gt; \/\/ for the SD card\n#include &lt;DHT.h&gt; \/\/ for the DHT sensor\n#include &lt;RTClib.h&gt; \/\/ for the RTC\n\n\/\/define DHT pin\n#define DHTPIN 2     \/\/ what pin we're connected to\n\n\/\/ uncomment whatever type you're using\n#define DHTTYPE DHT11   \/\/ DHT 11 \n\/\/#define DHTTYPE DHT22   \/\/ DHT 22  (AM2302)\n\/\/#define DHTTYPE DHT21   \/\/ DHT 21 (AM2301)\n\n\/\/ initialize DHT sensor for normal 16mhz Arduino\nDHT dht(DHTPIN, DHTTYPE);\n\n\/\/ change this to match your SD shield or module;\n\/\/ Arduino Ethernet shield and modules: pin 4\n\/\/ Data loggin SD shields and modules: pin 10\n\/\/ Sparkfun SD shield: pin 8\nconst int chipSelect = 4; \n\n\/\/ Create a file to store the data\nFile myFile;\n\n\/\/ RTC\nRTC_DS1307 rtc;\n\nvoid setup() {\n  \/\/initializing the DHT sensor\n  dht.begin();\n\n  \/\/initializing Serial monitor\n  Serial.begin(9600);\n  \n  \/\/ setup for the RTC\n  while(!Serial); \/\/ for Leonardo\/Micro\/Zero\n    if(! rtc.begin()) {\n      Serial.println(&quot;Couldn't find RTC&quot;);\n      while (1);\n    }\n    else {\n      \/\/ following line sets the RTC to the date &amp; time this sketch was compiled\n      rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));\n    }\n    if(! rtc.isrunning()) {\n      Serial.println(&quot;RTC is NOT running!&quot;);\n    }\n    \n  \/\/ setup for the SD card\n  Serial.print(&quot;Initializing SD card...&quot;);\n\n  if(!SD.begin(chipSelect)) {\n    Serial.println(&quot;initialization failed!&quot;);\n    return;\n  }\n  Serial.println(&quot;initialization done.&quot;);\n    \n  \/\/open file\n  myFile=SD.open(&quot;DATA.txt&quot;, FILE_WRITE);\n\n  \/\/ if the file opened ok, write to it:\n  if (myFile) {\n    Serial.println(&quot;File opened ok&quot;);\n    \/\/ print the headings for our data\n    myFile.println(&quot;Date,Time,Temperature \u00baC&quot;);\n  }\n  myFile.close();\n}\n\nvoid loggingTime() {\n  DateTime now = rtc.now();\n  myFile = SD.open(&quot;DATA.txt&quot;, FILE_WRITE);\n  if (myFile) {\n    myFile.print(now.year(), DEC);\n    myFile.print('\/');\n    myFile.print(now.month(), DEC);\n    myFile.print('\/');\n    myFile.print(now.day(), DEC);\n    myFile.print(',');\n    myFile.print(now.hour(), DEC);\n    myFile.print(':');\n    myFile.print(now.minute(), DEC);\n    myFile.print(':');\n    myFile.print(now.second(), DEC);\n    myFile.print(&quot;,&quot;);\n  }\n  Serial.print(now.year(), DEC);\n  Serial.print('\/');\n  Serial.print(now.month(), DEC);\n  Serial.print('\/');\n  Serial.println(now.day(), DEC);\n  Serial.print(now.hour(), DEC);\n  Serial.print(':');\n  Serial.print(now.minute(), DEC);\n  Serial.print(':');\n  Serial.println(now.second(), DEC);\n  myFile.close();\n  delay(1000);  \n}\n\nvoid loggingTemperature() {\n  \/\/ Reading temperature or humidity takes about 250 milliseconds!\n  \/\/ Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)\n  \/\/ Read temperature as Celsius\n  float t = dht.readTemperature();\n  \/\/ Read temperature as Fahrenheit\n  \/\/float f = dht.readTemperature(true);\n  \n  \/\/ Check if any reads failed and exit early (to try again).\n  if  (isnan(t) \/*|| isnan(f)*\/) {\n    Serial.println(&quot;Failed to read from DHT sensor!&quot;);\n    return;\n  }\n  \n  \/\/debugging purposes\n  Serial.print(&quot;Temperature: &quot;); \n  Serial.print(t);\n  Serial.println(&quot; *C&quot;);\n  \/\/Serial.print(f);\n  \/\/Serial.println(&quot; *F\\t&quot;); \n  \n  myFile = SD.open(&quot;DATA.txt&quot;, FILE_WRITE);\n  if (myFile) {\n    Serial.println(&quot;open with success&quot;);\n    myFile.print(t);\n    myFile.println(&quot;,&quot;);\n  }\n  myFile.close();\n}\n\nvoid loop() {\n  loggingTime();\n  loggingTemperature();\n  delay(5000);\n}\n<\/code><\/pre>\n\t<p style=\"text-align:center\"><a class=\"rntwhite\" href=\"https:\/\/github.com\/RuiSantosdotme\/Random-Nerd-Tutorials\/raw\/master\/Projects\/Arduino_Temperature_Data_Logger.ino\" target=\"_blank\">View raw code<\/a><\/p>\n<p>In this code we create a <strong>loggingTime()<\/strong> function and a <strong>loggingTemperature()<\/strong> function that we call in the <strong>loop()<\/strong> to log the time and temperature to the <strong>DATA.txt<\/strong> file in the SD card.<\/p>\n<p>Open the Serial Monitor at a baud rate of 9600 and check if everything is working properly.<\/p>\n<h2>Getting the data from the SD card<\/h2>\n<p>Let this project run for a few hours to gather a decent amount of data, and when you\u2019re happy with the data logging period, shut down the Arduino and remove the SD from the SD card module.<\/p>\n<p>Insert the SD card on your computer, open it, and you should have a <strong>DATA.txt<\/strong> file with the collected data.<\/p>\n<p><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-42351\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/08\/datafile.png?resize=82%2C120&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" width=\"82\" height=\"120\" \/><\/p>\n<p>You can open the data with a text editor, or use a spreadsheet to analyse and process your data.<\/p>\n<p><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-42341\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/08\/data-txt.png?resize=493%2C542&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" width=\"493\" height=\"542\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/08\/data-txt.png?w=493&amp;quality=100&amp;strip=all&amp;ssl=1 493w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/08\/data-txt.png?resize=273%2C300&amp;quality=100&amp;strip=all&amp;ssl=1 273w\" sizes=\"(max-width: 493px) 100vw, 493px\" \/><\/p>\n<p>The data is separated by commas, and each reading is in a new line. In this format, you can easily import data to Excel or other data processing software.<\/p>\n<h2>Wrapping up<\/h2>\n<p>This is a great project to learn how to use the SD card module with Arduino to build a data logger. You can apply this concept in pretty much any project you&#8217;d like.<\/p>\n<p>If you like Arduino projects, make sure you check our latest Arduino course:\u00a0<strong><a href=\"https:\/\/randomnerdtutorials.com\/arduino-step-by-step-projects\/\" target=\"_blank\" rel=\"noopener noreferrer\">Arduino Step-by-step Projects \u2013 Build 23 Projects<\/a><\/strong><\/p>\n<p>We hope you\u2019ve found this project useful.<\/p>\n<p>Thanks for reading.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This post shows you how to create a temperature Arduino data logger. We\u2019ll use the DHT11 to measure temperature, the real time clock (RTC) module to take time stamps and &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"Arduino Temperature Data Logger with SD Card Module\" class=\"read-more button\" href=\"https:\/\/randomnerdtutorials.com\/arduino-temperature-data-logger-with-sd-card-module\/#more-42334\" aria-label=\"Read more about Arduino Temperature Data Logger with SD Card Module\">CONTINUE READING \u00bb<\/a><\/p>\n","protected":false},"author":5,"featured_media":42344,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[303,267,2,264,215,10],"tags":[],"class_list":["post-42334","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-0-arduino","category-arduino-project","category-arduino","category-project","category-sensors","category-a-tutorials"],"aioseo_notices":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/08\/data-logger-circuit.jpg?fit=700%2C516&quality=100&strip=all&ssl=1","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/42334","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/comments?post=42334"}],"version-history":[{"count":0,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/42334\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media\/42344"}],"wp:attachment":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media?parent=42334"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/categories?post=42334"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/tags?post=42334"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}