Temperature Data Logger using Raspberry Pi Pico and DHT22 Temperature Sensor

Published  February 28, 2022   0
Temperature Monitoring using Raspberry Pi Pico and DHT22

With temperature monitoring, you can easily monitor, track and generate graphs to understand the daily temperature trends. Data monitoring and generating trends is one of the major applications in the field of the Internet of Things. Internet of Things is the capability to connect any device over the internet. Each device can be programmed for one or more applications. Temperature Monitoring is one of the well known applications widely used by many industries to monitor server room temperature, environment temperature trends, and so on. I use the internal storage of Pico to store the data with the time stamp in a new line format. This saves the cost of an external SD card module and memory card. Pico gives freedom to select the file type and allows users to store the data in an easy friendly format.

Temperature Monitoring Data Logger

You can check out our previously built Temperature Monitoring System,

Component Requirement for Temperature Monitoring Data Logger

Project Used Hardware

  • Raspberry Pi Pico,
  • DHT22,
  • RTC - DS3231 module

Project Used Software

  • Thonny IDE - Python

Project Hardware Software Selection

Raspberry Pi Pico - Raspberry Pi Pico is a tiny, fast, and versatile board developed by Raspberry Pi. It is built using RP2040 a new microcontroller that supports Python environment Pico uses Arm cortex (processor) which is designed for small and affordable applications Pico is developed with a wide range of programmable options I/O, UART, I2C, SPI, etc

DHT22 - Temperature and Humidity sensor DHT22 is low cost digital temperature and humidity sensor. It measures temperature and humidity of surrounding air with a refresh rate of 2 seconds DHT22 works on 3 to 5V and uses 2.5mA of maximum current Temperature Range: -40 to +80 degree Celcius with an accuracy of 0.5-degree Humidity Range: 0-100% with an accuracy of 2-5%

DS3231 RTC Module - I2C based Time and Clock module: DS3231, It is used to measure time and date. It is powered independently and worked on the I2C protocol.

Thonny IDE -  Easy to use and light weight software Support Python 3.7 and very easy to install Simply debugger, explains scopes, highlight syntax errors, and texts

Circuit Diagram


Temperature logger using Raspberry Pi and DHT22 Circuit Diagram

I am using DHT22 and RTC - DS3231 module interfaced with Raspberry Pi Pico DHT22 connections with Pico - Power pins are connected with vcc and ground respectively, Data of DHT22 is connected with Pin 15 on Pico RTC - DS3231 connections with Pico - Power pins are connected with vcc and ground respectively, SDA and SCL pins are connected on SCL and SDA on Pico.

# More than 30,000 datapoints can be stored.
# Practical example: Saving data at every minute will give
# Approx Backup time = 30000/1/60/24 = approx 20 days of backup
from machine import Pin, I2C
from DHT22 import DHT22
from urtc import DS3231
import time
# Initializing I2C
i2crtc = I2C(0, scl=Pin(21), sda=Pin(20), freq=100000)
exRTC = DS3231(i2crtc)
# Getting Date and Time from RTC
pDT = exRTC.datetime()
print("Present Date & Time :", pDT)
# Iitializing DHT22 Pin
dht22 = DHT22(Pin(15, Pin.IN, Pin.PULL_UP))
while True:
    # Reading Temperature and Humidty from DHT22
    T, H = dht22.read()
    # Getting Date and Time from RTC
    pDT = exRTC.datetime()
    print(pDT[0], pDT[1], pDT[2], pDT[3], pDT[4], pDT[5], pDT[6], T, H)
    # Backing up data into internal storage in the below syntax
    # year, month, day, weekday, hour, minute, second, temperature, humidity
    with open('Databackup.txt', 'a+') as file:
        file.write(str(pDT[0]) + ", " + str(pDT[1]) + ", " + str(pDT[2]) + ", " + str(pDT[3]) + ", " +
                   str(pDT[4]) + ", " + str(pDT[5]) + ", " + str(pDT[6]) + ", " + str(T) + ", " + str(H))
    # Since DHT22 updates temperature and humidity every 2 seconds