Arduino Data Logger: How To Build a Temperature and Humidity Data Recorder Using Arduino

Arduino data logger is ideal for prototyping actual data recorders for data gathering and analysis. One of the easiest ways to build this device is by incorporating an external sensor as the data source. You’ll …

A digital humidity, barometric pressure, and temperature data logger

Arduino data logger is ideal for prototyping actual data recorders for data gathering and analysis.

One of the easiest ways to build this device is by incorporating an external sensor as the data source.

You’ll also need an SD card module to record the data.

In this article, we will build a data logger that captures temperature and humidity readings from a sensor.

The device will then store the data in an SD card and Excel file. Let’s get right into it!

Table of Contents

What Is a Data Logger?

A data logger is an electronic device that monitors, then records changes in data or conditions over time. So it has three functions.

  • Monitor data
  • Record data
  • Time stamp the data

The result is meaningful information that can highlight trends or behaviors, and you can view this data in real-time or later.

A GPS logger

A GPS logger

The data logger must have a sensor to monitor the conditions to provide the three functions above.

Also, it must have a storage/communication unit to record the data and a clock to time stamp the data.

Let’s look at how an Arduino board can integrate these modules to form a functional data logger.

How To Build an Arduino Data Logger

You’ll need the following components to build the project.

  • An Arduino board (we’ll use the Arduino UNO)
  • DHT11 temperature and humidity sensor
A DHT11 temperature and humidity sensor

A DHT11 temperature and humidity sensor

  • RTC module (DS3231)
  • SD card module
  • Breadboard
  • SD card
  • Jumper wires
  • Laptop/PC
A micro SD card module

A micro SD card module

It is vital to note that the DS3231 RTC module has a built-in temperature sensor.

But we’ll use the DHT11 because it is more accurate and doubles as a humidity sensor.

Alternatively, you can use the LM35 temperature sensor. But it lacks a humidity sensor unit.

Hardware Connections

Connect the components as shown below.

Arduino UNODHT11 Sensor
5VVcc
GNDGND
7Out
Arduino UNODS3231 Real-Time Clock Module
5VVcc
GNDGND
A5SCL
A4SDA
Arduino UNOMicro SD Card Module
5VVcc
GNDGND
4CS
11MOSI
12MISO
13SCK

Circuit Diagram

The Arduino board acts as the brains for the data logger; it coordinates the functions of the three modules to monitor and record the time-stamped data.

Code

With the hardware connected, we have to write code that monitors/reads temperature and humidity data from the environment using the DHT11 sensor.

After that, the code should get the time stamp from the RTC module when reading the data by initializing the I2C bus.

The Arduino board interfaces with the DS3231 via I2C communication (SCL and SDA).

An RTC module

An RTC module

Next, we must initialize the SPI bus (CS, MOSI, MISO, and SCK) to communicate with the SD card module.

This part initiates the storage of temperature, humidity, date, and time inside the SD card.

The stored data will be in CSV format. So you can import it into an Excel sheet.

But instead of importing the data, we’ll use third-party software to log the data to a spreadsheet simultaneously.

So you’ll need to download the PLX-DAQ software add-in for Microsoft Excel.

Also, download the DHT11 sensor and RTC (DS3231) module libraries before writing the code.

Install the add-in; it should create a PLX-DAQ folder on the desktop.

Next, import the libraries into your code using the library manager (go to Sketch>Include Library>Add ZIP Library).

Here’s the code.

Code Explanation

The core of the code is the top section, where we include the libraries, set up the devices, and call the functions.

This code structure (function calling) makes the program easy to read and understand.

So we begin by including the RTC module, SPI communication, SD card, and DHT11 sensor libraries.

Remember, we downloaded and imported the RTC module and DHT11 sensor libraries before coding.

But the SD card and SPI communication libraries come pre-loaded into Arduino.

After that, the code defines the following.

  • An Arduino pin connected to the sensor (pin 7)
  • A sensor object named DHT
  • A constant CS (chip select) integer for the Arduino pin connected to the SD card

Next, we initialize the RTC module using its SDA and SCL hardware interfaces.

An Arduino project with a DHT11 sensor

An Arduino project with a DHT11 sensor

We’ll set up the serial communication in the void setup () function at 9600 bits per second.

After that, we initialize the SD card, RTC module, and PLX-DAQ Excel add-in.

The void loop () function concludes the core of the code by calling the Read_DHT11, Write_SDcard, and Write_PlxDaq functions in that order.

So the program will read the sensor data, write this data on the SD card, then write the same on an Excel sheet.

After that, the program will wait for five seconds (delay (5000)), then repeat the void loop () function.

So you’ll get what the DHT11 temperature sensor reads every five seconds in the data files.

Let’s look at how the functions work.

Initializing the SD Card

Before initializing the SD card, the code checks if the card is available in the port using this code.

The code tries to initialize the card’s CS pin first. If unavailable, you’ll get the error message “card failed, or not present.”

But if available, you won’t get a message.

The program will proceed to the next step; opening a text file in the card to log the data.

The program will create a file named LoggerCD.txt, write the date, time, temperature, and humidity (separated by commas), then close the file.

Initializing the RTC Module

After that, the program initializes the RTC module clock object.

Also, it sets the day of the week, time (in a 24-hour format), and date.

You can leave this code section if you set the date and time the first time.

Alternatively, you can comment between a forward slash and an asterisk (/*code*/).

Initializing Excel File

Next, we initialize the Excel file using the PLX-DAQ add-in.

The function clears any data left from previous projects.

After that, it writes the label at the top in the same order as the SD card; date, time, temperature, and humidity.

Reading DHT11 Sensor Data

The first step in the void loop () function is to call the function that reads the DHT sensor data.

It is a simple function that uses the DHT object to read the sensor via the connected Arduino pin.

Remember the definition DHT11_PIN refers to pin 7.

The function stores the read sensor value in an integer called chk.

Writing the SD Card

After reading the sensor data, the code logs it in the SD card by opening the text file created earlier, then writing the following.

  • Date (obtained from the RTC)
  • Time reading from the RTC
  • Temperature (obtained from the DHT11)
  • Humidity data from the DHT11

The function separates the values using commas.

And the data will fall under the words date, time, temperature, and humidity written on the file earlier.

There will be a comma at the end then the code will end the row, skip to the following line, and close the file.

If there’s any error, you’ll get the message “SD card writing failed.”

SD and micro sd card with adapter

SD and micro sd card with adapter

Writing the Excel File

The last step is to write the data in the Excel file using the Write_PlxDaq function. It logs the data in the same format as in the SD card.

But if you look at the code keenly, we don’t use the RTC data to log the date and time.

Since the Excel file runs on your PC, you can log your computer’s date and time by referencing “DATE” and “TIME” in the function.

The program can recognize keywords like:

  • LABEL: To write the first row in the Excel sheet
  • DATA: To indicate the information logged afterward is data

Serial.println() with empty brackets signals the end of the row.

What To Expect

After running the code, the data logger will begin storing the temperature and humidity values in the SD card.

But the PLX-DAQ add-in might encounter a security block when attempting to write values into the Excel file.

Follow these steps to fix the issue.

  • Open the Excel file in the desktop folder created by the software during installation.
  • Go to Options>Enable Content>Finish>OK.
  • On the pop-up window, set the baud rate to 9600 on the Arduino-connected port, then tap the connect.

You should begin to see the logged values appear row after row every five seconds.

The SD card file should have similar values, but you can’t open it simultaneously with the program.

So unplug the card from the module, then connect and open it to your computer.

Look for the txt SD card file LoggerCD.txt, open it, and check the values.

You can open these CSV values in Excel or use the already created spreadsheet to create graphs or analyze the data to make more sense.

For instance, you can plot temperature against humidity, temperature against time, or humidity against time.

Please note that you should not try to use the serial monitor when running the PLX-DAQ Excel add-in.

Only create a serial connection to your computer.

Try To Go Wireless

You must maintain a USB connection between the Arduino board and your computer to log the data to the spreadsheet.

You can try to introduce a Bluetooth module to enable wireless communication between the UNO and your computer to log the data wirelessly.

With a module like the HC-05, change the “Serial.print” to “BluetoothName.print”, then connect your PC to the module.

After that, pick the COM port your PC’s Bluetooth connects to the HC-05.

Also, power the data logger using AA batteries to enable remote operations while sending the data to your computer.

Wrap Up

There you have it! Building a data logger using Arduino isn’t such a difficult task.

We hope this article has been insightful and easy to follow.

But if you encounter any challenges, contact us or comment below, and we’ll be in touch to assist.

Categories How