Complete Guide Temperature and Humidity  Raspberry Pi Data Logger 

Wiring the DHT22 sensor to the Pi

This project will learn how to create data logger with raspberry pi; that men’s temperature and humanities can be automatically stored. And learn more about how to read data from environments that used in your myriad applications. 

≡Required Component:

 You have to need the following component for Raspberry Pi Data Logger project.

  1. Raspberry Pi
  2. Breadboard
  3. DHT22 temperature and humidity sensor (DHT11 and AM2302 also work)
  4. 4.7 kΩ resistor
  5. Jumper wires


≡Required software:

You have to need the following software for Raspberry Pi Data Logger project.

Adafruit_Python_DHT library

DHT22 will use temperature and humidity sensors to collect data, stored in a .txt file. And can be used to create other visualisations.

This project will show the basics of data collection used in different sensor applications in different applications such as monitoring soil dampness, controlling a place’s temperature, etc.

≡Introduction to DHT22 SENSOR:

(shown in Figure 12-1)  a digital temperature and humidity sensor with a built-in chip convert analog to the digital signal, so there is no need to use analog to digital converter IC.

 DHT22 temperature and humidity sensor
FIGURE 1: DHT22 temperature and humidity sensor


≡Circuit Diagram Data Logger Raspberry Pi:

This is a simple circuit that simply raspberry pi through the register. Follow these instructions using the circuit diagram in Figure 2 as a reference.

  1. Connect GND and 3.3V to the raspberry pi on the blue and red power rails on the breadboard, respectively.
  2. Then connect the DHT22 sensor to the raspberry pi according to the table below, starting the pins from left to right. Be sure to wire the register between pin 2 of the sensor and the red power rail on the breadboard.
Wiring the DHT22 sensor to the Pi
FIGURE 2: Wiring the DHT22 sensor to the Pi
13.3 V
2GPIO 4; also connect to 3.3 V through a 4.7 kΩ resistor
3Don’t connect


≡Script Writing For Data Logging with Raspberry Pi


Here the Adafruit_Python_DHT library is used to control the DHT22 sensor and read the data easily. So now you have to need Installing the DHT22 Library. For installing it, you have to need to follow This instruction. 

This library allows you to use more similar sensors such as DHT11 and AM2302 or like this. 

Open the terminal and enter the following.

pi@raspberrypi:~ $ sudo apt update
pi@raspberrypi:~ $ sudo apt install build-essential python-dev

create a folder named Desktop Environment Libraries by following the procedure below:

pi@raspberrypi:~ $ cd ~/Desktop
pi@raspberrypi:~/Desktop $ mkdir Libraries
pi@raspberrypi:~/Desktop $ cd Libraries
pi@raspberrypi:~/Desktop/Libraries $

Clone the library by entering the following command


pi@raspberrypi:~/Desktop/Libraries $ git clone

Enter Adafruit_Python_DHT Install Light via the following command

pi@raspberrypi:~/Desktop/Libraries $ cd Adafruit_Python_DHT
pi@raspberrypi:~/Desktop/Libraries/Adafruit_Python_DHT $ sudo python install

The library  installed we will start writing the script:

DHT22 library does not support Python 3, so you need to use Python 2. 2 Open Python 2.7 (IDL) and copy and paste the following code. 



#import necessary libraries

➊ import Adafruit_DHT

  import time

  #comment and uncomment the lines below depending on your sensor

  #sensor = Adafruit_DHT.DHT11

➋ sensor = Adafruit_DHT.DHT22

  #sensor = Adafruit_DHT.AM2302

  #DHT pin connects to GPIO 4

  sensor_pin = 4

  #create a variable to control the while loop

  running = True

  #new .txt file created with header

➌ file = open('sensor_readings.txt', 'w')

➍ file.write('time and date, temperature, humidity\n')

  #loop forever

  while running:


          #read the humidity and temperature

       ➎ humidity, temperature = Adafruit_DHT.read_retry(sensor,


          #uncomment the line below to convert to Fahrenheit

       ➏ #temperature = temperature * 9/5.0 + 32

          #sometimes you won't get a reading and

          #the results will be null

          #the next statement guarantees that

          #it only saves valid readings

       ➐ if humidity is not None and temperature is not None:

              #print temperature and humidity

              print('Temperature = ' + str(temperature) +

  ', Humidity = ' + str(humidity))

              #save time, date, temperature and humidity in .txt file

           ➑ file.write(time.strftime('%H:%M:%S %d/%m/%Y') + ', ' +

                  str(temperature) + ', ' + str(humidity) + '\n')


              print('Failed to get reading. Try again!')

          #wait 10s between each sensor reading

       ➒ time.sleep(10)

      #if KeyboardInterrupt triggered, stop loop and close .txt file

      except KeyboardInterrupt:

          print ('Program stopped')

          running = False

       ➓ file.close()


Script analysis Raspberry pi Data Logger: 


➊ import Adafruit_DHT
  import time
  #sensor = Adafruit_DHT.DHT11
➋ sensor = Adafruit_DHT.DHT22
  #sensor = Adafruit_DHT.AM2302
  sensor_pin = 4

Firstly, you have to install  Adafruit_DHT library. You need to import the time library. Then, the line you are using needs to change along with the sensor. If you use DHT22, no changes are required.

➌ file = open('sensor_readings.txt', 'w')

DHT22 Temperature and humidity readings automatically saved in a .txt file by saving the file to a variable with the open () function. This function accepts the name you want to give the file as an argument, and in this case, a ‘w’ tells Python that you want to put this file in writing mode, meaning that the program can write and modify the file.


➍ file.write ('time and date, temperature, humidity \ n')

Every time you run the code, the sensor_readings.txt file will overwrite what was in the file. If you do not want to do this, change the file name to create a new file each time the script executed.    

➎ humidity, temperature = Adafruit_DHT.read_retry (sensor,sensor_pin)

 #temperature = temperature * 9 / 5.0 + 32

Here ➎ Temperature and humidity read data and store data in temperature and humidity variables, respectively. 

       ➐ if humidity is not None and temperature is not None:
                 print ('Temperature =' + str (temperature) + Humidity = '+ str (humidity))          

           ➑ file.write (time.strftime ('% H:% M:% S% d /% m /% Y') + ',' +
                  str (temperature) + ',' + str (humidity) + 'n')
             print ('Failed to get reading. Try again!')

Sometimes Raspberry Pi can’t read any data from the sensor, so the ➐ if the statement used here means only Raspberry Pi data saved when the condition is true. 

The specific format of the data according to the .txt file saved ➑ 

    ➒ time.sleep (10) #if KeyboardInterrupt triggered    

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


Running script: 

To run first, you have to save or save the above program, and then you have to run Run ▸ Run current script or press F5 from the keyboard then the program will run.


 326 total views,  1 views today