Arduino Mega Temperature and Humidity Monitor With TFT Display and SD Logging
by vandenbrande in Circuits > Arduino
43 Views, 0 Favorites, 0 Comments
Arduino Mega Temperature and Humidity Monitor With TFT Display and SD Logging
Hello world! Today i made a temperature and humidity monitor with an Arduino Mega 2560 and a 3.2-inch colour TFT display.
I wanted a simple home monitor with a clear screen, a clock and readings that I can save to an SD card. A DHT22 measures room temperature and humidity, while a DS3231 keeps the date and time. The display also shows minimum and maximum room temperatures since startup and the temperature measured inside the RTC chip.
The blue background is loaded from the SD card. Instead of clearing the whole screen for every update, the code restores small background sections before drawing new values. A CSV file records readings every minute.
Follow the steps below for the parts list, wiring, libraries, SD graphics and Arduino code. This version displays Celsius. The min/max values reset when the Arduino restarts; they are not daily values.
Supplies
PARTS LIST
• Arduino Mega 2560 and USB cable
• HUGA 3.2-inch 480×320 TFT LCD shield for Arduino Mega 2560, working with the ILI9481 driver used in this sketch
• DHT22 / AM2302 three-pin sensor module, labelled DAT, VCC and GND
• DS3231 real-time-clock module
• Compatible backup battery for your particular RTC module
• SD card formatted FAT32 and a card reader
• Jumper wires and a regulated USB power source
SOFTWARE
Arduino IDE with Arduino AVR Boards installed. Libraries: UTFT 2.8.1, UTFT_SdRaw 1.2.4, SdFat 2.3.0, RTClib 2.1.4, DHT sensor library 1.4.7, Adafruit Unified Sensor 1.1.15 and Adafruit BusIO 1.17.4. Wire and SPI come with the board package.
A different TFT shield may need a different driver or pin mapping. Match the hardware shown here before using this sketch.
Connect the Sensor, Clock and TFT Shield
Disconnect USB power while making connections. Use the wiring diagram above; it shows signal names rather than physical header positions.
DHT22 MODULE → MEGA
DAT → D2
VCC → 5V
GND → GND
DS3231 MODULE → MEGA
SDA → D20 / SDA
SCL → D21 / SCL
VCC → 5V
GND → GND
Leave 32K and SQW disconnected. All modules share GND. Follow the printed labels, not the wire colours. These instructions are for the three-pin DHT22 module shown in the photos, not a bare four-pin sensor.
Fit the HUGA TFT shield to the matching Mega headers with every pin aligned. The sketch uses UTFT myGLCD(ILI9481, 38, 39, 40, 41): RS D38, WR D39, CS D40 and reset D41. The shield handles the parallel display connections. Its SD interface uses Mega SPI pins D50–D52 and chip select D53.
The shield routing is inferred from the configured driver, not a continuity measurement of every trace. Do not use this as a pin-by-pin guide for a bare LCD.
Downloads
Prepare the SD Card and Background Graphics
Use the BLUECORE_TECH_FLICKERFREE SD-card files supplied with this tutorial. These are the matching graphics for the downloadable sketch and the files used on my SD card.
Back up your card before formatting it as FAT32. Copy the contents of BLUECORE_TECH_FLICKERFREE directly to the SD card root, not the folder itself.
The complete pack contains these 11 files:
BG.RAW
DATE.RAW
TIME.RAW
TEMP.RAW
MINMAX.RAW
HUM.RAW
RTC.RAW
BG.BMP
DATA.CSV
CONFIG.TXT
README.TXT
The seven RAW files are required by the sketch. BG.RAW is the full 480×320 background; DATE, TIME, TEMP, MINMAX, HUM and RTC restore the background behind each changing value. They use RGB565, high-byte-first format. Keep the exact filenames above if a download is renamed. Renaming a BMP to RAW does not convert it.
BG.BMP is a reference image. DATA.CSV starts with the log header; the sketch appends readings and can create the file if missing. CONFIG.TXT and README.TXT are included with the original pack for reference. This sketch does not read CONFIG.TXT: change settings in the Arduino code.
Download the attached files below; BG.BMP is shown as an image. Insert the prepared card into the TFT shield before powering on. A successful startup prints SD OK in Serial Monitor at 9600 baud.
Install the Libraries and Upload the Code
IF YOU HAVE A SCREEN With inverted colors, Probably with v2.0 Printed on it, then use the BlueCoreTemperatureMonitorV2Screens.ino
Save the sketch as BlueCoreTemperatureMonitor.ino inside a folder named BlueCoreTemperatureMonitor. If Instructables renames a download, restore that exact filename.
Install the library versions listed in Supplies. UTFT is the base graphics library; UTFT_SdRaw is a separate add-on and cannot replace it. The Adafruit DHT and RTClib libraries also require Adafruit Unified Sensor and Adafruit BusIO. Wire and SPI are included with Arduino AVR Boards. Preserve third-party license notices.
Select Arduino Mega or Mega 2560, ATmega2560 and your port in Arduino IDE. Click Verify, then Upload. Open Serial Monitor at 9600 baud.
The supplied code was compile-checked for the Mega: 53,756 bytes of flash and 1,665 bytes of global RAM. It reads the DHT22 every 2.5 seconds and logs every 60 seconds. This compile check does not guarantee compatibility with a different display shield.
Library credits: UTFT by Henning Karlsen / Rinky-Dink Electronics; UTFT_SdRaw by Graham Lawrence; SdFat by Bill Greiman; RTClib, DHT sensor library, Unified Sensor and BusIO by their credited Adafruit contributors. Each library keeps its own license.
Set the Clock, Then Disable the Startup Reset
The downloadable sketch contains this clock-setting line in setup():
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
Upload once with that line active to set the DS3231 approximately to the computer’s compile time. Check that your computer’s date and local time are correct. Upload and startup delays mean this is not an exact time synchronisation.
After setting the clock, comment out that line by placing // at its beginning and upload again. Also remove or comment the message RTC TIME RESET TO COMPILE TIME so the Serial Monitor output remains accurate.
If you leave the adjustment active, EVERY reset or power cycle sets the clock back to the original compile time. A suitable working RTC backup battery is needed to retain time without main power. Check your module’s battery/charging requirements before fitting a cell.
The clock uses local time as set by you. This sketch does not automatically change daylight-saving time.
Read the Display and Open the Data Log
The large centre value is the DHT22 room temperature in Celsius. Humidity appears at the lower left. The lower-right temperature comes from the DS3231 chip; it can differ from room temperature and is not a second calibrated ambient sensor.
MIN and MAX record the lowest and highest valid DHT temperatures since startup. They reset when you restart the Arduino, not at midnight. The opening comment in the supplied sketch says daily, but the actual code tracks since startup.
Every minute, valid readings are appended to DATA.CSV with these columns:
DATE,TIME,TEMP_C,HUMIDITY,RTC_TEMP_C
Power off before removing the card. Open DATA.CSV in a spreadsheet to view or plot readings. If needed, choose comma as the separator and day-month-year for dates.
Keep the DHT22 away from the display’s heat and direct sunlight for more representative room readings. On a failed sensor read, the screen retains its previous values and logging is skipped.
Troubleshooting and Finishing Up
Missing UTFT.h: install the base UTFT library, not only UTFT_SdRaw. Missing DHT.h: install Adafruit DHT sensor library and its dependencies.
White or blank TFT: power off, check shield alignment and confirm that your screen matches the ILI9481 driver and Mega pin mapping. A display of the same size can use a different controller.
SD FAILED or missing graphics: check the card format, D53 chip select, filenames and root-directory placement. Use the matching RAW crop files; do not substitute ordinary BMP files.
DHT ERROR: check DAT on D2, VCC, GND and DHT22 sensor type. Allow the sensor time between readings.
RTC ERROR: check SDA on D20 and SCL on D21. Time returning to the upload date: disable the startup rtc.adjust line as explained in Step 4 and check the backup battery.
No CSV entries: both the RTC and DHT must be working and the SD card must be available. Wait at least one minute.
Enjoy building your own room monitor! You can customise the background, add an enclosure or extend the logging. The downloadable sketch is preserved as demonstrated; read the clock and min/max notes before using it unattended.
Created by Danny van den Brande — BlueCore Tech.