ESP32 Data Logging Temperature to MicroSD Card
This project shows how to log data with timestamps to a microSD card using the ESP32. We'll log temperature readings from the DS18B20 sensor every 10 minutes.
- Suitable forB.Tech/Diploma IoT/ESP32 students
- Build time1-2 weeks
- LevelIntermediate
- Available atAjvika Labs, Kakinada
About this project
This project shows how to log data with timestamps to a microSD card using the ESP32. As an example, we’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).
:
ESP32: How to Log Data (10 Different Ways)
Project Overview
Before getting started, let’s highlight the project’s main features:
The ESP32 reads temperature using the DS18B20 temperature sensor.
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.
The data (temperature and timestamp) are logged to a microSD card. To log data to the microSD card we’re using a microSD card module.
After completing these previous tasks, the ESP32 sleeps for 10 minutes.
The ESP32 wakes up and repeats the process.
Parts Required
Here’s a list of the parts required to build this project (click the links below to find the best price at
Maker Advisor
):
ESP32 DOIT DEVKIT V1 Board
–
read ESP32 Development Boards Review and Comparison
MicroSD card module
MicroSD card
DS18B20 temperature sensor
10k Ohm resistor
Jumper wires
Breadboard
You can use the preceding links or go directly to
MakerAdvisor.com/tools
to find all the parts for your projects at the best price!
Preparing the microSD Card Module
To save data on the microSD card with the ESP32, we use the following
microSD card module
that communicates with the ESP32 using SPI communication protocol.
Related content:
ESP32: Guide for MicroSD Card Module using Arduino IDE
Formatting the microSD card
When using a microSD card with the ESP32, you should format it first. Follow the next instructions to format your microSD card.
1.
Insert the microSD card in your computer. Go to
My Computer
and right click on the SD card. Select
Format
as shown in figure below.
2.
A new window pops up. Select
FAT32
, press
Start
to initialize the formatting process and follow the onscreen instructions.
Schematic
Follow the next schematic diagram to assemble the circuit for this project.
You can also use the following table as a reference to wire the microSD card module:
MicroSD Card Module
ESP32
3V3
3V3
CS
GPIO 5
MOSI
GPIO 23
CLK
GPIO 18
MISO
GPIO 19
GND
GND
The next figure shows how your circuit should look like:
Preparing the Arduino IDE
There’s 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’t already.
Installing ESP32 Board in Arduino IDE 2 (Windows, Mac OS X, Linux)
Installing Libraries
Before uploading the code, you need to install some libraries in the Arduino IDE. The
OneWire library by Paul Stoffregen
and the
Dallas Temperature library
so that you can use the DS18B20 sensor. You also need to install the
NTPClient library forked by Taranais
to make request to an NTP server.
Follow the next steps to install those libraries in your Arduino IDE:
OneWire library
In the Arduino IDE, go to
Sketch
>
Include Library
>
Manage Libraries
OneWire
Install the OneWire library as shown in the picture below.
Dallas Temperature library
In the Arduino IDE, go to
Sketch
>
Include Library
>
Manage Libraries
DallasTemperature
Install the
DallasTemperature
library by Miles Burton.
NTPClient library
IMPORTANT
: we’re not using the default NTPClient library. To follow this tutorial you need to install the library we recommend using the following steps.
We’ll be using the
NTPClient Library forked by Taranais
. Follow the next steps to install this library in your Arduino IDE:
Click here to download the NTP Client library
. You should have a .zip folder in your
Downloads
In your Arduino IDE, go to
Sketch
>
Include Library
>
Add . ZIP library
…
Select the .ZIP file of the library you just downloaded.
The library will be installed after a few seconds.
Uploading Code
Here’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.
// Libraries for SD card
#include \"FS.h\"
#include \"SD.h\"
#include `SPI.h`
//DS18B20 libraries
#include `OneWire.h`
#include `DallasTemperature.h`
// Libraries to get time from NTP Server
#include `WiFi.h`
#include `NTPClient.h`
#include `WiFiUdp.h`
// Define deep sleep options
uint64_t uS_TO_S_FACTOR = 1000000; // Conversion factor for micro seconds to seconds
// Sleep for 10 minutes = 600 seconds
uint64_t TIME_TO_SLEEP = 600;
// Replace with your network credentials
const char* ssid = \"REPLACE_WITH_YOUR_SSID\";
const char* password = \"REPLACE_WITH_YOUR_PASSWORD\";
// Define CS pin for the SD card module
#define SD_CS 5
// Save reading number on RTC memory
RTC_DATA_ATTR int readingID = 0;
String dataMessage;
// Data wire is connected to ESP32 GPIO 21
#define ONE_WIRE_BUS 21
// Setup a oneWire instance to communicate with a OneWire device
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature sensor
DallasTemperature sensors(&oneWire);
// Temperature Sensor variables
float temperature;
// Define NTP Client to get time
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP);
// Variables to save date and time
String formattedDate;
String dayStamp;
String timeStamp;
void setup() {
// Start serial communication for debugging purposes
Serial.begin(115200);
// Connect to Wi-Fi network with SSID and password
Serial.print(\"Connecting to \");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(\".\");
}
Serial.println(\"\");
Serial.println(\"WiFi connected.\");
// Initialize a NTPClient to get time
timeClient.begin();
// Set offset time in seconds to adjust for your timezone, for example:
// GMT +1 = 3600
// GMT +8 = 28800
// GMT -1 = -3600
// GMT 0 = 0
timeClient.setTimeOffset(3600);
// Initialize SD card
SD.begin(SD_CS);
if(!SD.begin(SD_CS)) {
Serial.println(\"Card Mount Failed\");
return;
}
uint8_t cardType = SD.cardType();
if(cardType == CARD_NONE) {
Serial.println(\"No SD card attached\");
return;
}
Serial.println(\"Initializing SD card...\");
if (!SD.begin(SD_CS)) {
Serial.println(\"ERROR - SD card initialization failed!\");
return; // init failed
}
// If the data.txt file doesn't exist
// Create a file on the SD card and write the data labels
File file = SD.open(\"/data.txt\");
if(!file) {
Serial.println(\"File doens't exist\");
Serial.println(\"Creating file...\");
writeFile(SD, \"/data.txt\", \"Reading ID, Date, Hour, Temperature \
\");
}
else {
Serial.println(\"File already exists\");
}
file.close();
// Enable Timer wake_up
esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR);
// Start the DallasTemperature library
sensors.begin();
getReadings();
getTimeStamp();
logSDCard();
// Increment readingID on every new reading
readingID++;
// Start deep sleep
Serial.println(\"DONE! Going to sleep now.\");
esp_deep_sleep_start();
}
void loop() {
// The ESP32 will be in deep sleep
// it never reaches the loop()
}
// Function to get temperature
void getReadings(){
sensors.requestTemperatures();
temperature = sensors.getTempCByIndex(0); // Temperature in Celsius
//temperature = sensors.getTempFByIndex(0); // Temperature in Fahrenheit
Serial.print(\"Temperature: \");
Serial.println(temperature);
}
// Function to get date and time from NTPClient
void getTimeStamp() {
while(!timeClient.update()) {
timeClient.forceUpdate();
}
// The formattedDate comes with the following format:
// 2018-05-28T16:00:13Z
// W
:
ESP32: How to Log Data (10 Different Ways)
Project Overview
Before getting started, let’s highlight the project’s main features:
The ESP32 reads temperature using the DS18B20 temperature sensor.
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.
The data (temperature and timestamp) are logged to a microSD card. To log data to the microSD card we’re using a microSD card module.
After completing these previous tasks, the ESP32 sleeps for 10 minutes.
The ESP32 wakes up and repeats the process.
Parts Required
Here’s a list of the parts required to build this project (click the links below to find the best price at
Maker Advisor
):
ESP32 DOIT DEVKIT V1 Board
–
read ESP32 Development Boards Review and Comparison
MicroSD card module
MicroSD card
DS18B20 temperature sensor
10k Ohm resistor
Jumper wires
Breadboard
You can use the preceding links or go directly to
MakerAdvisor.com/tools
to find all the parts for your projects at the best price!
Preparing the microSD Card Module
To save data on the microSD card with the ESP32, we use the following
microSD card module
that communicates with the ESP32 using SPI communication protocol.
Related content:
ESP32: Guide for MicroSD Card Module using Arduino IDE
Formatting the microSD card
When using a microSD card with the ESP32, you should format it first. Follow the next instructions to format your microSD card.
1.
Insert the microSD card in your computer. Go to
My Computer
and right click on the SD card. Select
Format
as shown in figure below.
2.
A new window pops up. Select
FAT32
, press
Start
to initialize the formatting process and follow the onscreen instructions.
Schematic
Follow the next schematic diagram to assemble the circuit for this project.
You can also use the following table as a reference to wire the microSD card module:
MicroSD Card Module
ESP32
3V3
3V3
CS
GPIO 5
MOSI
GPIO 23
CLK
GPIO 18
MISO
GPIO 19
GND
GND
The next figure shows how your circuit should look like:
Preparing the Arduino IDE
There’s 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’t already.
Installing ESP32 Board in Arduino IDE 2 (Windows, Mac OS X, Linux)
Installing Libraries
Before uploading the code, you need to install some libraries in the Arduino IDE. The
OneWire library by Paul Stoffregen
and the
Dallas Temperature library
so that you can use the DS18B20 sensor. You also need to install the
NTPClient library forked by Taranais
to make request to an NTP server.
Follow the next steps to install those libraries in your Arduino IDE:
OneWire library
In the Arduino IDE, go to
Sketch
>
Include Library
>
Manage Libraries
OneWire
Install the OneWire library as shown in the picture below.
Dallas Temperature library
In the Arduino IDE, go to
Sketch
>
Include Library
>
Manage Libraries
DallasTemperature
Install the
DallasTemperature
library by Miles Burton.
NTPClient library
IMPORTANT
: we’re not using the default NTPClient library. To follow this tutorial you need to install the library we recommend using the following steps.
We’ll be using the
NTPClient Library forked by Taranais
. Follow the next steps to install this library in your Arduino IDE:
Click here to download the NTP Client library
. You should have a .zip folder in your
Downloads
In your Arduino IDE, go to
Sketch
>
Include Library
>
Add . ZIP library
…
Select the .ZIP file of the library you just downloaded.
The library will be installed after a few seconds.
Uploading Code
Here’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.
// Libraries for SD card
#include \"FS.h\"
#include \"SD.h\"
#include `SPI.h`
//DS18B20 libraries
#include `OneWire.h`
#include `DallasTemperature.h`
// Libraries to get time from NTP Server
#include `WiFi.h`
#include `NTPClient.h`
#include `WiFiUdp.h`
// Define deep sleep options
uint64_t uS_TO_S_FACTOR = 1000000; // Conversion factor for micro seconds to seconds
// Sleep for 10 minutes = 600 seconds
uint64_t TIME_TO_SLEEP = 600;
// Replace with your network credentials
const char* ssid = \"REPLACE_WITH_YOUR_SSID\";
const char* password = \"REPLACE_WITH_YOUR_PASSWORD\";
// Define CS pin for the SD card module
#define SD_CS 5
// Save reading number on RTC memory
RTC_DATA_ATTR int readingID = 0;
String dataMessage;
// Data wire is connected to ESP32 GPIO 21
#define ONE_WIRE_BUS 21
// Setup a oneWire instance to communicate with a OneWire device
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature sensor
DallasTemperature sensors(&oneWire);
// Temperature Sensor variables
float temperature;
// Define NTP Client to get time
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP);
// Variables to save date and time
String formattedDate;
String dayStamp;
String timeStamp;
void setup() {
// Start serial communication for debugging purposes
Serial.begin(115200);
// Connect to Wi-Fi network with SSID and password
Serial.print(\"Connecting to \");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(\".\");
}
Serial.println(\"\");
Serial.println(\"WiFi connected.\");
// Initialize a NTPClient to get time
timeClient.begin();
// Set offset time in seconds to adjust for your timezone, for example:
// GMT +1 = 3600
// GMT +8 = 28800
// GMT -1 = -3600
// GMT 0 = 0
timeClient.setTimeOffset(3600);
// Initialize SD card
SD.begin(SD_CS);
if(!SD.begin(SD_CS)) {
Serial.println(\"Card Mount Failed\");
return;
}
uint8_t cardType = SD.cardType();
if(cardType == CARD_NONE) {
Serial.println(\"No SD card attached\");
return;
}
Serial.println(\"Initializing SD card...\");
if (!SD.begin(SD_CS)) {
Serial.println(\"ERROR - SD card initialization failed!\");
return; // init failed
}
// If the data.txt file doesn't exist
// Create a file on the SD card and write the data labels
File file = SD.open(\"/data.txt\");
if(!file) {
Serial.println(\"File doens't exist\");
Serial.println(\"Creating file...\");
writeFile(SD, \"/data.txt\", \"Reading ID, Date, Hour, Temperature \
\");
}
else {
Serial.println(\"File already exists\");
}
file.close();
// Enable Timer wake_up
esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR);
// Start the DallasTemperature library
sensors.begin();
getReadings();
getTimeStamp();
logSDCard();
// Increment readingID on every new reading
readingID++;
// Start deep sleep
Serial.println(\"DONE! Going to sleep now.\");
esp_deep_sleep_start();
}
void loop() {
// The ESP32 will be in deep sleep
// it never reaches the loop()
}
// Function to get temperature
void getReadings(){
sensors.requestTemperatures();
temperature = sensors.getTempCByIndex(0); // Temperature in Celsius
//temperature = sensors.getTempFByIndex(0); // Temperature in Fahrenheit
Serial.print(\"Temperature: \");
Serial.println(temperature);
}
// Function to get date and time from NTPClient
void getTimeStamp() {
while(!timeClient.update()) {
timeClient.forceUpdate();
}
// The formattedDate comes with the following format:
// 2018-05-28T16:00:13Z
// W
Technologies used
Hardware components
- ESP32
- Arduino
- DS18B20
- SD Card
Software & tools
- Arduino IDE
What you get
- Working project / hardware kit, tested before delivery
- Complete source code with explanation
- Project demo and guidance for your viva
- Project report, PPT and IEEE paper support (on request)
Delivery & Payment
Delivery takes a minimum of two weeks from the date your inquiry is confirmed.
Payment is in two parts:
• 50% advance to start the project
• Remaining 50% before delivery
Note:
• Documentation and PPT are not included by default
• If you need documentation/PPT, it can be arranged for extra time and cost
We'll confirm the exact timeline and cost with you after reviewing your requirements.