Temperature and humidity reading from RHT03 (also known as DHT22) using ESP8266 WeMos D1 mini board.

As many other examples available on the internet, it was inevitably to post myself a temperature and humidity reading application.

The values from sensors are displayed on the mobile phone, using Blynk IoT framework.


ESP8266 WeMos D1 mini board


RHT03 temperature and humidity sensor

rht03-dht22 sensor
Sensor specifications:

  • 3.3-6V Input
  • Humidity from 0-100% RH, ±2% RH accuracy
  • -40 – 80 degrees C temperature range,±0.5 degrees C

source: http://cdn.sparkfun.com/datasheets/Sensors/Weather/RHT03.pdf

Details here: https://learn.adafruit.com/dht

Download and install DHT22 arduino library into your Arduino IDE. The library is available here: https://github.com/adafruit/DHT-sensor-library

dht library arduino

Blynk knowledge and blynk app installed on your android or iphone.


Breadboard and wires


Ok, let’s put this ingredients into one “bowl”, like this


Connect the data pin of the sensor to GPIO2 (marked as D4) which already has an internal 10k pull-up resistor.

#define BLYNK_PRINT Serial // Comment this out to disable prints and save space
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
#include <DHT.h>

#define DHTPIN D4 //pin gpio 2 in sensor
#define DHTTYPE DHT22 // DHT 22 Change this if you have a DHT11

//static const uint8_t SDA = 4;
//static const uint8_t SCL = 5;
//static const uint8_t LED_BUILTIN = 2;
//static const uint8_t BUILTIN_LED = 2;
//static const uint8_t D0 = 16;
//static const uint8_t D1 = 5;
//static const uint8_t D2 = 4;
//static const uint8_t D3 = 0;
//static const uint8_t D4 = 2;
//static const uint8_t D5 = 14;
//static const uint8_t D6 = 12;
//static const uint8_t D7 = 13;
//static const uint8_t D8 = 15;
//static const uint8_t RX = 3;
//static const uint8_t TX = 1;

// Blynk Settings //
char BlynkAuth[] = "142da76c91af47b192d396xxxxxxxxx";
char WiFiNetwork[] = "xxxxxxWlan";
char WiFiPassword[] = "xxx_xxx_xxx_xxx";
SimpleTimer timer;

float t,h;
unsigned long previousMillis = 0; // will store last temp was read
const long interval = 2000; // interval at which to read sensor

void setup() {
    Serial.begin(115200); // Serial
    // put your setup code here, to run once:
    pinMode(BUILTIN_LED, OUTPUT); // initialize onboard LED as output
    // Initialize Blynk
    Blynk.begin(BlynkAuth, WiFiNetwork, WiFiPassword);
    // Setup a function to be called 30 second
    timer.setInterval(30000L, sendUptime);

void loop() {
    // put your main code here, to run repeatedly:
    //digitalWrite(BUILTIN_LED, HIGH); // turn on LED with voltage HIGH
    //delay(1000); // wait one second
    //digitalWrite(BUILTIN_LED, LOW); // turn off LED with voltage LOW
    //delay(1000); // wait one second

void sendUptime()
    // You can send any value at any time.
    // Please don't send more that 10 values per second.
    //Read the Temp and Humidity from DHT

    Blynk.virtualWrite(10, t); // virtual pin
    Blynk.virtualWrite(11, h); // virtual pin

void gettemperature() {
    // Wait at least 2 seconds seconds between measurements.
    // if the difference between the current time and last time you read
    // the sensor is bigger than the interval you set, read the sensor
    // Works better than delay for things happening elsewhere also
    unsigned long currentMillis = millis();

    if(currentMillis - previousMillis >= interval) {
        // save the last time you read the sensor
        previousMillis = currentMillis;

        // Reading temperature for humidity takes about 250 milliseconds!
        // Sensor readings may also be up to 2 seconds 'old' (it's a very slow sensor)
        h = dht.readHumidity(); // Read humidity (percent)
        t = dht.readTemperature(); // Read temperature as C
        // Check if any reads failed and exit early (to try again).
        if (isnan(h) || isnan(t)) {
            Serial.println("Failed to read from DHT sensor!");


Change your BlynkAuthWiFiNetworkWiFiPassword to your settings.

This program generate a serial output like this:

esp-utput serial


The blynk app has a gauge, a value display and a history graph item


updated 2020.07.23

BOM used in this article:


ESP8266: DHT22 temperature sensor
Tagged on:                         

6 thoughts on “ESP8266: DHT22 temperature sensor

  • February 3, 2017 at 16:09

    This is EXACTLY what I needed!
    Many thanks for documenting this so good.

    • February 3, 2017 at 16:12

      Still working right now in my home.
      Glad it helps!

      I’ve added a loop of 3 times readings from the sensor (with a pause of 3 seconds) if I get a fail reading from the sensor.

  • February 17, 2017 at 06:42

    Thank you. This is one of the best well expleined instructions. Is this code suitable for ESP8266 only (without WeMos D1 mini board) if we program the ESP8266 module via CP2102 USB UART Board (mini)? I want to miniaturize the setup that is why I am asking:)

    • February 17, 2017 at 10:03

      Thanks for reading.
      This instructions and my knowledges are gathered from many sources. You cand find many instructables with this setup.

      To answer on you question, yes, you can use this code on any ESP8266 module. The only mention is that to check if the board/module you have has an internal pull-up resistor on D4 pin (for this example).
      If you are using any other digital pin, the reading of the sensor will also be ok.
      Here is an example with nodemcu board
      Join us on facebook group “ESP8266” to get specific answers.

  • July 22, 2020 at 22:06

    Just a little tip. Do not use the delay() function, IT will delay your commands to Blynk

    • July 23, 2020 at 13:10

      Hello, thank you for the suggestion! That delay should not be inside the loop.

      I’ve changed the code above, thank you.


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.