{"id":85288,"date":"2019-05-23T09:56:45","date_gmt":"2019-05-23T09:56:45","guid":{"rendered":"https:\/\/randomnerdtutorials.com\/?p=85288"},"modified":"2020-07-30T14:12:47","modified_gmt":"2020-07-30T14:12:47","slug":"esp32-esp8266-dht-temperature-and-humidity-oled-display","status":"publish","type":"post","link":"https:\/\/randomnerdtutorials.com\/esp32-esp8266-dht-temperature-and-humidity-oled-display\/","title":{"rendered":"ESP32\/ESP8266: DHT Temperature and Humidity Readings in OLED Display"},"content":{"rendered":"\n<p>Learn how to display temperature and humidity readings from a DHT11\/DHT22 sensor in an SSD1306 OLED display using an ESP32 or an ESP8266 with Arduino IDE.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img data-recalc-dims=\"1\" fetchpriority=\"high\" decoding=\"async\" width=\"1200\" height=\"675\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/05\/ESP32-ESP8266-OLED-DHT22-DHT11-Temperature-Humidity.jpg?resize=1200%2C675&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 ESP8266 Display DHT11 DHT22 Temperature and Humidity Readings in OLED Display\" class=\"wp-image-85532\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/05\/ESP32-ESP8266-OLED-DHT22-DHT11-Temperature-Humidity.jpg?w=1280&amp;quality=100&amp;strip=all&amp;ssl=1 1280w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/05\/ESP32-ESP8266-OLED-DHT22-DHT11-Temperature-Humidity.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/05\/ESP32-ESP8266-OLED-DHT22-DHT11-Temperature-Humidity.jpg?resize=768%2C432&amp;quality=100&amp;strip=all&amp;ssl=1 768w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/05\/ESP32-ESP8266-OLED-DHT22-DHT11-Temperature-Humidity.jpg?resize=1024%2C576&amp;quality=100&amp;strip=all&amp;ssl=1 1024w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" \/><\/figure><\/div>\n\n\n\n<p>The idea of using the OLED display with the ESP32 or ESP8266 is to ilustrate how you can create a physical user interface for your boards.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Project Overview<\/h2>\n\n\n\n<p>In this project we&#8217;ll use an <a href=\"https:\/\/makeradvisor.com\/tools\/oled-display-128x64-0-96-inch\/\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\">I2C SSD1306 128&#215;64 OLED display<\/a> as shown in the following figure.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"750\" height=\"412\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/09\/ssd1306-oled-display-arduino.jpg?resize=750%2C412&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"I2C SSD1306 128x64 0.96 inch OLED display for ESP32 ESP8266\" class=\"wp-image-80990\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/09\/ssd1306-oled-display-arduino.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/09\/ssd1306-oled-display-arduino.jpg?resize=300%2C165&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n\n<p>The temperature and humidity will be measured using the <a rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\" href=\"https:\/\/makeradvisor.com\/tools\/dht22-temperature-humidity-sensor\/\" target=\"_blank\">DHT22 temperature and humidity sensor<\/a> (you can also use <a href=\"https:\/\/makeradvisor.com\/tools\/dht11-temperature-humidity-sensor\/\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\">DHT11<\/a>).<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"750\" height=\"442\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/04\/DHT11-DHT22-Temperature-Humidity-Sensor.jpg?resize=750%2C442&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"DHT11 DHT22 Temperature and Humidity Sensor for ESP32 ESP8266\" class=\"wp-image-84441\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/04\/DHT11-DHT22-Temperature-Humidity-Sensor.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/04\/DHT11-DHT22-Temperature-Humidity-Sensor.jpg?resize=300%2C177&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n\n<p>If you\u2019re not familiar with the DHT11\/DHT22 sensor, we recommend reading the following guide:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/randomnerdtutorials.com\/esp32-dht11-dht22-temperature-humidity-sensor-arduino-ide\/\">ESP32 with DHT11\/DHT22 Sensor (Arduino IDE)<\/a><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Parts required<\/h2>\n\n\n\n<p>For this tutorial you need the following components:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\" href=\"https:\/\/makeradvisor.com\/tools\/oled-display-128x64-0-96-inch\/\" target=\"_blank\">0.96 inch OLED display&nbsp;<\/a><\/li><li><a rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\" href=\"https:\/\/makeradvisor.com\/tools\/esp32-dev-board-wi-fi-bluetooth\/\" target=\"_blank\">ESP32<\/a> or <a rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\" href=\"https:\/\/makeradvisor.com\/tools\/esp8266-esp-12e-nodemcu-wi-fi-development-board\/\" target=\"_blank\">ESP8266<\/a> (read <a rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\" href=\"https:\/\/makeradvisor.com\/esp32-vs-esp8266\/\" target=\"_blank\">ESP32 vs ESP8266<\/a>)<\/li><li><a rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\" href=\"https:\/\/makeradvisor.com\/tools\/dht22-temperature-humidity-sensor\/\" target=\"_blank\">DHT22<\/a> or <a rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\" href=\"https:\/\/makeradvisor.com\/tools\/dht11-temperature-humidity-sensor\/\" target=\"_blank\">DHT11<\/a> temperature and humidity sensor<a rel=\"noreferrer noopener\" href=\"https:\/\/makeradvisor.com\/tools\/dht11-temperature-humidity-sensor\/\" target=\"_blank\">&nbsp;<\/a><\/li><li><a rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\" href=\"https:\/\/makeradvisor.com\/tools\/mb-102-solderless-breadboard-830-points\/\" target=\"_blank\">Breadboard<\/a><\/li><li><a rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\" href=\"https:\/\/makeradvisor.com\/tools\/resistors-kits\/\" target=\"_blank\">10k Ohm resistor<\/a><\/li><li><a href=\"https:\/\/makeradvisor.com\/tools\/jumper-wires-kit-120-pieces\/\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\">Jumper wires<\/a><\/li><\/ul>\n\n\n<p>You can use the preceding links or go directly to <a href=\"https:\/\/makeradvisor.com\/tools\/?utm_source=rnt&utm_medium=post&utm_campaign=post\" target=\"_blank\">MakerAdvisor.com\/tools<\/a> to find all the parts for your projects at the best price!<\/p><p style=\"text-align:center;\"><a href=\"https:\/\/makeradvisor.com\/tools\/?utm_source=rnt&utm_medium=post&utm_campaign=post\" target=\"_blank\"><img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/10\/header-200.png?w=1200&#038;quality=100&#038;strip=all&#038;ssl=1\"><\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Schematic<\/h2>\n\n\n\n<p>The OLED display we&#8217;re using communicates via I2C communication protocol, so you need to connect it to the ESP32 or ESP8266 I2C pins.<\/p>\n\n\n\n<p>By default, the ESP32 I2C pins are:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>GPIO 22:<\/strong> SCL<\/li><li><strong>GPIO 21:<\/strong> SDA<\/li><\/ul>\n\n\n\n<p>If you&#8217;re using an ESP8266, the default I2C pins are:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>GPIO 5 <\/strong>(D1): SCL<\/li><li><strong>GPIO 4 <\/strong>(D2): SDA<\/li><\/ul>\n\n\n\n<p>Follow the next schematic diagram if you&#8217;re using an ESP32 board:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><a href=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/05\/oled-temperature-humidity-esp32_bb.png?quality=100&#038;strip=all&#038;ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"947\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/05\/oled-temperature-humidity-esp32_bb.png?resize=1024%2C947&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 OLED DHT11 DHT22 Sensor Circuit Diagram Schematics\" class=\"wp-image-85541\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/05\/oled-temperature-humidity-esp32_bb.png?resize=1024%2C947&amp;quality=100&amp;strip=all&amp;ssl=1 1024w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/05\/oled-temperature-humidity-esp32_bb.png?resize=300%2C277&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/05\/oled-temperature-humidity-esp32_bb.png?resize=768%2C710&amp;quality=100&amp;strip=all&amp;ssl=1 768w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/05\/oled-temperature-humidity-esp32_bb.png?w=1064&amp;quality=100&amp;strip=all&amp;ssl=1 1064w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure><\/div>\n\n\n\n<p class=\"rntbox rntclblue\"><strong>Recommended reading:<\/strong> <a href=\"https:\/\/randomnerdtutorials.com\/esp32-pinout-reference-gpios\/\">ESP32 Pinout Reference Guide<\/a><\/p>\n\n\n\n<p>If you&#8217;re using an ESP8266 follow the next diagram instead.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><a href=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/05\/oled-temperature-humidity-esp8266.png?quality=100&#038;strip=all&#038;ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"880\" height=\"892\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/05\/oled-temperature-humidity-esp8266.png?resize=880%2C892&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP8266 OLED DHT11 DHT22 Sensor Circuit Diagram Schematics\" class=\"wp-image-85463\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/05\/oled-temperature-humidity-esp8266.png?w=880&amp;quality=100&amp;strip=all&amp;ssl=1 880w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/05\/oled-temperature-humidity-esp8266.png?resize=296%2C300&amp;quality=100&amp;strip=all&amp;ssl=1 296w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/05\/oled-temperature-humidity-esp8266.png?resize=768%2C778&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 880px) 100vw, 880px\" \/><\/a><\/figure><\/div>\n\n\n\n<p>In this case we&#8217;re connecting the DHT data pin to <span class=\"rnthl rntcyellow\">GPIO 14<\/span>, but you can use any other suitable GPIO.<\/p>\n\n\n\n<p class=\"rntbox rntclblue\"><strong>Recommended reading:<\/strong> <a href=\"https:\/\/randomnerdtutorials.com\/esp8266-pinout-reference-gpios\/\">ESP8266 Pinout Reference Guide<\/a> <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Installing Libraries<\/h2>\n\n\n\n<p>Before uploading the code, you need to install the libraries to write to the OLED display and the libraries to read from the DHT sensor.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Installing the OLED libraries<\/h3>\n\n\n\n<p>There are several libraries available to control the OLED display with the ESP8266. In this tutorial we&#8217;ll use the libraries from adafruit: the <a rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\" href=\"https:\/\/github.com\/adafruit\/Adafruit_SSD1306\" target=\"_blank\">Adafruit_SSD1306 library<\/a> and the <a href=\"https:\/\/github.com\/adafruit\/Adafruit-GFX-Library\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\">Adafruit_GFX library<\/a>. Follow the next steps to install these libraries:<\/p>\n\n\n\n<p><strong>1.<\/strong> Open your Arduino IDE and go to&nbsp;<strong>Sketch&nbsp;<\/strong>&gt;&nbsp;<strong>Include Library<\/strong>&nbsp;&gt;&nbsp;<strong>Manage Libraries<\/strong>. The Library Manager should open.<\/p>\n\n\n\n<p><strong>2.<\/strong> Type &#8220;<strong>SSD1306<\/strong>&#8221; in the Search box and install the SSD1306 library from Adafruit.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"788\" height=\"443\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/05\/install-ssd1306-adafruit-library.png?resize=788%2C443&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Installing SSD1306 OLED Adafruit library in Arduino IDE\" class=\"wp-image-85156\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/05\/install-ssd1306-adafruit-library.png?w=788&amp;quality=100&amp;strip=all&amp;ssl=1 788w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/05\/install-ssd1306-adafruit-library.png?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/05\/install-ssd1306-adafruit-library.png?resize=768%2C432&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 788px) 100vw, 788px\" \/><\/figure><\/div>\n\n\n\n<p><strong>3.<\/strong> After installing the SSD1306 library from Adafruit, type &#8220;<strong>GFX<\/strong>&#8221; in the search box and install the library.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"786\" height=\"443\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/05\/install-gfx-library-adafruit.png?resize=786%2C443&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Installing GFX Adafruit library in Arduino IDE\" class=\"wp-image-85157\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/05\/install-gfx-library-adafruit.png?w=786&amp;quality=100&amp;strip=all&amp;ssl=1 786w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/05\/install-gfx-library-adafruit.png?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/05\/install-gfx-library-adafruit.png?resize=768%2C433&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 786px) 100vw, 786px\" \/><\/figure><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">Installing the DHT Sensor libraries<\/h3>\n\n\n\n<p>To read from the DHT sensor we&#8217;ll use the libraries from Adafruit.<\/p>\n\n\n\n<p><strong>1.<\/strong> Open your Arduino IDE and go to <strong>Sketch&nbsp;<\/strong>&gt;&nbsp;<strong>Include Library<\/strong>&nbsp;&gt;&nbsp;<strong>Manage Libraries<\/strong>. The Library Manager should open.<\/p>\n\n\n\n<p><strong>2.<\/strong> Search for &#8220;<strong>DHT<\/strong>&#8221; on the Search box and install the DHT library from Adafruit.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"422\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/04\/adafruit_dht_library.png?resize=750%2C422&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Installing Adafruit DHT library in Arduino IDE\" class=\"wp-image-84293\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/04\/adafruit_dht_library.png?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/04\/adafruit_dht_library.png?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n\n<p><strong>3.<\/strong> After installing the DHT library from Adafruit, type &#8220;<strong>Adafruit Unified Sensor<\/strong>&#8221; in the search box. Scroll all the way down to find the library and install it.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"422\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/04\/adafruit_unified_sensor_library.png?resize=750%2C422&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Installing Adafruit Unified Sensor driver library in Arduino IDE\" class=\"wp-image-84295\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/04\/adafruit_unified_sensor_library.png?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/04\/adafruit_unified_sensor_library.png?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">Installing the ESP boards<\/h3>\n\n\n\n<p>We\u2019ll program the ESP32\/ESP8266 using Arduino IDE, so you must have the ESP32\/ESP8266 add-on installed in your Arduino IDE. If you haven\u2019t, follow the next tutorial first that fits your needs:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/randomnerdtutorials.com\/installing-the-esp32-board-in-arduino-ide-windows-instructions\/\">Install the ESP32 Board in Arduino IDE (Windows instructions)<\/a><\/li><li><a href=\"https:\/\/randomnerdtutorials.com\/installing-the-esp32-board-in-arduino-ide-mac-and-linux-instructions\/\">Install the ESP32 Board in Arduino IDE (Mac OS X and Linux instructions)<\/a><\/li><li><a href=\"https:\/\/randomnerdtutorials.com\/how-to-install-esp8266-board-arduino-ide\/\">Install the ESP8266 Board in Arduino IDE<\/a><\/li><\/ul>\n\n\n\n<p>Finally, restart your Arduino IDE.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Code<\/h2>\n\n\n\n<p>After installing the necessary libraries, you can copy the following code to your Arduino IDE and upload it to your ESP32 or ESP8266 board.<\/p>\n\n\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-c\">\/*********\n  Rui Santos\n  Complete project details at https:\/\/randomnerdtutorials.com  \n*********\/\n\n#include &lt;Wire.h&gt;\n#include &lt;Adafruit_GFX.h&gt;\n#include &lt;Adafruit_SSD1306.h&gt;\n#include &lt;Adafruit_Sensor.h&gt;\n#include &lt;DHT.h&gt;\n\n#define SCREEN_WIDTH 128 \/\/ OLED display width, in pixels\n#define SCREEN_HEIGHT 64 \/\/ OLED display height, in pixels\n\n\/\/ Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)\nAdafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &amp;Wire, -1);\n\n#define DHTPIN 14     \/\/ Digital pin connected to the DHT sensor\n\n\/\/ Uncomment the type of sensor in use:\n\/\/#define DHTTYPE    DHT11     \/\/ DHT 11\n#define DHTTYPE    DHT22     \/\/ DHT 22 (AM2302)\n\/\/#define DHTTYPE    DHT21     \/\/ DHT 21 (AM2301)\n\nDHT dht(DHTPIN, DHTTYPE);\n\nvoid setup() {\n  Serial.begin(115200);\n\n  dht.begin();\n\n  if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {\n    Serial.println(F(&quot;SSD1306 allocation failed&quot;));\n    for(;;);\n  }\n  delay(2000);\n  display.clearDisplay();\n  display.setTextColor(WHITE);\n}\n\nvoid loop() {\n  delay(5000);\n\n  \/\/read temperature and humidity\n  float t = dht.readTemperature();\n  float h = dht.readHumidity();\n  if (isnan(h) || isnan(t)) {\n    Serial.println(&quot;Failed to read from DHT sensor!&quot;);\n  }\n  \/\/ clear display\n  display.clearDisplay();\n  \n  \/\/ display temperature\n  display.setTextSize(1);\n  display.setCursor(0,0);\n  display.print(&quot;Temperature: &quot;);\n  display.setTextSize(2);\n  display.setCursor(0,10);\n  display.print(t);\n  display.print(&quot; &quot;);\n  display.setTextSize(1);\n  display.cp437(true);\n  display.write(167);\n  display.setTextSize(2);\n  display.print(&quot;C&quot;);\n  \n  \/\/ display humidity\n  display.setTextSize(1);\n  display.setCursor(0, 35);\n  display.print(&quot;Humidity: &quot;);\n  display.setTextSize(2);\n  display.setCursor(0, 45);\n  display.print(h);\n  display.print(&quot; %&quot;); \n  \n  display.display(); \n}\n<\/code><\/pre>\n\t<p style=\"text-align:center\"><a class=\"rntwhite\" href=\"https:\/\/github.com\/RuiSantosdotme\/Random-Nerd-Tutorials\/raw\/master\/Projects\/OLED\/oled_dht_temperature_humidity.ino\" target=\"_blank\">View raw code<\/a><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">How the code works<\/h3>\n\n\n\n<p>Let&#8217;s take a quick look on how the code works.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Importing libraries<\/h4>\n\n\n\n<p>The code starts by including the necessary libraries. The <span class=\"rnthl rntliteral\">Wire<\/span>, <span class=\"rnthl rntliteral\">Adafruit_GFX<\/span> and <span class=\"rnthl rntliteral\">Adafruit_SSD1306<\/span> are used to interface with the OLED display. The <span class=\"rnthl rntliteral\">Adafruit_Sensor<\/span> and the <span class=\"rnthl rntliteral\">DHT<\/span> libraries are used to interface with the DHT22 or DHT11 sensors.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>#include &lt;Wire.h>\n#include &lt;Adafruit_GFX.h>\n#include &lt;Adafruit_SSD1306.h>\n#include &lt;Adafruit_Sensor.h>\n#include &lt;DHT.h><\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Create a display object<\/h4>\n\n\n\n<p>Then, define your OLED display dimensions. In this case, we&#8217;re using a 128&#215;64 pixel display.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>#define SCREEN_WIDTH 128 \/\/ OLED display width, in pixels\n#define SCREEN_HEIGHT 64 \/\/ OLED display height, in pixels<\/code><\/pre>\n\n\n\n<p>Then, initialize a display object with the width and height defined earlier with I2C communication protocol (<span class=\"rnthl rntliteral\">&amp;Wire<\/span>). <\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &amp;Wire, -1);<\/code><\/pre>\n\n\n\n<p>The (-1) parameter means that your OLED display doesn&#8217;t have a RESET pin. If your OLED display does have a RESET pin, it should be connected to a GPIO. In that case, you should pass the GPIO number as a parameter.  <\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Create a DHT object<\/h4>\n\n\n\n<p>Then, define the DHT sensor type you&#8217;re using. If you&#8217;re using a DHT22 you don&#8217;t need to change anything on the code. If you&#8217;re using another sensor, just uncomment the sensor you&#8217;re using and comment the others.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>\/\/#define DHTTYPE    DHT11     \/\/ DHT 11\n#define DHTTYPE    DHT22     \/\/ DHT 22 (AM2302)\n\/\/#define DHTTYPE    DHT21     \/\/ DHT 21 (AM2301)<\/code><\/pre>\n\n\n\n<p>Initialize a <span class=\"rnthl rntliteral\">DHT<\/span> sensor object with the pin and type defined earlier.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>DHT dht(DHTPIN, DHTTYPE);<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">setup()<\/h4>\n\n\n\n<p>In the <span class=\"rnthl rntliteral\">setup()<\/span>, initialize the serial monitor for debugging purposes.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>Serial.begin(115200);<\/code><\/pre>\n\n\n\n<p>Initialize the DHT sensor:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>dht.begin();<\/code><\/pre>\n\n\n\n<p>Then, initialize the OLED display.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { \/\/ Address 0x3D for 128x64\n  Serial.println(F(\"SSD1306 allocation failed\"));\n  for(;;);\n}<\/code><\/pre>\n\n\n\n<p>In this case, the address of the OLED display we&#8217;re using is <strong>0x3C<\/strong>. If this address doesn&#8217;t work, you can run an I2C scanner sketch to find your OLED address. You can <a href=\"https:\/\/raw.githubusercontent.com\/RuiSantosdotme\/Random-Nerd-Tutorials\/master\/Projects\/LCD_I2C\/I2C_Scanner.ino\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\">find the I2C scanner sketch here<\/a>. <\/p>\n\n\n\n<p>Add a delay to give time for the display to initialize, clear the display and set the text color to white:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>delay(2000);\ndisplay.clearDisplay();\ndisplay.setTextColor(WHITE)<\/code><\/pre>\n\n\n\n<p>In the <span class=\"rnthl rntliteral\">loop()<\/span> is where we read the sensor and display the temperature and humidity on the display.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Get temperature and humidity readings from DHT<\/h4>\n\n\n\n<p>The temperature and humidity are saved on the <span class=\"rnthl rntliteral\">t<\/span> and <span class=\"rnthl rntliteral\">h<\/span> variables, respectively. Reading temperature and humidity is as simple as using the <span class=\"rnthl rntliteral\">readTemperature()<\/span> and <span class=\"rnthl rntliteral\">readHumidity()<\/span> methods on the <span class=\"rnthl rntliteral\">dht<\/span> object. <\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>float t = dht.readTemperature();\nfloat h = dht.readHumidity();<\/code><\/pre>\n\n\n\n<p>In case we are not able to get the readings, display an error message:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>if (isnan(h) || isnan(t)) {\n  Serial.println(\"Failed to read from DHT sensor!\");\n}<\/code><\/pre>\n\n\n\n<p>If you get that error message, read our troubleshooting guide: <a href=\"https:\/\/randomnerdtutorials.com\/solved-dht11-dht22-failed-to-read-from-dht-sensor\/\">how to fix &#8220;Failed to read from DHT sensor&#8221;<\/a>.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Display sensor readings on the OLED display<\/h4>\n\n\n\n<p>The following lines display the temperature on the OLED display.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>  display.setTextSize(1);\n  display.setCursor(0,0);\n  display.print(\"Temperature: \");\n  display.setTextSize(2);\n  display.setCursor(0,10);\n  display.print(t);\n  display.print(\" \");\n  display.setTextSize(1);\n  display.cp437(true);\n  display.write(167);\n  display.setTextSize(2);\n  display.print(\"C\");<\/code><\/pre>\n\n\n\n<p>We use the <span class=\"rnthl rntliteral\">setTextSize()<\/span> method to define the font size, the <span class=\"rnthl rntliteral\">setCursor()<\/span> sets where the text should start being displayed and the <span class=\"rnthl rntliteral\">print()<\/span> method is used to write something on the display.<\/p>\n\n\n\n<p>To print the temperature and humidity  you just need to pass their variables to the <span class=\"rnthl rntliteral\">print()<\/span> method as follows:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>display.print(t);<\/code><\/pre>\n\n\n\n<p>The &#8220;Temperature&#8221; label is displayed in size 1, and the actual reading is displayed in size 2. <\/p>\n\n\n\n<p>To display the \u00ba symbol, we use the <a href=\"https:\/\/www.ascii-codes.com\/\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\">Code Page 437<\/a> font. For that, you need to set the <span class=\"rnthl rntliteral\">cp437<\/span> to <span class=\"rnthl rntliteral\">true<\/span> as follows:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>display.cp437(true);<\/code><\/pre>\n\n\n\n<p>Then, use the <span class=\"rnthl rntliteral\">write()<\/span> method to display your chosen character. The \u00ba symbol corresponds to character 167.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>display.write(167);<\/code><\/pre>\n\n\n\n<p>A similar approach is used to display the humidity:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>display.setTextSize(1);\ndisplay.setCursor(0, 35);\ndisplay.print(\"Humidity: \");\ndisplay.setTextSize(2);\ndisplay.setCursor(0, 45);\ndisplay.print(h);\ndisplay.print(\" %\"); <\/code><\/pre>\n\n\n\n<p>Don&#8217;t forget that you need to call <span class=\"rnthl rntliteral\">display.display()<\/span> at the end, so that you can actually display something on the OLED. <\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>display.display(); <\/code><\/pre>\n\n\n\n<p><strong>Recommended reading:<\/strong> <a href=\"https:\/\/randomnerdtutorials.com\/esp32-dht11-dht22-temperature-humidity-sensor-arduino-ide\/\">ESP32 with DHT11\/DHT22 Temperature and Humidity Sensor using Arduino IDE<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Demonstration<\/h2>\n\n\n\n<p>The following figure shows what you should get at the end of this tutorial. Humidity and temperature readings are displayed on the OLED.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"630\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/05\/ESP32-ESP8266-OLED-DHT22-DHT11-Display-Temperature-Humidity-readings.jpg?resize=750%2C630&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"DHT11 DHT22 sensor display temperature humidity readings in OLED display ESP8266 ESP32 using Arduino IDE\" class=\"wp-image-85556\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/05\/ESP32-ESP8266-OLED-DHT22-DHT11-Display-Temperature-Humidity-readings.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/05\/ESP32-ESP8266-OLED-DHT22-DHT11-Display-Temperature-Humidity-readings.jpg?resize=300%2C252&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Troubleshooting<\/h2>\n\n\n\n<p>If your DHT sensor fails to get the readings or you get the message &#8220;Failed to read from DHT sensor&#8221;, read our&nbsp;<a href=\"https:\/\/randomnerdtutorials.com\/solved-dht11-dht22-failed-to-read-from-dht-sensor\/\"><strong>DHT Troubleshooting Guide<\/strong><\/a>&nbsp;to help you solve that problem.<\/p>\n\n\n\n<p>If you get the &#8220;<strong>SSD1306 allocation failed<\/strong>&#8221; error or if the OLED is not displaying anything in the screen, it can be one of the following issues:<\/p>\n\n\n\n<p><strong>Wrong I2C address<\/strong><\/p>\n\n\n\n<p>The I2C address for the OLED display we are using is 0x3C. However, yours may be different. So, make sure you check your display I2C address using an <a href=\"https:\/\/raw.githubusercontent.com\/RuiSantosdotme\/Random-Nerd-Tutorials\/master\/Projects\/LCD_I2C\/I2C_Scanner.ino\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\">I2C scanner sketch<\/a>.<\/p>\n\n\n\n<p><strong>SDA and SCL not connected properly<\/strong><\/p>\n\n\n\n<p>Please make sure that you have the SDA and SCL pins of the OLED display wired correctly. If you&#8217;re using:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>ESP32: connect SDA pin to <span class=\"rnthl rntcyellow\">GPIO 21<\/span> and SCL pin to <span class=\"rnthl rntclgray\">GPIO 22<\/span><\/li><li>ESP8266: connect SDA pin to <span class=\"rnthl rntcyellow\">GPIO 4<\/span> (D2) and SCL pin to <span class=\"rnthl rntclgray\">GPIO 5<\/span> (D1)<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Wrapping Up<\/h2>\n\n\n\n<p>We hope you&#8217;ve found this tutorial about displaying sensor readings on the OLED display useful. The OLED display is a great way to add a user interface to your projects. If you like this project, you may also like to know how to display sensor readings in your browser using an ESP Web Server:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/randomnerdtutorials.com\/esp32-dht11-dht22-temperature-humidity-web-server-arduino-ide\/\">ESP32 DHT Web Server (Arduino IDE)<\/a><\/li><li><a href=\"https:\/\/randomnerdtutorials.com\/esp8266-dht11dht22-temperature-and-humidity-web-server-with-arduino-ide\/\">ESP8266 DHT Web Server (Arduino IDE)<\/a><\/li><li><a href=\"https:\/\/randomnerdtutorials.com\/micropython-esp32-esp8266-dht11-dht22-web-server\/\">ESP32\/ESP8266 DHT Web Server (MicroPython)<\/a><\/li><\/ul>\n\n\n\n<p>You can learn more about the ESP32 and ESP8266 with our courses:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/randomnerdtutorials.com\/learn-esp32-with-arduino-ide\/\">Learn ESP32 with Arduino IDE<\/a><\/li><li><a href=\"https:\/\/randomnerdtutorials.com\/home-automation-using-esp8266\/\">Home Automation using ESP8266<\/a><\/li><\/ul>\n\n\n\n<p>Thanks for reading.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Learn how to display temperature and humidity readings from a DHT11\/DHT22 sensor in an SSD1306 OLED display using an ESP32 or an ESP8266 with Arduino IDE. The idea of using &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"ESP32\/ESP8266: DHT Temperature and Humidity Readings in OLED Display\" class=\"read-more button\" href=\"https:\/\/randomnerdtutorials.com\/esp32-esp8266-dht-temperature-and-humidity-oled-display\/#more-85288\" aria-label=\"Read more about ESP32\/ESP8266: DHT Temperature and Humidity Readings in OLED Display\">CONTINUE READING \u00bb<\/a><\/p>\n","protected":false},"author":5,"featured_media":85532,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[276,281,277,299,265,214,246,300,264],"tags":[],"class_list":["post-85288","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-esp32","category-esp32-project","category-esp32-arduino-ide","category-0-esp32","category-esp8266-project","category-esp8266","category-esp8266-arduino-ide","category-0-esp8266","category-project"],"aioseo_notices":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/05\/ESP32-ESP8266-OLED-DHT22-DHT11-Temperature-Humidity.jpg?fit=1280%2C720&quality=100&strip=all&ssl=1","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/85288","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/comments?post=85288"}],"version-history":[{"count":0,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/85288\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media\/85532"}],"wp:attachment":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media?parent=85288"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/categories?post=85288"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/tags?post=85288"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}