Raspberry Pi with DHT11/DHT22: Temperature and Humidity Data Logger (Python)

In this project, you’re going to build a data logger with the Raspberry Pi and the DHT11/DHT22 sensor that automatically stores temperature, humidity, and the corresponding timestamp on a .txt file. This project gives you the basics of data collection, which is useful in many different applications that use sensors. You can apply the concepts from this project to any sensor.

Raspberry Pi with DHT11 DHT22 Temperature and Humidity Data Logger Python

New to the DHT11 or DHT22 temperature sensor? Read our getting started guide: Raspberry Pi: DHT11/DHT22 Temperature and Humidity (Python)

Table of Contents

Prerequisites

Before continuing with this tutorial, check the following prerequisites.

  1. Get familiar with the Raspberry Pi board—if you’re not familiar with the Raspberry Pi, you can read our Raspberry Pi Getting Started Guide here.
  2. You must know how to run and create Python files on your Raspberry Pi. We like to program our Raspberry Pi via SSH using an extension on VS Code. We have a detailed tutorial about that subject: Programming Raspberry Pi Remotely using VS Code (Remote-SSH).
  3. Know how to use the Raspberry Pi GPIOs so that you know how to wire the circuit properly. Read the following tutorial: Raspberry Pi Pinout Guide: How to use the Raspberry Pi GPIOs?

Introducing the DHT11/DHT22 Sensor

The DHT11 and DHT22 sensors are used to measure temperature and relative humidity. These are very popular among makers and electronics hobbyists.

DHT11/DHT22 Temperature and Humidity Sensors

These sensors contain a chip that does analog-to-digital conversion and spit out a digital signal with the temperature and humidity. This makes them very easy to use with any microcontroller.

For a more detailed introduction to the DHT11/DHT22 sensor, read our getting started guide:

Parts Required

Raspberry Pi with DHT22 Circuit

Here’s a list of parts you need to build the circuit (if you don’t have a DHT breakout board, you need a 4.7kOhm resistor):

You can use the preceding links or go directly to MakerAdvisor.com/tools to find all the parts for your projects at the best price!

Wiring the DHT11/DHT22 Sensor to the Raspberry Pi

Wire the DHT22 or DHT11 sensor to the Raspberry Pi as shown in the following schematic diagram. If you have a DHT breakout board, ignore the resistor.

Raspberry Pi DHT Sensor Wiring

In this example, we’re connecting the DHT data pin to GPIO 4. However, you can use any other suitable digital GPIO. Learn more about the Raspberry Pi GPIOs here.

Getting DHT11/22 Temperature and Humidity

There are different ways to get temperature and humidity readings from the DHT11 or DHT22 sensors using the Raspberry Pi with Python. We’ll use the Adafruit_CircuitPython_DHT Python library.

Update your Raspberry Pi

First, update and upgrade your Raspberry Pi, if any updates are available. Run the following command:

sudo apt update && sudo apt upgrade

If there’s a need to update, it will ask you if you want to continue. Click Y and Enter to proceed. You may need to wait a few minutes if it needs to update.

Create a Virtual Environment

We’ll install the DHT library in a virtual environment. Creating a virtual environment will isolate the Python libraries we’re using, in this case, the DHT library, from the rest of the system.

We’ll create our virtual environment on a directory on our Desktop. Enter the following command on a Terminal window to move to the Desktop:

cd ~/Desktop

Create a folder for your project. This is where we’ll create the virtual environment and install the library. We’ll create a folder called dht_test.

mkdir dht_test

Move to the newly created folder:

cd ~/Desktop/dht_test

Create a virtual environment for this directory called myenv. This must be the same directory where we’ll install the DHT library. Replace myenv with the desired name for your virtual environment.

python3 -m venv myenv

Then, you can run the following command to check that the virtual environment is there.

ls -l
Create a Virtual Environment Raspberry Pi Python

Activate the virtual environment:

source myenv/bin/activate

Your prompt should change to indicate that you are now in the virtual environment.

Activate Python Virtual Environment

Installing the Adafruit_CircuitPython_DHT Library

Now that we are in our virtual environment, we can install the library. Run the following command:

python3 -m pip install adafruit-circuitpython-dht

After a few seconds, the library will be installed (ignore any yellow warnings about deprecated packages).

Installing the Adafruit_CircuitPython_DHT Library

Now, you have everything ready to start writing your Python code.

Raspberry Pi DHT Datalogger Python Script

Create a new Python file on your Raspberry Pi called dht-datalogger.py and copy the following code. It must be saved on the same folder where you created the virtual environment (in our case Desktop/dht_test)

# Complete Project Details: https://RandomNerdTutorials.com/raspberry-pi-temperature-humidity-data-logger/

import adafruit_dht
import board
import time
import os

# Sensor data pin is connected to GPIO 4
sensor = adafruit_dht.DHT22(board.D4)
# Uncomment for DHT11
#sensor = adafruit_dht.DHT11(board.D4)

# create a variable to control the while loop
running = True

# Check if the file exists before opening it in 'a' mode (append mode)
file_exists = os.path.isfile('sensor_readings.txt')
file = open('sensor_readings.txt', 'a')

# Write the header to the file if the file does not exist
if not file_exists:
    file.write('Time and Date, temperature (ºC), temperature (ºF), humidity (%)\n')

# loop forever
while running:
    try:
        # read the humidity and temperature
        temperature_c = sensor.temperature
        temperature_f = temperature_c * (9 / 5) + 32
        humidity = sensor.humidity

        #print readings on the shell
        print("Temp={0:0.1f}ºC, Temp={1:0.1f}ºF, Humidity={2:0.1f}%".format(temperature_c, temperature_f, humidity))

        # save time, date, temperature, and humidity in .txt file
        file.write(time.strftime('%H:%M:%S %d/%m/%Y') + ', {:.2f}, {:.2f}, {:.2f}\n'.format(temperature_c, temperature_f, humidity))

        # log new readings every 10 seconds
        time.sleep(10)

    except RuntimeError as error:
        # Errors happen fairly often, DHT's are hard to read, just keep going
        print(error.args[0])
        time.sleep(2.0)
        continue

    except KeyboardInterrupt:
        print('Program stopped')
        running = False
        file.close()

    except Exception as e:
        print('An unexpected error occurred:', str(e))
        running = False
        file.close()
        sensor.exit()

View raw code

How the Code Works

Here’s a quick description of what the code does:

  • it saves sensor readings and the corresponding timestamp to a file called sensor_readings.txt.
  • but first, it checks if a file called sensor_readings.txt already exists (this is to prevent writing the file header multiple times when the program resets or stops/starts).
  • if the file doesn’t exist, it will create a new file.
  • there’s an infinite loop, in which you get temperature and humidity readings and write those to the file with the corresponding timestamp.
  • the program keeps running and writing to the file until you tell the program to stop with a keyboard interrupt or until the program stops for any other reason. In that case, we close the file.

Continue reading for a more detailed explanation of the code, or skip to the demonstration section,

The code is very similar to the one in this guide (but it adds some lines for datalogging).

Import the Required Libraries

First, you need to import the required libraries. The adafruit_dht to get readings from the DHT sensor, the board module to interact with the GPIOs, the time module to get timestamps and add delays to your code, and the os module that will allow us to interact with the operating system (in our case, we need to check if a file already exists on the filesystem).

import adafruit_dht
import board
import time
import os

DHT Sensor

On the following line, we initialize our DHT sensor. We are using a DHT22 connected to GPIO 4 (you can use any other suitable GPIO as long as you change the code on the following line):

# Sensor data pin is connected to GPIO 4
sensor = adafruit_dht.DHT22(board.D4)

If you’re using a DHT11, it should be like this:

sensor = adafruit_dht.DHT11(board.D4)

From now on, we’ll refer to our DHT sensor as sensor in our code.

File Handling

The following line, checks if a file called sensor_readings.txt already exists (it will look for in the project folder path). If the file already exists, the file_exists variable will be True. Otherwise, it will be False.

file_exists = os.path.isfile('sensor_readings.txt')

Then, it opens the file in ‘a’ (append) mode. If the file doesn’t exist yet, it will automatically create the file before opening it.

file = open('sensor_readings.txt', 'a')

When using the open() function, you can specify different modes to control how the file is accessed and used. When writing to the file, the most popular methods are ‘w’ (write) and ‘a’ (append).

Write vs Append mode: the write mode opens the file for writing and empties the file if it already exists. The append mode opens the file for writing and it appends new data to the end of the file. If you want to have a record of your readings over time, the best mode is append so that you can keep all your previous data.

If the file doesn’t exist yet, the code writes a header line to the file, containing the column names. We’ll save date and time first, then, temperature in Celsius and Fahrenheit, and finally the humidity.

if not file_exists:
    file.write('Time and Date, temperature (ºC), temperature (ºF), humidity (%)\n')

Getting Sensor Readings and Data Logging

Then, we have a while loop that is always running as long as the running variable is True. We have set that variable to True at the beginning of the code. The running variable will change to False, when the execution of the program is stopped by a keyboard interrupt, or when an unexpected error occurs.

while running:
    try:

Inside the loop, the try block attempts to read the humidity and temperature from the DHT sensor and saves the readings in the humidity and temperature_c variables. We also convert the temperature to Fahrenheit.

 temperature_c = sensor.temperature
 temperature_f = temperature_c * (9 / 5) + 32
 humidity = sensor.humidity

All readings are printed in the shell.

print("Temp={0:0.1f}ºC, Temp={1:0.1f}ºF, Humidity={2:0.1f}%".format(temperature_c, temperature_f, humidity))

Then, we use the write() function on the file object to write data to the file. We print the timestamp, temperature in Celsius, temperature in Fahrenheit, and humidity.

file.write(time.strftime('%H:%M:%S %d/%m/%Y') + ', {:.2f}, {:.2f}, {:.2f}\n'.format(temperature_c, temperature_f, humidity))

To get a timestamp, we use time.strftime(“%H:%M:%S %d/%m/%Y”)—the argument in the parentheses here indicates the format you want the time and date to appear in: hours, minutes, seconds, day, month, and year, respectively.

This script reads and records the temperature and humidity every 10 seconds, but you can change that by changing the delay time. The sensor is capable of taking readings every 2 seconds, but
no faster than that.

 time.sleep(10)

Interrupt and Exception Handling

In case there is an error reading from the DHT sensor, we print the error message, pause for two seconds, and use the continue statement to go to the next iteration of the try block.

except RuntimeError as error:
    # Errors happen fairly often, DHT's are hard to read, just keep going
    print(error.args[0])
    time.sleep(2.0)
    continue

If you interrupt the program by pressing Ctrl+C, the code catches the KeyboardInterrupt exception, prints a message indicating program termination, and then closes the file before setting running to False to exit the loop.

except KeyboardInterrupt:
    print('Program stopped')
    running = False
    file.close()

Important: you need to call file.close() so that all data is written to the file.

We proceed similarly if any other error or exception occurs.

except Exception as e:
    print('An unexpected error occurred:', str(e))
    running = False
    file.close()

Demonstration

Save your Python file on the same directory of your virtual environment. Then run it on your Raspberry Pi. Run the following command (on the same path of the virtual environment):

python dht-datalogger.py

You’ll start getting new temperature and humidity readings on the terminal window, and a new file called sensors_readings.txt will be created in your project folder. If you’re using VS Code, you’ll see the newly created file on the file explorer (don’t open it yet).

Raspberry Pi Datalogger VS Code

Let the code run for a while so it gathers a considerable amount of data. Then, stop the execution of the code by pressing CTRL+C.

Raspberry Pi DHT datalogger keyboard interrupt

Now, you can open the sensors_readings.txt file and see all the data it collected. You can run the program again to gather more data and it won’t overwrite the previous data.

Sensor Readings Data Logger Raspberry Pi VS Code

If you’re using PuTTY, navigate to your project folder. List all files inside the project folder using the ls command and note that a file called sensor_readings.txt is listed.

Raspberry Pi Datalogger List Files PuTTY

To open that file, use the following command:

cat sensor_readings.txt

Then, you’ll see all data gathered by your sensor with the corresponding timestamps.

Raspberry Pi Datalogger File txt Putty

Wrapping Up

In this project, you’ve learned a very useful concept: data logging. Now you can use data logging in other monitoring projects. Here are some ideas:

  • Use a PIR motion sensor that saves a timestamp every time it detects movement;
  • Duplicate this project, but use a different sensor— we have an example for the BME280;
  • Build a weather station data logger using more sensors;
  • Search for other monitoring sensor applications, for example, soil moisture, rain, and light sensors to build a greenhouse data logger.

We hope you found this tutorial useful. If you’re quite new to the Raspberry Pi, check the following tutorials:

You can check all our Raspberry Pi projects on the following link:



Learn how to build a home automation system and we’ll cover the following main subjects: Node-RED, Node-RED Dashboard, Raspberry Pi, ESP32, ESP8266, MQTT, and InfluxDB database DOWNLOAD »
Learn how to build a home automation system and we’ll cover the following main subjects: Node-RED, Node-RED Dashboard, Raspberry Pi, ESP32, ESP8266, MQTT, and InfluxDB database DOWNLOAD »

Recommended Resources

Build a Home Automation System from Scratch » With Raspberry Pi, ESP8266, Arduino, and Node-RED.

Home Automation using ESP8266 eBook and video course » Build IoT and home automation projects.

Arduino Step-by-Step Projects » Build 25 Arduino projects with our course, even with no prior experience!

What to Read Next…


Enjoyed this project? Stay updated by subscribing our newsletter!

Leave a Comment

Download Our Free eBooks and Resources

Get instant access to our FREE eBooks, Resources, and Exclusive Electronics Projects by entering your email address below.