Arduino

DHT11 Temperature and Humidity Sensor with OLED Using Arduino Nano

Arduino Nano is a small, powerful microcontroller board that is capable of interacting with a wide range of sensors and devices. The DHT11 sensor is a commonly used sensor for measuring temperature and humidity, and it can be easily connected to an Arduino Nano board using jumper wires and a breadboard.

In this project, we will be using the Arduino Nano and DHT11 sensor to create a temperature and humidity monitoring system. The Arduino Nano will read temperature and humidity values from DHT11 and show on OLED.

This tutorial covers following content:

1: Introduction to DHT11 Sensor

2: DHT11 Sensor Pinout

2.1: 3 Pin DHT11 Sensor

2.2: 4 Pin DHT11 Sensor

3: OLED Display Module with Arduino Nano

4: Installing the Required Libraries

4.1: Arduino Library for DHT Sensor

4.2: Arduino Library for OLED Display

5: Check OLED Display I2C Address in Arduino Nano

6: Interfacing Arduino Nano with DHT11 Sensor and OLED

6.1: Schematic

6.2: Code

6.3: Output

1: Introduction to DHT11 Sensor

The DHT11 sensor is a compact and low-cost device for measuring temperature and humidity. Arduino Nano with DHT11 is used for designing portable weather stations, HVAC systems, and home automation systems.

The DHT11 sensor consists of a humidity sensing element and a temperature sensing element, which are combined on a single integrated circuit. The sensor is capable of measuring both relative humidity and temperature, and it can transmit this data via a digital signal to a microcontroller or other device.

The DHT11 sensor can be integrated and controlled using the Arduino code. It can be connected to a microcontroller or single-board computer using jumper wires and a breadboard, and it can be easily integrated into a variety of projects.

Some major specification of DHT11:

  • Operating voltage starts from 3.5V to 5.5V
  • Sensor current while measuring values is 0.3mA and standby current is 60uA
  • Output values as digital signal
  • Temperature starts from 0°C to 50°C
  • Humidity measured from 20% to 90%
  • Temperature and Humidity both are 16-bit
  • Accuracy of ±1°C for temperature measuring and ±1% for relative humidity readings

Now we covered the basics of the DHT11 sensor. Now we will discuss the DHT11 pinout.

2: DHT11 Sensor Pinout

DHT11 has two variants, one with 4 pins and other one with 3 pins. Only difference here is that the 4 pin DHT11 sensor has an extra pin with no connection. This pin is labeled as NC and not used for any purpose.

The 3 pins of DHT11 are:

  • Power voltage pin
  • GND pin
  • Digital data signal pin

2.1: 3 Pin DHT11 Sensor

Following pinout is of 3 pins DHT11:

Graphical user interface Description automatically generated with medium confidence

1 Data Output temperature readings and humidity values
2 Vcc Input voltage between 3.5V to 5.5V
3 GND GND

2.2: 4 Pin DHT11 Sensor

Below is the 4 pin DHT11 sensor pinout:

Diagram Description automatically generated

These 4 pins of DHT11 sensor includes:

1 Vcc Input 3.5V to 5.5V
2 Data Output temperature and humidity readings
3 NC No connection pin
4 GND GND

3: OLED Display Module with Arduino Nano

The OLED display mainly comes with two different communication protocols. These two are I2C and SPI. The SPI protocol is faster as compared to I2C, but I2C is preferred and has the advantage over SPI due to less pins required.

Following image illustrates an Arduino Nano connection diagram with 128×64 pixels (0.96’’) OLED display.

Below table show pinout configuration of OLED with Nano:

As we have interfaced Arduino Nano with an OLED display. To display data on an OLED screen we must install some necessary libraries first.

4: Installing the Required Libraries

We are interfacing two sensors; one is an OLED display and the other one is a DHT11 sensor. Both sensors required separate libraries for functioning. Now we will install separate libraries for DHT11 and OLED screens.

4.1: Arduino Library for DHT Sensor

Open IDE, go to: Sketch>Include Library>Manage Libraries:

Graphical user interface, text, application Description automatically generated

One can also use the Arduino library manager for installing libraries. Search the DHT11 sensor library and install the updated version. This library will read data from the DHT11 sensor.

Now we will install the unified sensor library.

Graphical user interface, text, application Description automatically generated

DHT11 sensor libraries are installed. Next, the OLED libraries need to be installed.

4.2: Arduino Library for OLED Display

There are a number of libraries available for OLED display in IDE. We will use Adafruit GFX and SSD1306 library for OLED display.

Open the IDE and search the SSD1306 library in the library manager:

Graphical user interface, text, application Description automatically generated

After installing the SSD1306 library, install the GFX library by Adafruit:

Graphical user interface, text, application Description automatically generated

We have installed libraries for both sensors and now we can upload code in Arduino Nano. But before that it’s necessary to check the OLED I2C address.

5: Check OLED Display I2C Address in Arduino Nano

I2C allows multiple devices to be connected and communicate with each other over a two-wire interface. Each I2C device must have a unique address, ranging from 0 to 127, to ensure that it can be identified and communicated with on the I2C line. Multiple devices with the same address cannot be connected on the same I2C bus.

Connect the OLED display with Arduino Nano and after selecting the board and port in Arduino IDE upload the code given in article Scan I2C devices in Arduino. After uploading code, we will get the I2C address of the OLED display which in our case is 0X3C:

Graphical user interface, text, application, email Description automatically generated

We will define this I2C address inside the Arduino code.

6: Interfacing Arduino Nano with DHT11 Sensor and OLED

For interfacing Arduino Nano with DHT11 a digital pin of Nano board will be used for data reading. To power the DHT11 5V Nano board pin will be interfaced.

For OLED screen I2C pins SDA and SCL at A4 and A5 pins of Arduino Nano will be used. For powering an OLED 5V pin of Arduino Nano will be used.

Chart Description automatically generated with medium confidence

6.1: Schematic

Below is the schematic diagram of Arduino Nano with DHT11 sensor and to display read values an OLED screen is used. This schematic image is of 3 pin DHT11 sensor. 10kΩ pull up resistor is integrated at DHT11 output.

Similarly, a 4 pin DHT11 sensor is connected with a Nano board. The OLED display is connected to A4 and A5 GPIO pins of Nano using the I2C communication. DHT11 pin 2 is data output. The 4 pin DHT11 has 1 pin extra which is of no use.

Diagram Description automatically generated

6.2: Code

Connect Arduino Nano and upload the given code:

#include <Wire.h>             /*Wire Communication library*/

#include <Adafruit_GFX.h>

#include <Adafruit_SSD1306.h> /*OLED Adafruit library*/

#include <Adafruit_Sensor.h>

#include <DHT.h>             /*DHT sensor library*/

#define SCREEN_WIDTH 128    /*128 width OLED in pixels*/

#define SCREEN_HEIGHT 64   /*64 height OLED in pixel*/

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1); /*I2C Display initialization*/

#define DHTPIN 4          /*DHT11 signal pin*/

#define DHTTYPE    DHT11

//#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321

//#define DHTTYPE DHT21 // DHT 21 (AM2301)

DHT dht(DHTPIN, DHTTYPE);
void setup() {
  Serial.begin(9600);
  dht.begin();
  if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {   /*OLED I2C Address*/
    Serial.println(F("SSD1306 allocation failed"));
    for(;;);
  }
  delay(2000);
  display.clearDisplay();
  display.setTextColor(WHITE);  /*Text color*/
}
void loop() {
  delay(5000);
  float t = dht.readTemperature();  /*read temp*/
  float h = dht.readHumidity();    /*read humidity*/
  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
  }
  display.clearDisplay();  /*clear display*/
  display.setTextSize(1); /*OLED font size*/
  display.setCursor(0,0);
  display.print("Temperature: ");
  display.setTextSize(2);
  display.setCursor(0,10);
  display.print(t);       /*print temp in Celsius*/
  display.print(" ");
  display.setTextSize(1);
  display.cp437(true);
  display.write(167);
  display.setTextSize(2);
  display.print("C");  
  display.setTextSize(1);
  display.setCursor(0, 35);
  display.print("Humidity: ");
  display.setTextSize(2);
  display.setCursor(0, 45);
  display.print(h);      /*prints humidity percentage*/
  display.print(" %");
  display.display();
}

At the start of code, we included the OLED and DHT sensor libraries. Next OLED screen size is defined in pixels. After that DHT sensor type is initialized. If you are using any other type of DHT11, uncomment the sensor name accordingly inside the code.

Next in code we initialized DHT and OLED sensor. The OLED is connected at 0x3C I2C address. I2C address can be checked using the code in this article.

The two float variables t and h will store the temperature and humidity values respectively. Lasty in code all the values are displayed on an OLED screen using the OLED GFX library functions.

6.3: Output

Output shows real time temperature and humidity values displayed on the OLED screen:

We have completed interfacing of the OLED and DHT11 sensor with the Arduino Nano board.

Conclusion

Arduino Nano can be integrated with multiple sensors. This article covers OLED and DHT11 sensor interfacing with Arduino Nano. Using the DHT11 we measured temperature and humidity which are displayed on OLED. Using the given code any of the Arduino Nano can be programmed to display sensor readings on an OLED screen.

About the author

Kashif

I am an Electrical Engineer. I love to write about electronics. I am passionate about writing and sharing new ideas related to emerging technologies in the field of electronics.