Arduino Nano - Temperature Humidity Sensor - OLED

This tutorial instructs you how to read the temperature and humidity from a DHT11/DHT22 sensor and display it on an OLED.

Arduino Nano DHT11/DHT22 temperature humidity sensor OLED display

Hardware Preparation

1×Arduino Nano
1×USB A to Mini-B USB cable
1×SSD1306 I2C OLED Display 128x64
1×DHT11 Temperature and Humidity Sensor
1×Jumper Wires
1×(Optional) 9V Power Adapter for Arduino Nano
1×(Recommended) Screw Terminal Adapter for Arduino Nano

You can use DHT22 sensor instead of DHT11 sensor.

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you. We appreciate your support.

Overview of OLED display, DHT11 and DHT22 Temperature Humidity Sensor

If you are unfamiliar with OLED display, DHT11 and DHT22 temperature humidity sensor (pinout, functionality, programming ...), the following tutorials can help you:

Wiring Diagram

Arduino Nano - DHT11 Module LCD Wiring

The wiring diagram between Arduino Nano and DHT11 Sensor OLED

This image is created using Fritzing. Click to enlarge image

Arduino Nano - DHT22 Module LCD Wiring

The wiring diagram between Arduino Nano and DHT22 Sensor OLED

This image is created using Fritzing. Click to enlarge image

Arduino Nano Code - DHT11 Sensor - OLED

/* * This Arduino Nano code was developed by newbiely.com * * This Arduino Nano code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano/arduino-nano-temperature-humidity-sensor-oled */ #include <Wire.h> #include <Adafruit_GFX.h> #include <Adafruit_SSD1306.h> #include <DHT.h> #define OLED_WIDTH 128 // OLED display width, in pixels #define OLED_HEIGHT 64 // OLED display height, in pixels #define DHT_PIN 2 // The Arduino Nano pin connected to DHT11 sensor #define DHT_TYPE DHT11 Adafruit_SSD1306 oled(OLED_WIDTH, OLED_HEIGHT, &Wire, -1); // create SSD1306 display object connected to I2C DHT dht(DHT_PIN, DHT_TYPE); String displayString; void setup() { Serial.begin(9600); // initialize OLED display with address 0x3C for 128x64 if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { Serial.println(F("SSD1306 allocation failed")); while (true); } delay(2000); // wait for initializing oled.clearDisplay(); // clear display oled.setTextSize(2); // text size oled.setTextColor(WHITE); // text color oled.setCursor(0, 10); // position to display dht.begin(); // initialize DHT11 the temperature and humidity sensor displayString.reserve(10); // to avoid fragmenting memory when using String } void loop() { float humi = dht.readHumidity(); // read humidity float temperature_C = dht.readTemperature(); // read temperature // check if any reads failed if (isnan(humi) || isnan(temperature_C)) { displayString = "Failed"; } else { displayString = String(temperature_C, 1); // one decimal places displayString += "°C"; displayString += String(humi, 1); // one decimal places displayString += "%"; } Serial.println(displayString); // print the temperature in Celsius to Serial Monitor oled_display_center(displayString); // display temperature and humidity on OLED } void oled_display_center(String text) { int16_t x1; int16_t y1; uint16_t width; uint16_t height; oled.getTextBounds(text, 0, 0, &x1, &y1, &width, &height); // center the display both horizontally and vertically oled.clearDisplay(); // clear display oled.setCursor((OLED_WIDTH - width) / 2, (OLED_HEIGHT - height) / 2); oled.println(text); // text to display oled.display(); }

Detailed Instructions

  • Click to the Libraries icon on the left bar of the Arduino IDE.
  • Look for “SSD1306” and then locate the SSD1306 library from Adafruit.
  • Press the Install button to install the library.
Arduino Nano OLED library
  • You will be requested to install some other library dependencies.
  • Press the Install All button to complete the installation of all library dependencies.
Arduino Nano Adafruit GFX sensor library
  • Search for “DHT” and locate the Adafruit DHT sensor library.
  • Press the Install button to install the library.
Arduino Nano DHT sensor library
  • You will be requested to install some other library dependencies.
  • Click the Install All button to install all of the library dependencies.
Arduino Nano Adafruit Unified sensor library
  • Copy the code above and open it in the Arduino IDE.
  • Click the Upload button in the Arduino IDE to send the code to the Arduino Nano.
  • Place the sensor in hot and cold water, or hold it in your hand.
  • Check the result on the OLED and Serial Monitor.

※ NOTE THAT:

The code in question automatically centers the text both horizontally and vertically on an OLED display.

Arduino Nano Code - DHT22 Sensor - OLED

/* * This Arduino Nano code was developed by newbiely.com * * This Arduino Nano code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano/arduino-nano-temperature-humidity-sensor-oled */ #include <Wire.h> #include <Adafruit_GFX.h> #include <Adafruit_SSD1306.h> #include <DHT.h> #define OLED_WIDTH 128 // OLED display width, in pixels #define OLED_HEIGHT 64 // OLED display height, in pixels #define DHT_PIN 2 // The Arduino Nano pin connected to DHT22 sensor #define DHT_TYPE DHT22 Adafruit_SSD1306 oled(OLED_WIDTH, OLED_HEIGHT, &Wire, -1); // create SSD1306 display object connected to I2C DHT dht(DHT_PIN, DHT_TYPE); String displayString; void setup() { Serial.begin(9600); // initialize OLED display with address 0x3C for 128x64 if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { Serial.println(F("SSD1306 allocation failed")); while (true); } delay(2000); // wait for initializing oled.clearDisplay(); // clear display oled.setTextSize(2); // text size oled.setTextColor(WHITE); // text color oled.setCursor(0, 10); // position to display dht.begin(); // initialize DHT22 the temperature and humidity sensor displayString.reserve(10); // to avoid fragmenting memory when using String } void loop() { float humi = dht.readHumidity(); // read humidity float temperature_C = dht.readTemperature(); // read temperature // check if any reads failed if (isnan(humi) || isnan(temperature_C)) { displayString = "Failed"; } else { displayString = String(temperature_C, 1); // one decimal places displayString += "°C"; displayString += String(humi, 1); // one decimal places displayString += "%"; } Serial.println(displayString); // print the temperature in Celsius to Serial Monitor oled_display_center(displayString); // display temperature and humidity on OLED } void oled_display_center(String text) { int16_t x1; int16_t y1; uint16_t width; uint16_t height; oled.getTextBounds(text, 0, 0, &x1, &y1, &width, &height); // center the display both horizontally and vertically oled.clearDisplay(); // clear display oled.setCursor((OLED_WIDTH - width) / 2, (OLED_HEIGHT - height) / 2); oled.println(text); // text to display oled.display(); }

※ NOTE THAT:

The code for DHT11 and DHT22 is the same, apart from one line. The library used for both is also the same.

Video Tutorial

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!