In this project, we will build a temperature, humidity, and dew point data logger with ESP32. This system will show the current time on a 20×4 LCD along with temperature, humidity, and dew point and periodically log the data on an SD card. In the design, an ESP32 microcontroller is used, together with a DS3231 RTC module for correct time representation and a CHT8305 sensor for temperature and humidity readings.
The temperature, humidity, and dew point data logger with ESP32 is a versatile project useful in applications such as environmental monitoring, greenhouse automation, and weather tracking.
Components Required for Build a Temperature, Humidity, and Dew Point Data Logger with ESP32:
- ESP32 Development Board
- 20×4 I2C LCD Display
- DS3231 RTC Module
- SD Card Module
- CHT8305 Temperature and Humidity Sensor
Circuit Diagram
To build a temperature, humidity, and dew point data logger with ESP32, you will need to connect the components to the ESP32 as follows:
- LCD (I2C):
- SDA (LCD) to GPIO 21 (ESP32)
- SCL (LCD) to GPIO 22 (ESP32)
- RTC Module (DS3231):
- SDA (RTC) to GPIO 21 (ESP32)
- SCL (RTC) to GPIO 22 (ESP32)
- SD Card Module:
- CS (SD) to GPIO 5 (ESP32)
- MOSI to GPIO 23
- MISO to GPIO 19
- SCK to GPIO 18
- CHT8305 Sensor:
- SDA (CHT8305) to GPIO 21 (ESP32)
- SCL (CHT8305) to GPIO 22 (ESP32)
Description of System:
The current time is read from the DS3231 RTC, and the temperature and humidity data are read from the CHT8305 sensor. Then, it would use the Magnus formula to calculate the dew point. It displays it on the 20×4 LCD while logging every 5 minutes into a text file in an SD card inside the device. An ESP32-based Temperature and Humidity Data Logger is best suited for acquiring time-stamped environmental data.
Interfacing the CHT8305 Temperature and Humidity Sensor
The CHT8305 sensor is an I2C-based sensor designed for temperature and humidity measurement. It is one of the significant elements comprising an ESP32 data logger. For an in-depth tutorial regarding interfacing the CHT8305 sensor with ESP32, you can refer to this tutorial. Interfacing the CHT8305 Sensor with ESP32: Real-Time Temperature and Humidity Display
Using the DS3231 RTC for Timekeeping
The DS3231 RTC module is a key component in this project, as it gives the data logged a timestamp. If you want to know how to set up the DS3231 RTC with the ESP32, this is the guide you would want to look at. How to Set Up a Real-Time Clock (RTC) with ESP32 and DS3231 Using NTP Time Synchronization
Interfacing the I2C LCD with ESP32
In our circuitry, we will use a 20×4 I2C LCD to display the current date, time, temperature, humidity, and dew point. An article on interfacing I2C LCDs with ESP32 and other microcontrollers may be found here. How to Interface Multiple I2C LCD to Arduino
SD Card Logging with ESP32
It logs the data to the SD card module. The temperature, humidity, and dew point data logger with ESP32 writes each reading along with its timestamp in a text file after every 5 minutes. Learn more about interfacing of SD card with ESP32 here in this tutorial. SD Card Interfacing with ESP32
Figure 2: Author Prototype of Temperature Humidity and Dew Point Data Logger with ESP32
Software Code Walkthrough
In this project, the ESP32 handles reading from the sensor, formatting of the data, display on the LCD, and logging to the SD card. Here’s a complete code for a temperature, humidity, and dew point data logger with ESP32:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
#include <Wire.h> #include <LiquidCrystal_I2C.h> #include <SD.h> #include <RTClib.h> // Library for DS3231 RTC #define CHT8305_I2C_ADDRESS 0x40 // I2C address of the CHT8305 sensor #define SD_CS_PIN 5 // Chip select pin for SD card LiquidCrystal_I2C lcd(0x27, 20, 4); RTC_DS3231 rtc; unsigned long previousMillis = 0, interval = 60000; uint8_t buf[4]; uint16_t tempData, humData; float temperature, humidity, dewPoint; // Inline function to read the CHT8305 sensor inline uint8_t readCHT8305(uint8_t reg, uint8_t* buffer, size_t size) { Wire.beginTransmission(CHT8305_I2C_ADDRESS); Wire.write(reg); if (Wire.endTransmission()) return 0; delay(20); Wire.requestFrom(CHT8305_I2C_ADDRESS, (uint8_t)size); for (uint16_t i = 0; i < size; i++) buffer[i] = Wire.available() ? Wire.read() : 0; return size; } // Inline function to calculate the dew point using Magnus formula inline float calculateDewPoint(float temp, float hum) { const float a = 17.27, b = 237.7; float alpha = ((a * temp) / (b + temp)) + log(hum / 100.0); return (b * alpha) / (a - alpha); } // Inline function to display time on the LCD inline void displayTime(DateTime now) { lcd.setCursor(0, 0); lcd.printf("%04d/%02d/%02d %02d:%02d:%02d", now.year(), now.month(), now.day(), now.hour(), now.minute(), now.second()); } void setup() { Serial.begin(115200); Wire.begin(); lcd.init(); lcd.backlight(); lcd.print("Temp, Humidity,"); lcd.setCursor(0, 1); lcd.print("Dew Point Logger"); delay(2000); lcd.clear(); lcd.print("Initializing..."); if (!SD.begin(SD_CS_PIN)) { lcd.print("SD Card Failed"); Serial.println("SD Card Init Failed"); return; } if (!rtc.begin()) { lcd.print("RTC Failed"); Serial.println("Couldn't find RTC"); return; } if (rtc.lostPower()) rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); lcd.clear(); lcd.print("SD and RTC OK"); delay(2000); lcd.clear(); } void loop() { DateTime now = rtc.now(); displayTime(now); if (readCHT8305(0x00, buf, 4)) { tempData = (buf[0] << 8) | buf[1]; humData = (buf[2] << 8) | buf[3]; temperature = ((float)tempData * 165 / 65535.0) - 40.0; humidity = ((float)humData / 65535.0) * 100; dewPoint = calculateDewPoint(temperature, humidity); lcd.setCursor(0, 1); lcd.printf("Temperature: %.1f%cC", temperature, (char)223); lcd.setCursor(0, 2); lcd.printf("Humidity: %.1f%%", humidity); lcd.setCursor(0, 3); lcd.printf("Dew Point: %.1f%cC", dewPoint, (char)223); if (millis() - previousMillis >= interval) { previousMillis = millis(); File dataFile = SD.open("/datalog.txt", FILE_APPEND); // Use FILE_APPEND to keep old data if (dataFile) { dataFile.printf("%04d/%02d/%02d %02d:%02d:%02d, Temp: %.1f C, Humidity: %.1f%%, Dew Point: %.1f C\n", now.year(), now.month(), now.day(), now.hour(), now.minute(), now.second(), temperature, humidity, dewPoint); dataFile.close(); Serial.println("Data logged successfully."); } else { lcd.setCursor(0, 3); lcd.print("File Write Error"); Serial.println("Error writing to datalog.txt"); } } } else { lcd.setCursor(0, 1); lcd.print("Sensor Read Error"); Serial.println("Failed to read from CHT8305."); } delay(1000); // Update sensor readings every 1 second } |
Dew Point Calculation:
Coming to the calculation of dew in the ESP32-based temperature, humidity, and dew point data logger, the Magnus formula relates both the temperature and humidity in calculating the dew point:Â
const float a = 17.27, b = 237.7;
float alpha = ((a * temp) / (b + temp)) + log(hum / 100.0);
return (b * alpha) / (a – alpha);
Figure 3: Data Sample Saved in SD Card
This tutorial is going to take you through building a temperature, humidity, and dew point data logger using ESP32. The system will be displaying real-time environmental data on an LCD while logging that data on an SD card for analysis later. Also, thanks to the DS3231 RTC module, accurate timestamps enable one to track the changes in environmental parameters over time.
PCB Diagram:
Download the PCB solder side and component side from the link provided below.
For more elaborative details and tutorials on the interfacing of the components used in this project, refer to the link.