{"id":63239,"date":"2018-06-14T10:30:03","date_gmt":"2018-06-14T10:30:03","guid":{"rendered":"http:\/\/randomnerdtutorials.com\/?p=63239"},"modified":"2024-06-22T15:21:05","modified_gmt":"2024-06-22T15:21:05","slug":"esp32-data-logging-temperature-to-microsd-card","status":"publish","type":"post","link":"https:\/\/randomnerdtutorials.com\/esp32-data-logging-temperature-to-microsd-card\/","title":{"rendered":"ESP32 Data Logging Temperature to MicroSD Card"},"content":{"rendered":"\n<p>This project shows how to log data with timestamps to a microSD card using the ESP32. As an example, we&#8217;ll log temperature readings from the DS18B20 sensor every 10 minutes. The ESP32 will be in deep sleep mode between each reading, and it will request the date and time using Network Time Protocol (NTP).<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img data-recalc-dims=\"1\" fetchpriority=\"high\" decoding=\"async\" width=\"1200\" height=\"675\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/06\/ESP32-data-logging-thumbnail.jpg?resize=1200%2C675&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" class=\"wp-image-63293\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/06\/ESP32-data-logging-thumbnail.jpg?w=1280&amp;quality=100&amp;strip=all&amp;ssl=1 1280w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/06\/ESP32-data-logging-thumbnail.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/06\/ESP32-data-logging-thumbnail.jpg?resize=768%2C432&amp;quality=100&amp;strip=all&amp;ssl=1 768w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/06\/ESP32-data-logging-thumbnail.jpg?resize=1024%2C576&amp;quality=100&amp;strip=all&amp;ssl=1 1024w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" \/><\/figure><\/div>\n\n\n<p class=\"rntbox rntclblue\">You may also like: <a href=\"https:\/\/randomnerdtutorials.com\/esp32-how-to-log-data\/\">ESP32: How to Log Data (10 Different Ways)<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Project Overview<\/h2>\n\n\n\n<p>Before getting started, let&#8217;s highlight the project&#8217;s main features:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"1040\" height=\"704\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/06\/ESP-32-data-logging-overview.png?resize=1040%2C704&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" class=\"wp-image-63278\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/06\/ESP-32-data-logging-overview.png?w=1040&amp;quality=100&amp;strip=all&amp;ssl=1 1040w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/06\/ESP-32-data-logging-overview.png?resize=300%2C203&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/06\/ESP-32-data-logging-overview.png?resize=768%2C520&amp;quality=100&amp;strip=all&amp;ssl=1 768w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/06\/ESP-32-data-logging-overview.png?resize=1024%2C693&amp;quality=100&amp;strip=all&amp;ssl=1 1024w\" sizes=\"(max-width: 1040px) 100vw, 1040px\" \/><\/figure><\/div>\n\n\n<ul class=\"wp-block-list\">\n<li>The ESP32 reads temperature using the DS18B20 temperature sensor.<\/li>\n\n\n\n<li>After getting the temperature, it makes a request to an NTP (Network Time Protocol) server to get date and time. So, the ESP32 needs a Wi-Fi connection.<\/li>\n\n\n\n<li>The data (temperature and timestamp) are logged to a microSD card. To log data to the microSD card we&#8217;re using a microSD card module.<\/li>\n\n\n\n<li>After completing these previous tasks, the ESP32 sleeps for 10 minutes.<\/li>\n\n\n\n<li>The ESP32 wakes up and repeats the process.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Parts Required<\/h2>\n\n\n\n<p>Here&#8217;s a list of the parts required to build this project&nbsp;(click the links below to find the best price at&nbsp;<a href=\"https:\/\/makeradvisor.com\/tools\/\" target=\"_blank\" rel=\"noreferrer noopener\">Maker Advisor<\/a>):<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"750\" height=\"384\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/06\/esp32-datalogging-parts-required.jpg?resize=750%2C384&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" class=\"wp-image-63297\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/06\/esp32-datalogging-parts-required.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/06\/esp32-datalogging-parts-required.jpg?resize=300%2C154&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/esp32-dev-board-wi-fi-bluetooth\/\" target=\"_blank\" rel=\"noreferrer noopener\">ESP32 DOIT DEVKIT V1 Board<\/a>\u00a0\u2013\u00a0<a href=\"https:\/\/makeradvisor.com\/esp32-development-boards-review-comparison\/\" target=\"_blank\" rel=\"noreferrer noopener\">read ESP32 Development Boards Review and Comparison<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/sd-card-module\/\" target=\"_blank\" rel=\"noreferrer noopener\">MicroSD card module<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/microsd-card-raspberry-pi-16gb-class-10\/\" target=\"_blank\" rel=\"noreferrer noopener\">MicroSD card<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/ds18b20-temperature-sensor-2\/\" target=\"_blank\" rel=\"noreferrer noopener\">DS18B20 temperature sensor<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/resistors-kits\/\">10k Ohm resistor<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/jumper-wires-kit-120-pieces\/\" target=\"_blank\" rel=\"noreferrer noopener\">Jumper wires<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/mb-102-solderless-breadboard-830-points\/\" target=\"_blank\" rel=\"noreferrer noopener\">Breadboard<\/a><\/li>\n<\/ul>\n\n\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\n\n\n<h2 class=\"wp-block-heading\">Preparing the microSD Card Module<\/h2>\n\n\n\n<p>To save data on the microSD card with the ESP32, we use the following <a href=\"https:\/\/makeradvisor.com\/tools\/sd-card-module\/\" target=\"_blank\" rel=\"noreferrer noopener\">microSD card module<\/a> that communicates with the ESP32 using SPI communication protocol.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"455\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/06\/microsd-card-module.png?resize=750%2C455&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" class=\"wp-image-63246\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/06\/microsd-card-module.png?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/06\/microsd-card-module.png?resize=300%2C182&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n<p class=\"rntbox rntclblue\">Related content: <a href=\"https:\/\/randomnerdtutorials.com\/esp32-microsd-card-arduino\/\">ESP32: Guide for MicroSD Card Module using Arduino IDE<\/a><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Formatting the microSD card<\/h3>\n\n\n\n<p>When using a microSD card with the ESP32, you should format it first. Follow the next instructions to format your microSD card.<\/p>\n\n\n\n<p><strong>1.<\/strong>&nbsp;Insert the microSD card in your computer. Go to&nbsp;<strong>My Computer<\/strong>&nbsp;and right click on the SD card. Select&nbsp;<strong>Format<\/strong>&nbsp;as shown in figure below.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/rntlab.com\/wp-content\/uploads\/2018\/05\/format-SD-card-1.png?w=1200&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" class=\"wp-image-11805\"\/><\/figure><\/div>\n\n\n<p><strong>2.&nbsp;<\/strong>A new window pops up. Select&nbsp;<strong>FAT32<\/strong>, press&nbsp;<strong>Start<\/strong>&nbsp;to initialize the formatting process and follow the onscreen instructions.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/rntlab.com\/wp-content\/uploads\/2018\/05\/format-SD-card-2.png?w=1200&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" class=\"wp-image-11806\"\/><\/figure><\/div>\n\n\n<h2 class=\"wp-block-heading\">Schematic<\/h2>\n\n\n\n<p>Follow the next schematic diagram to assemble the circuit for this project.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><a href=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/06\/esp32-data-logging_bb.png?quality=100&#038;strip=all&#038;ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"1200\" height=\"565\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/06\/esp32-data-logging_bb.png?resize=1200%2C565&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" class=\"wp-image-63247\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/06\/esp32-data-logging_bb.png?w=1292&amp;quality=100&amp;strip=all&amp;ssl=1 1292w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/06\/esp32-data-logging_bb.png?resize=300%2C141&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/06\/esp32-data-logging_bb.png?resize=768%2C361&amp;quality=100&amp;strip=all&amp;ssl=1 768w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/06\/esp32-data-logging_bb.png?resize=1024%2C482&amp;quality=100&amp;strip=all&amp;ssl=1 1024w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" \/><\/a><\/figure><\/div>\n\n\n<p>You can also use the following table as a reference to wire the microSD card module:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>MicroSD Card Module<\/strong><\/td><td><strong>ESP32<\/strong><\/td><\/tr><tr><td>3V3<\/td><td><span class=\"rnthl rntcred\">3V3<\/span><\/td><\/tr><tr><td>CS<\/td><td><span class=\"rnthl rntcblue\">GPIO 5<\/span><\/td><\/tr><tr><td>MOSI<\/td><td><span class=\"rnthl rntcgreen\">GPIO 23<\/span><\/td><\/tr><tr><td>CLK<\/td><td><span class=\"rnthl rntclgray\">GPIO 18<\/span><\/td><\/tr><tr><td>MISO<\/td><td><span class=\"rnthl rntcyellow\">GPIO 19<\/span><\/td><\/tr><tr><td>GND<\/td><td><span class=\"rnthl rntcblack\">GND<\/span><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>The next figure shows how your circuit should look like:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"421\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/06\/esp32-data-logging-circuit.jpg?resize=750%2C421&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" class=\"wp-image-63298\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/06\/esp32-data-logging-circuit.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/06\/esp32-data-logging-circuit.jpg?resize=300%2C168&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n<h2 class=\"wp-block-heading\">Preparing the Arduino IDE<\/h2>\n\n\n\n<p>There\u2019s an add-on for the Arduino IDE that allows you to program the ESP32 using the Arduino IDE and its programming language. Follow the next tutorial to prepare your Arduino IDE to work with the ESP32, if you haven\u2019t already.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/installing-esp32-arduino-ide-2-0\/\" title=\"\">Installing ESP32 Board in Arduino IDE 2 (Windows, Mac OS X, Linux)<\/a><\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Installing Libraries<\/h3>\n\n\n\n<p>Before uploading the code, you need to install some libraries in  the Arduino IDE. The\u00a0<a href=\"https:\/\/github.com\/PaulStoffregen\/OneWire\" target=\"_blank\" rel=\"noreferrer noopener\">OneWire library by Paul Stoffregen<\/a>\u00a0and the\u00a0<a href=\"https:\/\/github.com\/milesburton\/Arduino-Temperature-Control-Library\" target=\"_blank\" rel=\"noreferrer noopener\">Dallas Temperature library<\/a> so that you can use the DS18B20 sensor. You also need to install the\u00a0<a href=\"https:\/\/github.com\/taranais\/NTPClient\" target=\"_blank\" rel=\"noreferrer noopener\">NTPClient library forked by Taranais<\/a>\u00a0to make request to an NTP server.<\/p>\n\n\n\n<p>Follow the next steps to install those libraries in your Arduino IDE:<\/p>\n\n\n\n<p><strong>OneWire library<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>In the Arduino IDE, go to <strong>Sketch <\/strong>> <strong>Include Library<\/strong> > <strong>Manage Libraries<\/strong>.<\/li>\n\n\n\n<li>Search for <em>OneWire<\/em>.<\/li>\n\n\n\n<li>Install the OneWire library as shown in the picture below.<\/li>\n<\/ol>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"337\" height=\"395\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/06\/install-onewire-library-arduino-ide-2.png?resize=337%2C395&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"install OneWire Library Arduino IDE\" class=\"wp-image-159450\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/06\/install-onewire-library-arduino-ide-2.png?w=337&amp;quality=100&amp;strip=all&amp;ssl=1 337w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/06\/install-onewire-library-arduino-ide-2.png?resize=256%2C300&amp;quality=100&amp;strip=all&amp;ssl=1 256w\" sizes=\"(max-width: 337px) 100vw, 337px\" \/><\/figure><\/div>\n\n\n<p><strong>Dallas Temperature library<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>In the Arduino IDE, go to <strong>Sketch <\/strong>&gt; <strong>Include Library<\/strong> &gt; <strong>Manage Libraries<\/strong>.<\/li>\n\n\n\n<li>Search for <em>DallasTemperature<\/em>.<\/li>\n\n\n\n<li>Install the <em>DallasTemperature <\/em>library by Miles Burton.<\/li>\n<\/ol>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"326\" height=\"422\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/06\/install-dallastemperature-library-arduino-ide-2.png?resize=326%2C422&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Install DallasTemperature Library Arduino IDE 2\" class=\"wp-image-159451\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/06\/install-dallastemperature-library-arduino-ide-2.png?w=326&amp;quality=100&amp;strip=all&amp;ssl=1 326w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/06\/install-dallastemperature-library-arduino-ide-2.png?resize=232%2C300&amp;quality=100&amp;strip=all&amp;ssl=1 232w\" sizes=\"(max-width: 326px) 100vw, 326px\" \/><\/figure><\/div>\n\n\n<p><strong>NTPClient library<\/strong><\/p>\n\n\n\n<p class=\"rntbox rntcred\"><strong>IMPORTANT<\/strong>: we\u2019re not using the default NTPClient library. To follow this tutorial you need to install the library we recommend using the following steps.<\/p>\n\n\n\n<p>We&#8217;ll be using the<a href=\"https:\/\/github.com\/taranais\/NTPClient\" title=\"\"> NTPClient Library forked by Taranais<\/a>. Follow the next steps to install this library in your Arduino IDE:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><a href=\"https:\/\/github.com\/taranais\/NTPClient\/archive\/master.zip\">Click here to download the NTP Client library<\/a>. You should have a .zip folder in your\u00a0<em>Downloads<\/em><\/li>\n\n\n\n<li>In your Arduino IDE, go to\u00a0<strong>Sketch\u00a0<\/strong>>\u00a0<strong>Include Library<\/strong>\u00a0>\u00a0<strong>Add . ZIP library<\/strong>\u2026<\/li>\n\n\n\n<li>Select the .ZIP file of the library you just downloaded.<\/li>\n\n\n\n<li>The library will be installed after a few seconds.<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">Uploading Code<\/h2>\n\n\n\n<p>Here&#8217;s the code you need to upload to your ESP32. Before uploading, you need to modify the code to include your network credentials (SSID and password). Continue reading to learn how the code works.<\/p>\n\n\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-c\">\/*********\n  Rui Santos\n  Complete project details at https:\/\/randomnerdtutorials.com  \n*********\/\n\n\/\/ Libraries for SD card\n#include &quot;FS.h&quot;\n#include &quot;SD.h&quot;\n#include &lt;SPI.h&gt;\n\n\/\/DS18B20 libraries\n#include &lt;OneWire.h&gt;\n#include &lt;DallasTemperature.h&gt;\n\n\/\/ Libraries to get time from NTP Server\n#include &lt;WiFi.h&gt;\n#include &lt;NTPClient.h&gt;\n#include &lt;WiFiUdp.h&gt;\n\n\/\/ Define deep sleep options\nuint64_t uS_TO_S_FACTOR = 1000000;  \/\/ Conversion factor for micro seconds to seconds\n\/\/ Sleep for 10 minutes = 600 seconds\nuint64_t TIME_TO_SLEEP = 600;\n\n\/\/ Replace with your network credentials\nconst char* ssid     = &quot;REPLACE_WITH_YOUR_SSID&quot;;\nconst char* password = &quot;REPLACE_WITH_YOUR_PASSWORD&quot;;\n\n\/\/ Define CS pin for the SD card module\n#define SD_CS 5\n\n\/\/ Save reading number on RTC memory\nRTC_DATA_ATTR int readingID = 0;\n\nString dataMessage;\n\n\/\/ Data wire is connected to ESP32 GPIO 21\n#define ONE_WIRE_BUS 21\n\/\/ Setup a oneWire instance to communicate with a OneWire device\nOneWire oneWire(ONE_WIRE_BUS);\n\/\/ Pass our oneWire reference to Dallas Temperature sensor \nDallasTemperature sensors(&amp;oneWire);\n\n\/\/ Temperature Sensor variables\nfloat temperature;\n\n\/\/ Define NTP Client to get time\nWiFiUDP ntpUDP;\nNTPClient timeClient(ntpUDP);\n\n\/\/ Variables to save date and time\nString formattedDate;\nString dayStamp;\nString timeStamp;\n\nvoid setup() {\n  \/\/ Start serial communication for debugging purposes\n  Serial.begin(115200);\n\n  \/\/ Connect to Wi-Fi network with SSID and password\n  Serial.print(&quot;Connecting to &quot;);\n  Serial.println(ssid);\n  WiFi.begin(ssid, password);\n  while (WiFi.status() != WL_CONNECTED) {\n    delay(500);\n    Serial.print(&quot;.&quot;);\n  }\n  Serial.println(&quot;&quot;);\n  Serial.println(&quot;WiFi connected.&quot;);\n\n  \/\/ Initialize a NTPClient to get time\n  timeClient.begin();\n  \/\/ Set offset time in seconds to adjust for your timezone, for example:\n  \/\/ GMT +1 = 3600\n  \/\/ GMT +8 = 28800\n  \/\/ GMT -1 = -3600\n  \/\/ GMT 0 = 0\n  timeClient.setTimeOffset(3600);\n\n  \/\/ Initialize SD card\n  SD.begin(SD_CS);  \n  if(!SD.begin(SD_CS)) {\n    Serial.println(&quot;Card Mount Failed&quot;);\n    return;\n  }\n  uint8_t cardType = SD.cardType();\n  if(cardType == CARD_NONE) {\n    Serial.println(&quot;No SD card attached&quot;);\n    return;\n  }\n  Serial.println(&quot;Initializing SD card...&quot;);\n  if (!SD.begin(SD_CS)) {\n    Serial.println(&quot;ERROR - SD card initialization failed!&quot;);\n    return;    \/\/ init failed\n  }\n\n  \/\/ If the data.txt file doesn't exist\n  \/\/ Create a file on the SD card and write the data labels\n  File file = SD.open(&quot;\/data.txt&quot;);\n  if(!file) {\n    Serial.println(&quot;File doens't exist&quot;);\n    Serial.println(&quot;Creating file...&quot;);\n    writeFile(SD, &quot;\/data.txt&quot;, &quot;Reading ID, Date, Hour, Temperature \\r\\n&quot;);\n  }\n  else {\n    Serial.println(&quot;File already exists&quot;);  \n  }\n  file.close();\n\n  \/\/ Enable Timer wake_up\n  esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR);\n\n  \/\/ Start the DallasTemperature library\n  sensors.begin(); \n\n  getReadings();\n  getTimeStamp();\n  logSDCard();\n  \n  \/\/ Increment readingID on every new reading\n  readingID++;\n  \n  \/\/ Start deep sleep\n  Serial.println(&quot;DONE! Going to sleep now.&quot;);\n  esp_deep_sleep_start(); \n}\n\nvoid loop() {\n  \/\/ The ESP32 will be in deep sleep\n  \/\/ it never reaches the loop()\n}\n\n\/\/ Function to get temperature\nvoid getReadings(){\n  sensors.requestTemperatures(); \n  temperature = sensors.getTempCByIndex(0); \/\/ Temperature in Celsius\n  \/\/temperature = sensors.getTempFByIndex(0); \/\/ Temperature in Fahrenheit\n  Serial.print(&quot;Temperature: &quot;);\n  Serial.println(temperature);\n}\n\n\/\/ Function to get date and time from NTPClient\nvoid getTimeStamp() {\n  while(!timeClient.update()) {\n    timeClient.forceUpdate();\n  }\n  \/\/ The formattedDate comes with the following format:\n  \/\/ 2018-05-28T16:00:13Z\n  \/\/ We need to extract date and time\n  formattedDate = timeClient.getFormattedDate();\n  Serial.println(formattedDate);\n\n  \/\/ Extract date\n  int splitT = formattedDate.indexOf(&quot;T&quot;);\n  dayStamp = formattedDate.substring(0, splitT);\n  Serial.println(dayStamp);\n  \/\/ Extract time\n  timeStamp = formattedDate.substring(splitT+1, formattedDate.length()-1);\n  Serial.println(timeStamp);\n}\n\n\/\/ Write the sensor readings on the SD card\nvoid logSDCard() {\n  dataMessage = String(readingID) + &quot;,&quot; + String(dayStamp) + &quot;,&quot; + String(timeStamp) + &quot;,&quot; + \n                String(temperature) + &quot;\\r\\n&quot;;\n  Serial.print(&quot;Save data: &quot;);\n  Serial.println(dataMessage);\n  appendFile(SD, &quot;\/data.txt&quot;, dataMessage.c_str());\n}\n\n\/\/ Write to the SD card (DON'T MODIFY THIS FUNCTION)\nvoid writeFile(fs::FS &amp;fs, const char * path, const char * message) {\n  Serial.printf(&quot;Writing file: %s\\n&quot;, path);\n\n  File file = fs.open(path, FILE_WRITE);\n  if(!file) {\n    Serial.println(&quot;Failed to open file for writing&quot;);\n    return;\n  }\n  if(file.print(message)) {\n    Serial.println(&quot;File written&quot;);\n  } else {\n    Serial.println(&quot;Write failed&quot;);\n  }\n  file.close();\n}\n\n\/\/ Append data to the SD card (DON'T MODIFY THIS FUNCTION)\nvoid appendFile(fs::FS &amp;fs, const char * path, const char * message) {\n  Serial.printf(&quot;Appending to file: %s\\n&quot;, path);\n\n  File file = fs.open(path, FILE_APPEND);\n  if(!file) {\n    Serial.println(&quot;Failed to open file for appending&quot;);\n    return;\n  }\n  if(file.print(message)) {\n    Serial.println(&quot;Message appended&quot;);\n  } else {\n    Serial.println(&quot;Append failed&quot;);\n  }\n  file.close();\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\/ESP32\/ESP32_Temperature_Data_Logging.ino\" target=\"_blank\">View raw code<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How the Code Works<\/h2>\n\n\n\n<p>In this example, the ESP32 is in deep sleep mode between each reading. In deep sleep mode, all your code should go in the <span class=\"rnthl rntliteral\">setup()<\/span> function, because the ESP32 never reaches the <span class=\"rnthl rntliteral\">loop()<\/span>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Importing libraries<\/h3>\n\n\n\n<p>First, you import the needed libraries for the microSD card module:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>#include \"FS.h\"\n#include \"SD.h\"\n#include &lt;SPI.h&gt;<\/code><\/pre>\n\n\n\n<p>Import these libraries to work with the DS18B20 temperature sensor.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>#include &lt;OneWire.h&gt;\n#include &lt;DallasTemperature.h&gt;<\/code><\/pre>\n\n\n\n<p>The following libraries allow you to request the date and time from an NTP server.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>#include &lt;WiFi.h&gt;\n#include &lt;NTPClient.h&gt;\n#include &lt;WiFiUdp.h&gt;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Setting deep sleep time<\/h3>\n\n\n\n<p>This example uses a conversion factor from microseconds to seconds, so that you can set the sleep time in the <span class=\"rnthl rntliteral\">TIME_TO_SLEEP<\/span> variable in seconds.<\/p>\n\n\n\n<p>In this case, we&#8217;re setting the ESP32 to go to sleep for 10 minutes (600 seconds). If you want the ESP32 to sleep for a different period of time, you just need to enter the number of seconds for deep sleep in the <span class=\"rnthl rntliteral\">TIME_TO_SLEEP<\/span> variable.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>\/\/ Define deep sleep options\nuint64_t uS_TO_S_FACTOR = 1000000; \/\/ Conversion factor for micro seconds to seconds\n\/\/ Sleep for 10 minutes = 600 seconds\nuint64_t TIME_TO_SLEEP = 600;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Setting your network credentials<\/h3>\n\n\n\n<p>Type your network credentials in the following variables, so that the ESP32 is able to connect to your local network.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>\/\/ Replace with your network credentials\nconst char* ssid = \"REPLACE_WITH_YOUR_SSID\";\nconst char* password = \"REPLACE_WITH_YOUR_PASSWORD\";<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Initializing sensors and variables<\/h3>\n\n\n\n<p>Next, define the microSD card SD pin. In this case, it is set to <span class=\"rnthl rntcblue\">GPIO 5<\/span>.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>#define SD_CS 5<\/code><\/pre>\n\n\n\n<p>Create a variable called <span class=\"rnthl rntliteral\">readingID<\/span> to hold the reading ID. This is a way to get your readings organized. To save a variable value during deep sleep, we can save it in the RTC memory. To save data on the RTC memory, you just need to add <span class=\"rnthl rntliteral\">RTC_DATA_ATTR<\/span> before the variable definition.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>\/\/ Save reading number on RTC memory\nRTC_DATA_ATTR int readingID = 0;<\/code><\/pre>\n\n\n\n<p>Create a String variable to hold the data to be saved on the microSD card.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>String dataMessage;<\/code><\/pre>\n\n\n\n<p>Next, create the instances needed for the temperature sensor. The temperature sensor is connected to <span class=\"rnthl rntcpurple\">GPIO 21<\/span>.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>\/\/ Data wire is connected to ESP32 GPIO21\n#define ONE_WIRE_BUS 21\n\/\/ Setup a oneWire instance to communicate with a OneWire device\nOneWire oneWire(ONE_WIRE_BUS);\n\/\/ Pass our oneWire reference to Dallas Temperature sensor \nDallasTemperature sensors(&amp;oneWire);<\/code><\/pre>\n\n\n\n<p>Then, create a float variable to hold the temperature retrieved by the DS18B20 sensor.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>float temperature;<\/code><\/pre>\n\n\n\n<p>The following two lines define an NTPClient to request date and time from an NTP server.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>WiFiUDP ntpUDP;\nNTPClient timeClient(ntpUDP);<\/code><\/pre>\n\n\n\n<p>Then, initialize&nbsp;String&nbsp;variables to save the date and time.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>String formattedDate;\nString dayStamp;\nString timeStamp;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">setup()<\/h3>\n\n\n\n<p>When you use deep sleep with the ESP32, all the code should go inside the <span class=\"rnthl rntliteral\">setup()<\/span> function, because the ESP32 never reaches the <span class=\"rnthl rntliteral\">loop()<\/span>.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Connecting to Wi-Fi<\/h4>\n\n\n\n<p>The following snippet of code connects to the Wi-Fi network. You need to connect to wi-fi to request date and time from the NTP server.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>Serial.print(\"Connecting to \");\nSerial.println(ssid);\nWiFi.begin(ssid, password);\nwhile (WiFi.status() != WL_CONNECTED) {\n  delay(500);\n  Serial.print(\".\");\n}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Initializing the NTP client<\/h4>\n\n\n\n<p>Next, initialize the NTP client to get date and time from an NTP server.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>timeClient.begin();<\/code><\/pre>\n\n\n\n<p>You can use the <span class=\"rnthl rntliteral\">setTimeOffset(&lt;time&gt;)<\/span> method to adjust the time for your timezone.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>timeClient.setTimeOffset(3600);<\/code><\/pre>\n\n\n\n<p>Here are some examples for different timezones:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>GMT +1 = 3600<\/li>\n\n\n\n<li>GMT +8 = 28800<\/li>\n\n\n\n<li>GMT -1 = -3600<\/li>\n\n\n\n<li>GMT 0 = 0<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Initializing the microSD card module<\/h3>\n\n\n\n<p>Then, initialize the microSD card. The following&nbsp;<em>if<\/em>&nbsp;statements check if the microSD card is properly attached.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>SD.begin(SD_CS); \nif(!SD.begin(SD_CS)) {\n  Serial.println(\"Card Mount Failed\");\n  return;\n}\nuint8_t cardType = SD.cardType();\nif(cardType == CARD_NONE) {\n  Serial.println(\"No SD card attached\");\n  return;\n}\nSerial.println(\"Initializing SD card...\");\nif (!SD.begin(SD_CS)) {\n  Serial.println(\"ERROR - SD card initialization failed!\");\n  return; \/\/ init failed\n}<\/code><\/pre>\n\n\n\n<p>Then, try to open the <em>data.txt<\/em> file on the microSD card.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>File file = SD.open(\"\/data.txt\");<\/code><\/pre>\n\n\n\n<p>If that file doesn\u2019t exist, we need to create it and write the heading for the&nbsp;<em>.txt<\/em>&nbsp;file.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>writeFile(SD, \"\/data.txt\", \"Reading ID, Date, Hour, Temperature \\r\\n\");<\/code><\/pre>\n\n\n\n<p>If the file already exists, the code continues.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>else {\n  Serial.println(\"File already exists\");\n}<\/code><\/pre>\n\n\n\n<p>Finally, we close the file.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>file.close();<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Enable timer wake up<\/h4>\n\n\n\n<p>Then, you enable the timer wake up with the timer you&#8217;ve defined earlier in the <span class=\"rnthl rntliteral\">TIME_TO_SLEEP<\/span> variable.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR);<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Initializing the library for DS18B20<\/h4>\n\n\n\n<p>Next, you initialize the library for the DS18B20 temperature sensor.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>sensors.begin();<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Getting the readings and data logging<\/h4>\n\n\n\n<p>After having everything initialized, we can get the readings, timestamp, and log everything into the microSD card.<\/p>\n\n\n\n<p>To make the code easier to understand, we&#8217;ve created the following functions:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><span class=\"rnthl rntliteral\">getReadings()<\/span>: reads the temperature from the DS18B20 temperature sensor;<\/li>\n\n\n\n<li><span class=\"rnthl rntliteral\">getTimeStamp()<\/span>: gets date and time from the NTP server;<\/li>\n\n\n\n<li><span class=\"rnthl rntliteral\">logSDcard()<\/span>: logs the preceding data to the microSD card.<\/li>\n<\/ul>\n\n\n\n<p>After completing these tasks, we increment the <span class=\"rnthl rntliteral\">readingID<\/span>.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>readingID++;<\/code><\/pre>\n\n\n\n<p>Finally, the ESP32 starts the deep sleep.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>esp_deep_sleep_start();<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">getReadings()<\/h3>\n\n\n\n<p>Let\u2019s take a look at the <span class=\"rnthl rntliteral\">getReadings()<\/span> function. This function simply reads temperature from the DS18B20 temperature sensor.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>sensors.requestTemperatures(); \ntemperature = sensors.getTempCByIndex(0); \/\/ Temperature in Celsius<\/code><\/pre>\n\n\n\n<p>By default, the code retrieves the temperature in Celsius degrees. You can uncomment the following line and comment the previous one to get temperature in Fahrenheit.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>\/\/temperature = sensors.getTempFByIndex(0); \/\/ Temperature in Fahrenheit<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">getTimeStamp()<\/h3>\n\n\n\n<p>The <span class=\"rnthl rntliteral\">getTimeStamp()<\/span> function gets the date and time. These next lines ensure that we get a valid date and time:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>while(!timeClient.update()) {\n  timeClient.forceUpdate();\n}<\/code><\/pre>\n\n\n\n<p>Sometimes the NTPClient retrieves the year of 1970. To ensure that doesn\u2019t happen we force the update.<\/p>\n\n\n\n<p>Then, convert the date and time to a readable format with the <span class=\"rnthl rntliteral\">getFormattedDate()<\/span> method:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>formattedDate = timeClient.getFormattedDate();<\/code><\/pre>\n\n\n\n<p>The date and time are returned in this format:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">2018-04-30<strong>T<\/strong>16:00:13<strong>Z<\/strong><\/pre>\n\n\n\n<p>So, we need to split that string to get date and time separately. That\u2019s what we do here:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>\/\/ Extract date\nint splitT = formattedDate.indexOf(\"T\");\ndayStamp = formattedDate.substring(0, splitT);\nSerial.println(dayStamp);\n\/\/ Extract time\ntimeStamp = formattedDate.substring(splitT+1, formattedDate.length()-1);\nSerial.println(timeStamp);<\/code><\/pre>\n\n\n\n<p>The date is saved on the <span class=\"rnthl rntliteral\">dayStamp<\/span> variable, and the time on the <span class=\"rnthl rntliteral\">timeStamp<\/span> variable.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">logSDCard()<\/h3>\n\n\n\n<p>The <span class=\"rnthl rntliteral\">logSDCard()<\/span> function concatenates all the information in the <span class=\"rnthl rntliteral\">dataMessage<\/span> String variable. Each reading is separated by commas.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>dataMessage = String(readingID) + \",\" + String(dayStamp) + \",\" + String(timeStamp) + \",\" + String(temperature) + \"\\r\\n\";<\/code><\/pre>\n\n\n\n<p><strong>Note:<\/strong> the <span class=\"rnthl rntliteral\">&#8220;\\r\\n&#8221;<\/span> at the end of the <span class=\"rnthl rntliteral\">dataMessage<\/span>variable ensures the next reading is written on the next line.<\/p>\n\n\n\n<p>Then, with the following line, we write all the information to the&nbsp;<em>data.txt<\/em>&nbsp;file in the microSD card.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>appendFile(SD, \"\/data.txt\", dataMessage.c_str());<\/code><\/pre>\n\n\n\n<p><strong>Note:<\/strong> the <span class=\"rnthl rntliteral\">appendFile()<\/span> function only accepts variables of type const char for the message. So, use the <span class=\"rnthl rntliteral\">c_str()<\/span>&nbsp;method to convert the <span class=\"rnthl rntliteral\">dataMessage<\/span> variable.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">writeFile() and appendFile()<\/h3>\n\n\n\n<p>The last two functions: <span class=\"rnthl rntliteral\">writeFile()<\/span> and <span class=\"rnthl rntliteral\">appendFile()<\/span> are used to write and append data to the microSD card. They come with the SD card library examples and you shouldn\u2019t modify them.<\/p>\n\n\n\n<p>To try other examples to work with the microSD card, go to&nbsp;<strong>File <\/strong>&gt; <strong>Examples <\/strong>&gt; <strong>SD(esp32)<\/strong>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Uploading the Code<\/h2>\n\n\n\n<p>Now, upload the code to your ESP32. Make sure you have the right board and COM port selected.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><a href=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/05\/arduino-ide-2-upload-button.png?quality=100&#038;strip=all&#038;ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"36\" height=\"39\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/05\/arduino-ide-2-upload-button.png?resize=36%2C39&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Arduino IDE 2 Upload Button\" class=\"wp-image-146269\"\/><\/a><\/figure><\/div>\n\n\n<h2 class=\"wp-block-heading\">Demonstration<\/h2>\n\n\n\n<p>Open the Serial Monitor at a baud rate of 115200.<\/p>\n\n\n\n<p>Press the ESP32 Enable button, and check that everything is working properly (the ESP32 is connected to your local network, and the microSD card is properly attached).<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"554\" height=\"372\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/06\/ESP32-datalogging-serial-monitor.png?resize=554%2C372&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" class=\"wp-image-63279\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/06\/ESP32-datalogging-serial-monitor.png?w=554&amp;quality=100&amp;strip=all&amp;ssl=1 554w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/06\/ESP32-datalogging-serial-monitor.png?resize=300%2C201&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 554px) 100vw, 554px\" \/><\/figure><\/div>\n\n\n<p class=\"rntbox rntcred\"><strong>Note: <\/strong> If everything is wired properly and you keep getting an error initializing the SD card, powering your microSD card module with 5V might solve the issue.<\/p>\n\n\n\n<p>Let the ESP32 run for a few hours to test if everything is working as expected. After the testing period, remove the microSD card and insert it into your computer. The microSD card should contain a file called <em>data.txt<\/em>.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"88\" height=\"116\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/06\/data-file.jpg?resize=88%2C116&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" class=\"wp-image-63299\"\/><\/figure><\/div>\n\n\n<p>You can copy the file content to a spreadsheet on Google Sheets for example, and then split the data by commas. To split data by commas, select the column where you have your data, then go to <strong>Data<\/strong> &gt; <strong>Split text to columns&#8230;<\/strong>&nbsp;Then, you can build charts to analyse the data.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"600\" height=\"371\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/06\/esp32-datalogging-temperature.png?resize=600%2C371&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" class=\"wp-image-63301\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/06\/esp32-datalogging-temperature.png?w=600&amp;quality=100&amp;strip=all&amp;ssl=1 600w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/06\/esp32-datalogging-temperature.png?resize=300%2C186&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 600px) 100vw, 600px\" \/><\/figure><\/div>\n\n\n<h2 class=\"wp-block-heading\">Wrapping Up<\/h2>\n\n\n\n<p>In this tutorial, you learned to log data to a microSD card using the ESP32. We&#8217;ve also shown you how to read temperature from the DS18B20 temperature sensor and how to request time from an NTP server.<\/p>\n\n\n\n<p>You can apply the concepts from this tutorial to your own projects. You may also like:<\/p>\n\n\n\n<p><strong>You might also like reading other articles related with ESP32:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/esp32-microsd-card-arduino\/\">ESP32: Guide for MicroSD Card Module using Arduino IDE<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/altimeter-datalogger-esp32-bmp388\/\">Altimeter Datalogger: ESP32 with BMP388, MicroSD Card Storage and OLED Display<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/esp32-datalogger-download-data-file\/\">ESP32 Datalogger: Download Data File via Web Server (Arduino IDE)<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/esp32-how-to-log-data\/\">ESP32: How to Log Data (10 Different Ways)<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/esp32-datalogging-google-sheets\/\">ESP32 Datalogging to Google Sheets (using Google Service Account)<\/a><\/li>\n<\/ul>\n\n\n\n<p>Learn more about the ESP32 with our resources:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/learn-esp32-with-arduino-ide\/\">Learn ESP32 with Arduino IDE (eBook)<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/smart-home-ebook\/\" title=\"\">SMART HOME with Raspberry Pi, ESP32, ESP8266<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/build-web-servers-esp32-esp8266-ebook\/\">Build Web Servers with ESP32 and ESP8266 (eBook)<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/firebase-esp32-esp8266-ebook\/\" title=\"\">Firebase Web App with the ESP32 and ESP8266<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/projects-esp32\/\">Free ESP32 Projects and Tutorials\u2026<\/a><\/li>\n<\/ul>\n\n\n\n<p>Thanks for reading.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This project shows how to log data with timestamps to a microSD card using the ESP32. As an example, we&#8217;ll log temperature readings from the DS18B20 sensor every 10 minutes. &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"ESP32 Data Logging Temperature to MicroSD Card\" class=\"read-more button\" href=\"https:\/\/randomnerdtutorials.com\/esp32-data-logging-temperature-to-microsd-card\/#more-63239\" aria-label=\"Read more about ESP32 Data Logging Temperature to MicroSD Card\">CONTINUE READING \u00bb<\/a><\/p>\n","protected":false},"author":5,"featured_media":63293,"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":[281,276,277,299,264],"tags":[],"class_list":["post-63239","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-esp32-project","category-esp32","category-esp32-arduino-ide","category-0-esp32","category-project"],"aioseo_notices":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2018\/06\/ESP32-data-logging-thumbnail.jpg?fit=1280%2C720&quality=100&strip=all&ssl=1","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/63239","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=63239"}],"version-history":[{"count":3,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/63239\/revisions"}],"predecessor-version":[{"id":159457,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/63239\/revisions\/159457"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media\/63293"}],"wp:attachment":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media?parent=63239"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/categories?post=63239"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/tags?post=63239"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}