Room temperature and humidity logger with SD card
Build it step by step
0 of 7 doneWire the I2C devices
AHT20 and RTC share A4 and A5.
Wire the SD module
CS to D10 and SPI pins D11 to D13.
Format the card
FAT32, then insert it.
Upload
The first upload sets the clock. Open the serial monitor if you want to debug.
Wait for the first write
The green LED blinks once at start and then every five minutes.
Read the file
Power down, put the card in a computer and open LOG.CSV in a spreadsheet.
Chart
Select the temperature column and insert a line chart.
How do you build a room temperature and humidity logger with SD card?
An AHT20 sensor and an RTC log a reading every five minutes to a CSV file you can open in a spreadsheet. You need Arduino Nano, AHT20 temperature and humidity sensor, DS3231 RTC module, MicroSD Card Module, LED 5 mm green, Resistor 220 Ω, Breadboard 830 Tie Points, Jumper wires male to male (40 pcs, 20 cm) and USB A to micro-USB cable (data). It takes 7 steps and about 60 minutes.
- Level
- Beginner
- Time
- About 60 min
- Parts
- 9
Parts list
- Development BoardsBuyArduino Nano × 1
- SensorsBuy
- SensorsBuy
- InterfacesBuy
- SemiconductorsBuyLED 5 mm green × 1
- PassivesBuyResistor 220 Ω × 1
- PrototypingBuy
- PrototypingBuy
- Cables & WiresBuy
Tick what you already own. It saves to My Garage, so “Build with what I have” knows.
Wiring
| From | To | Why |
|---|---|---|
| AHT20 and RTC SDA, SCL | Nano A4 and A5 | Shared I2C bus |
| AHT20 and RTC VCC, GND | Nano 5V (RTC) and 3.3V or 5V as marked (AHT20), GND | Supplies |
| SD module CS | Nano D10 | Chip select |
| SD module MOSI, MISO, SCK | Nano D11, D12, D13 | SPI |
| SD module VCC and GND | Nano 5V and GND | Supply (5 V module with level shifting) |
| Green LED anode | Nano D7 through 220 Ω | Write indicator |
| Green LED cathode | Nano GND | Return |
Code
Arduino IDE, board "Arduino Nano" (processor ATmega328P, or ATmega328P (Old Bootloader) for clones). Install "Adafruit AHTX0", "RTClib". SD comes with the IDE. Format the card as FAT32. The first upload sets the RTC to the compile time, which is the time of upload, so upload soon after compiling. File names must be 8.3 style, like LOG.CSV.
Runs on a virtual Arduino Nano in your browser, with sliders for the sensors. Happy with it? Verify and upload to your real board from the same page (Chrome or Edge on a computer).
#include <Wire.h>
#include <Adafruit_AHTX0.h>
#include <RTClib.h>
#include <SD.h>
Adafruit_AHTX0 aht;
RTC_DS3231 rtc;
const byte CS = 10, LED = 7;
const unsigned long INTERVAL = 5UL * 60UL * 1000UL; // every five minutes
unsigned long last = 0;
bool first = true;
void logReading() {
sensors_event_t hum, temp;
aht.getEvent(&hum, &temp);
DateTime t = rtc.now();
File f = SD.open("LOG.CSV", FILE_WRITE);
if (f) {
if (f.size() == 0) f.println("date,time,temp_c,humidity_pct");
f.print(t.year()); f.print('-'); if (t.month() < 10) f.print('0'); f.print(t.month()); f.print('-'); if (t.day() < 10) f.print('0'); f.print(t.day()); f.print(',');
if (t.hour() < 10) f.print('0'); f.print(t.hour()); f.print(':'); if (t.minute() < 10) f.print('0'); f.print(t.minute()); f.print(',');
f.print(temp.temperature, 1); f.print(','); f.println(hum.relative_humidity, 0);
f.close(); // close every time so a power cut cannot corrupt the file
digitalWrite(LED, HIGH); delay(200); digitalWrite(LED, LOW);
}
}
void setup() {
pinMode(LED, OUTPUT);
Wire.begin();
aht.begin();
rtc.begin();
if (rtc.lostPower()) rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); // set the clock from the compile time
SD.begin(CS);
}
void loop() {
if (first || millis() - last >= INTERVAL) {
first = false; last = millis();
logReading();
}
}Test checklist
What you should see at each check. If a reading is far off, stop and use the troubleshooting list.
| Check | Expected |
|---|---|
| Start | Green LED blinks and a header line is written |
| After 15 minutes | Three data lines |
| Time column | Matches your clock |
| Pull the power and restart | File still intact and continues |
If it does not work
No file on the card
Wrong CS pin, a non-FAT32 card or a module without 5 V level shifting.
Time is wrong
The RTC lost power and was set at compile time. Re-upload to reset it.
Readings stuck
The sensor is not found. Check the I2C wiring.
LED never blinks
The SD card failed to open. Reseat the card.
Still stuck? Find the fault step by step with your multimeter →
Common questions
What parts do I need for the room temperature and humidity logger with SD card?
Arduino Nano, AHT20 temperature and humidity sensor, DS3231 RTC module, MicroSD Card Module, LED 5 mm green, Resistor 220 Ω, Breadboard 830 Tie Points, Jumper wires male to male (40 pcs, 20 cm) and USB A to micro-USB cable (data).
How long does the room temperature and humidity logger with SD card take to build?
About 60 minutes for a beginner maker, in 7 steps.
How do I know the room temperature and humidity logger with SD card works?
Start: Green LED blinks and a header line is written. After 15 minutes: Three data lines. Time column: Matches your clock.
No file on the card: what should I check?
Wrong CS pin, a non-FAT32 card or a module without 5 V level shifting.
Time is wrong: what should I check?
The RTC lost power and was set at compile time. Re-upload to reset it.
Readings stuck: what should I check?
The sensor is not found. Check the I2C wiring.
LED never blinks: what should I check?
The SD card failed to open. Reseat the card.
