Arduino temperature and humidity data logging

Photo of author

By Jackson Taylor

Tracking environmental data is a vital aspect of many projects, whether you’re monitoring the climate of a greenhouse, conducting weather research, or building a smart home system. One of the best ways to achieve this is by using an Arduino microcontroller to log temperature and humidity data. In this article, we will walk you through the process of setting up an Arduino-based system for temperature and humidity data logging.

What is Arduino Temperature and Humidity Data Logging?

Arduino temperature and humidity data logging involves capturing data from environmental sensors, such as a DHT11 or DHT22 sensor, and storing that information for later use. This can be useful for a variety of applications, including climate monitoring, weather stations, or even creating a digital record of conditions for future analysis.

Why Use Arduino for Data Logging?

Arduino offers several advantages when it comes to data logging:

  • Affordability: Arduino boards are inexpensive, making them ideal for budget-conscious projects.
  • Flexibility: Arduino can be easily adapted to various sensors, and the open-source nature of the platform means you can find a plethora of libraries and community support.
  • Simplicity: Setting up an Arduino-based data logger is relatively simple, even for beginners.

Setting Up Your Arduino for Temperature and Humidity Data Logging

Before diving into the coding and sensor setup, let’s cover the basic components you will need to get started.

Required Components

  • Arduino Board: The Arduino Uno is a popular choice, but other boards can also work.
  • DHT11 or DHT22 Sensor: These sensors measure temperature and humidity. The DHT22 is more accurate and suitable for a wider range of temperatures.
  • Breadboard: For connecting your components without soldering.
  • Jumper Wires: To connect the sensor to the Arduino.
  • MicroSD Card Module: This will allow you to store the data from the sensor.
  • Resistor: A 10kΩ pull-up resistor is commonly used in the wiring of the DHT sensor.
  • Power Supply: A USB cable for powering the Arduino.
See also
Arduino cloud integration with Firebase

Wiring the Sensor to the Arduino

The DHT sensor has three pins: VCC (power), GND (ground), and DATA (for reading the data). The connections are as follows:

  • VCC to the 5V pin on the Arduino.
  • GND to the GND pin on the Arduino.
  • DATA to a digital input pin on the Arduino (usually pin 2).

Also, place the 10kΩ resistor between the VCC and DATA pins to ensure proper data transmission.

Arduino Code for Temperature and Humidity Logging

The heart of the project is the Arduino code that reads sensor data and stores it on an SD card. To do this, you’ll use the DHT library to interface with the sensor and the SD library for logging data to the microSD card.

Step-by-Step Code Breakdown

cpp
#include <DHT.h>
#include <SD.h>
#include <SPI.h>

#define DHTPIN 2 // Pin connected to the DHT sensor
#define DHTTYPE DHT22 // DHT 22 (AM2302)
#define SD_CS_PIN 10 // Chip select pin for SD card module

DHT dht(DHTPIN, DHTTYPE); // Initialize DHT sensor

File dataFile;

void setup() {
Serial.begin(9600); // Start serial communication
dht.begin(); // Start DHT sensor
if (!SD.begin(SD_CS_PIN)) { // Initialize SD card
Serial.println("SD card initialization failed!");
return;
}
Serial.println("SD card is ready.");
}

void loop() {
// Reading the temperature and humidity
float h = dht.readHumidity();
float t = dht.readTemperature();

// Check if the readings failed
if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from DHT sensor!");
return;
}

// Open the file for appending data
dataFile = SD.open("datalog.txt", FILE_WRITE);
if (dataFile) {
// Write the data
dataFile.print("Temperature: ");
dataFile.print(t);
dataFile.print(" °C ");
dataFile.print("Humidity: ");
dataFile.print(h);
dataFile.println(" %");
dataFile.close(); // Close the file after writing
Serial.println("Data written to SD card.");
} else {
Serial.println("Error opening datalog.txt.");
}
delay(2000); // Wait for 2 seconds before the next reading
}

Code Explanation

  • Libraries: The code begins by including the necessary libraries for the DHT sensor and the SD card.
  • Sensor Setup: The DHT sensor is initialized, and the SD card is checked for readiness.
  • Reading Sensor Data: The temperature and humidity values are read from the sensor.
  • Writing to SD Card: The data is then written to a file on the microSD card, with each reading recorded in a new line.
  • Delay: The loop waits for 2 seconds before taking the next reading, giving the sensor time to stabilize.

Understanding the Data Storage

When the Arduino reads temperature and humidity data, it stores this information on a microSD card in a text file (e.g., datalog.txt). You can later open this file using a computer or any text editor to analyze the data. Each entry will contain the temperature and humidity readings, providing valuable insight into environmental conditions over time.

Viewing the Data

Once your system has logged some data, you can remove the microSD card from the Arduino and view the file on your computer. You’ll see entries like this:

yaml
Temperature: 22.5 °C Humidity: 45.8 %
Temperature: 22.7 °C Humidity: 46.1 %

These readings can be analyzed manually or imported into data analysis software like Excel for more advanced analysis.

Expanding Your Arduino Data Logging System

While this basic setup provides the essentials, there are several ways you can expand your Arduino temperature and humidity logging system to make it more advanced.

1. Adding Multiple Sensors

If you need to monitor different locations, you can add more DHT sensors and modify the code to handle multiple sensors at once. Each sensor would be connected to a different pin, and the code would need to read from each sensor sequentially.

2. Real-Time Monitoring

Instead of logging data to an SD card, you could modify the system to send the temperature and humidity data to an online server or cloud service in real time. This would allow you to monitor the data remotely using a web interface or app.

3. Adding a Display

Integrating a small display like an LCD or OLED screen could allow you to view real-time temperature and humidity readings directly on the Arduino system, making it more interactive.

Conclusion

Arduino-based temperature and humidity data logging is an accessible and powerful way to monitor environmental conditions. By using affordable sensors like the DHT11 or DHT22 and combining them with Arduino’s capabilities, you can create a customizable system for a wide variety of applications. Whether you’re tracking climate data, monitoring air quality, or creating a DIY weather station, Arduino offers a simple, cost-effective solution for logging essential environmental data. By following this guide, you can easily set up your own data logger, start recording valuable information, and explore the countless possibilities that come with this technology. Happy logging!