{"id":2070,"date":"2020-08-02T17:07:46","date_gmt":"2020-08-02T09:07:46","guid":{"rendered":"https:\/\/www.techcoil.com\/blog\/?p=2070"},"modified":"2020-12-12T22:44:59","modified_gmt":"2020-12-12T14:44:59","slug":"how-to-show-the-temperature-and-humidity-of-your-room-on-a-heltec-wifi-kit-32-attached-to-a-dht11-dht22-sensor","status":"publish","type":"post","link":"https:\/\/www.techcoil.com\/blog\/how-to-show-the-temperature-and-humidity-of-your-room-on-a-heltec-wifi-kit-32-attached-to-a-dht11-dht22-sensor\/","title":{"rendered":"How to show the temperature and humidity of your room on a Heltec WiFi Kit 32 attached to a DHT11 \/ DHT22 sensor"},"content":{"rendered":"<p>When you have a <a href=\"https:\/\/www.amazon.com\/Temperature-Humidity-Sensor-Arduino-Raspberry\/dp\/B07T7ZR7MS\/ref=as_li_ss_tl?ie=UTF8&linkCode=ll1&tag=clivsperswebs-20&linkId=97430b8e1e09bc16aadb36b138825088&language=en_US\" rel=\"noopener\" target=\"_blank\">DHT11<\/a> \/ <a href=\"https:\/\/www.amazon.com\/HiLetgo-Temperature-Humidity-Electronic-Practice\/dp\/B0795F19W6\/ref=as_li_ss_tl?ie=UTF8&linkCode=ll1&tag=clivsperswebs-20&linkId=dc5a9f3a8d61198a3029b2a4b5e03071&language=en_US\" rel=\"noopener\" target=\"_blank\">DHT22<\/a> sensor, you can measure the temperature and humidity of your room. If you attach the sensor to a <a href=\"https:\/\/www.amazon.com\/MakerFocus-Development-0-96inch-Display-Compatible\/dp\/B076KJZ5QM\/ref=as_li_ss_tl?ie=UTF8&linkCode=ll1&tag=clivsperswebs-20&linkId=1e68e6b616e39cacb5d39e71c0c3a126&language=en_US\" rel=\"noopener\" target=\"_blank\">Heltec WiFi Kit 32 development board<\/a>, you will be able to put up the readings on a 0.96 Inch OLED screen.<\/p>\n<p>Given these points, let's look at how we can show the temperature and humidity readings of your room on a Heltec WiFi Kit 32 development board attached to a DHTXX sensor.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/DHT11-connected-to-Heltec-WiFi-32-Kit-with-readings-on-OLED-display.jpg\" alt=\"DHT11 connected to Heltec WiFi 32 Kit with readings on OLED display\" class=\"aligncenter\" \/><\/p>\n<h2>1. Get the hardware to build your thermometer and humidity gauge<\/h2>\n<p>First, you will want to make sure that you have the following hardware:<\/p>\n<ul>\n<li><a href=\"https:\/\/www.amazon.com\/MakerFocus-Development-0-96inch-Display-Compatible\/dp\/B076KJZ5QM\/ref=as_li_ss_tl?ie=UTF8&linkCode=ll1&tag=clivsperswebs-20&linkId=1e68e6b616e39cacb5d39e71c0c3a126&language=en_US\" rel=\"noopener\" target=\"_blank\">Heltec WiFi Kit 32 development board<\/a><\/li>\n<li><a href=\"https:\/\/www.amazon.com\/Temperature-Humidity-Sensor-Arduino-Raspberry\/dp\/B07T7ZR7MS\/ref=as_li_ss_tl?ie=UTF8&linkCode=ll1&tag=clivsperswebs-20&linkId=97430b8e1e09bc16aadb36b138825088&language=en_US\" rel=\"noopener\" target=\"_blank\">DHT11<\/a> \/ <a href=\"https:\/\/www.amazon.com\/HiLetgo-Temperature-Humidity-Electronic-Practice\/dp\/B0795F19W6\/ref=as_li_ss_tl?ie=UTF8&linkCode=ll1&tag=clivsperswebs-20&linkId=dc5a9f3a8d61198a3029b2a4b5e03071&language=en_US\" rel=\"noopener\" target=\"_blank\">DHT22<\/a> sensor<\/li>\n<li><a href=\"https:\/\/www.amazon.com\/Anker-3-Pack-Powerline-Micro-USB\/dp\/B015MJLEUS\/\" rel=\"noopener\" target=\"_blank\">microUSB cable<\/a><\/li>\n<li><a href=\"https:\/\/www.amazon.com\/ANBES-Soldering-Iron-Kit-Electronics\/dp\/B06XZ31W3M\/ref=as_li_ss_tl?ie=UTF8&linkCode=ll1&tag=clivsperswebs-20&linkId=1943a7369bcc3e6e88bb2a7318049869&language=en_US\" rel=\"noopener\" target=\"_blank\">Soldering iron kit<\/a><\/li>\n<li><a href=\"https:\/\/www.amazon.com\/KESTER-SOLDER-32117-24-6040-0027-Diameter\/dp\/B00068IJPO\/ref=as_li_ss_tl?th=1&linkCode=ll1&tag=clivsperswebs-20&linkId=4aa768bd32434858071470a5b6125d06&language=en_US\" rel=\"noopener\" target=\"_blank\">Solder wire<\/a><\/li>\n<\/ul>\n<h2>2. Connect your DHT11 \/ DHT 22 sensor to your WiFi Kit 32 development board<\/h2>\n<p>After you have gotten the hardware, use your soldering iron and wire to solder the GPIO headers to your WiFi Kit 32 development board.<\/p>\n<p>When you have done so, you can then connect your DHTxx sensor to your development board.<\/p>\n<p>When you look at your DHT11 \/ DHT 22 sensor, you can see three pins labeled <strong>+\/VCC<\/strong>, <strong>out\/s\/data<\/strong> and <strong>-\/GND<\/strong>. Connect the:<\/p>\n<ul>\n<li><strong>+\/VCC<\/strong> pin to a <strong>3v3<\/strong> pin on your board<\/li>\n<li><strong>out\/s\/data<\/strong> pin to <strong>GPIO27<\/strong> on your board<\/li>\n<li><strong>-\/GND<\/strong> pin to a <strong>GND<\/strong> pin on your board<\/li>\n<\/ul>\n<p>For example, if you have a DHT11 sensor, this is how your connection may look like:<img decoding=\"async\" src=\"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/DHT11-connected-to-Heltec-WiFi-32-Kit-with-pins-clearly-visible.jpg\" alt=\"DHT11 connected to Heltec WiFi 32 Kit with pins clearly visible\" class=\"aligncenter\" \/><\/p>\n<h2>3. Prepare the Arduino IDE environment for ESP32 development<\/h2>\n<p>At this point in time, you are ready to flash a program into your ESP32 board to read from the DHT11 sensor. In order to do so, we need to use a tool to write our program into the flash memory of the development board.<\/p>\n<p>Since it is easy to write code and flash programs with <a href=\"https:\/\/www.arduino.cc\/en\/main\/software\" rel=\"noopener\" target=\"_blank\">Arduino IDE<\/a>, we can use it to serve our purpose.<\/p>\n<p>In order to use Arduino IDE for this guide, be sure to <a href=\"https:\/\/www.techcoil.com\/blog\/enabling-esp32-development-on-arduino-ide\/\" rel=\"noopener\" target=\"_blank\">enable ESP32 development on Arduino IDE<\/a> before continuing.<\/p>\n<h2>4. Install Heltec ESP32 board and Heltec ESP32 Library <\/h2>\n<p>Once you have the environment to flash a program to a ESP32 development board, <a href=\"https:\/\/heltec-automation-docs.readthedocs.io\/en\/latest\/esp32+arduino\/quick_start.html\" rel=\"noopener\" target=\"_blank\">setup Heltec ESP32 board and library on your Arduino IDE<\/a>. <\/p>\n<h2>5. Install the DHT sensor library for ESPx to read temperature and humidity from your DHT11 \/ DHT22 sensor<\/h2>\n<p>In addition to Heltec board and library, you will need to install the DHT sensor library for ESPx to read temperature and humidity from your DHT11 \/ DHT22 sensor.<\/p>\n<p>In order to do so, first go to <strong>Tools -> Manage Libraries...<\/strong>. After you had done so, the <strong>Library Manager<\/strong> window will appear. Search for <strong>DHT ESPx<\/strong> and install the <a href=\"https:\/\/github.com\/beegee-tokyo\/DHTesp\" rel=\"noopener\" target=\"_blank\">DHT sensor library for ESPx by beegee_tokyo<\/a>:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/Mac-Arduino-IDE-1.8.9-Library-Manager-with-libraries-filtered-by-DHT-sensor-library-for-ESPx.gif\" alt=\"Mac Arduino IDE 1.8.9 Library Manager with libraries filtered by DHT sensor library for ESPx\" class=\"aligncenter\" \/><\/p>\n<h2>6. Write the Arduino Sketch to read temperature and humidity from DHT sensor and display it on Heltec OLED<\/h2>\n<p>At this point in time, you will have everything needed to compose the Arduino Sketch to do what we had intended to do .<\/p>\n<p>In order to read temperature and humidity from DHT sensor and display it on the OLED, we can write the following sketch:<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\n#include &quot;DHTesp.h&quot;\r\n#include &quot;heltec.h&quot;\r\nDHTesp dht;\r\n\r\nfloat currentTemp;\r\nfloat currentHumidity;\r\n\r\nvoid displayReadingsOnOled() {\r\n  \r\n  String temperatureDisplay =&quot;Temperature: &quot; + (String)currentTemp +  &quot;\u00b0C&quot;;\r\n  String humidityDisplay = &quot;Humidity: &quot; + (String)currentHumidity + &quot;%&quot;;\r\n\r\n  \/\/ Clear the OLED screen\r\n  Heltec.display-&gt;clear();\r\n  \/\/ Prepare to display temperature\r\n  Heltec.display-&gt;drawString(0, 0, temperatureDisplay);\r\n  \/\/ Prepare to display humidity\r\n  Heltec.display-&gt;drawString(0, 12, humidityDisplay);\r\n  \/\/ Display the readings\r\n  Heltec.display-&gt;display();\r\n}\r\n   \r\nvoid setup()\r\n{\r\n\r\n  dht.setup(27, DHTesp::DHT11);\r\n  \r\n  currentTemp = dht.getTemperature();\r\n  currentHumidity = dht.getHumidity();\r\n\r\n  pinMode(LED,OUTPUT);\r\n  digitalWrite(LED,HIGH);\r\n\r\n  Heltec.begin(true \/*DisplayEnable Enable*\/, false \/*LoRa Enable*\/, false \/*Serial Enable*\/);\r\n  displayReadingsOnOled();\r\n}\r\n\r\n\r\n   \r\nvoid loop()\r\n{\r\n  float temperature = dht.getTemperature();\r\n  float humidity = dht.getHumidity();\r\n\r\n  if (temperature != currentTemp || humidity != currentHumidity) {\r\n    currentTemp = temperature;\r\n    currentHumidity = humidity;\r\n    displayReadingsOnOled();\r\n  }\r\n   \r\n  delay(2000);\r\n}\r\n<\/pre>\n<p>So what is the sketch doing? <\/p>\n<p>First, we include the <code>DHTesp.h<\/code> and <code>heltec.h<\/code> header files that contain the functions that we will need for our sketch.<\/p>\n<p>After that, we create a <code>DHTesp<\/code> object to interact with the DHT sensor. <\/p>\n<p>Next, we declare <code>currentTemp<\/code> and <code>currentHumidity<\/code> to hold float values that represent a set of temperature and humidity readings.<\/p>\n<p>After that, we define the <code>displayReadingsOnOled<\/code> function that will display the temperature and humidity readings to the OLED. <\/p>\n<h3>Inside the <code>displayReadingsOnOled<\/code> function<\/h3>\n<p>First, we construct the <a href=\"https:\/\/www.arduino.cc\/reference\/en\/language\/variables\/data-types\/stringobject\/\" rel=\"noopener\" target=\"_blank\">Strings<\/a> that will output the temperature and humidity readings to the display.<\/p>\n<p>After that, we use <code>Heltec.display->clear<\/code> to clear the contents of the OLED.<\/p>\n<p>Once we have done so, we prepare the screen to display the temperature and humidity readings with <code>Heltec.display->drawString<\/code>. <\/p>\n<p>Finally, we make a call to <code>Heltec.display->display<\/code> to show the readings on the OLED.<\/p>\n<h3>Inside the <code>setup<\/code> function<\/h3>\n<p>Since the <a href=\"https:\/\/www.arduino.cc\/reference\/en\/language\/structure\/sketch\/setup\/\" rel=\"noopener\" target=\"_blank\"><code>setup<\/code> function<\/a> is called once, we use it to run the logic to initialise the resources that we will need.<\/p>\n<p>Given that we first call <code>dht.setup<\/code> on get the board to read from our DHT sensor at pin 27. <\/p>\n<p>After that, we get the first set of temperature and humidity reading from the DHT sensor.<\/p>\n<p>When we have done so, we use <code>pinMode<\/code>, <code>digitalWrite<\/code> and <code>Heltec.begin<\/code> to initialise the OLED.<\/p>\n<p>Finally, we call <code>displayReadingsOnOled<\/code> to display the first set of readings to the OLED.<\/p>\n<h3>Inside the <code>loop<\/code> function<\/h3>\n<p>Since the <a href=\"https:\/\/www.arduino.cc\/reference\/en\/language\/structure\/sketch\/loop\/\" rel=\"noopener\" target=\"_blank\"><code>loop<\/code> function<\/a> is called for as long as the board has power, we use it to run the logic to read from the DHT sensor.<\/p>\n<p>Whenever <code>loop<\/code> is called, we get a set of readings and compare it with the previous set of readings.<\/p>\n<p>If there is a change in the new set of readings, then we first update <code>currentTemp<\/code> and <code>currentHumidity<\/code> with the new values. After that, we call <code>displayReadingsOnOled<\/code> to display the latest set of readings to the OLED.<\/p>\n<p>Finally at the end of the <code>loop<\/code> function, we call <code>delay<\/code> to wait for 2 seconds before reading from the sensor again.<\/p>\n<h2>7. Upload the Arduino Sketch to your Heltec WiFi 32 board<\/h2>\n<p>Once you have created the Arduino Sketch, connect the microUSB cable to your Heltec WiFi 32 board and computer.<\/p>\n<p>After that, select <strong>WiFi Kit 32<\/strong> as the board to use in your Arduino IDE:<img decoding=\"async\" src=\"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/Mac-Arduino-IDE-1.8.9-selecting-Heltec-ESP32-WiFI-Kit-32-board.gif\" alt=\"Mac Arduino IDE 1.8.9 selecting Heltec ESP32 WiFI Kit 32 board\" class=\"aligncenter\" \/><\/p>\n<p>In addition, select the serial port to upload the sketch:<img decoding=\"async\" src=\"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/Mac-Arduino-IDE-1.8.7-selecting-SILAB-USBtoUART-driver-as-the-port-to-flash-program-to.gif\" alt=\"Mac Arduino IDE 1.8.7 selecting SILAB USBtoUART driver as the port to flash program to\" class=\"aligncenter\" \/><\/p>\n<p>Once you had done so, upload your Arduino Sketch to your Heltec WiFI 32 board:<img decoding=\"async\" src=\"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/Mac-Arduino-1.8.9-IDE-after-uploading-sketch-to-read-temperature-and-humidity-on-Heltec-WiFi-32-Kit.gif\" alt=\"Mac Arduino 1.8.9 IDE after uploading sketch to read temperature and humidity on Heltec WiFi 32 Kit\" class=\"aligncenter\" \/><\/p>\n<p>When your Arduino sketch is loaded onto the Heltec WiFi 32 board, you will get the temperature and humidity readings on the OLED screen when there is power supplied to the board.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/How-to-show-the-temperature-and-humidity-of-your-room-on-a-Heltec-WiFi-Kit-32-attached-to-a-DHT11-or-DHT22-sensor.jpg\" alt=\"How to show the temperature and humidity of your room on a Heltec WiFi Kit 32 attached to a DHT11 or DHT22 sensor\" class=\"aligncenter\" \/><\/p>\n\n      <ul id=\"social-sharing-buttons-list\">\n        <li class=\"facebook\">\n          <a href=\"https:\/\/www.facebook.com\/sharer\/sharer.php?u=https%3A%2F%2Fwp.me%2Fp245TQ-xo\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n            <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Facebook.png\" alt=\"Facebook icon\"> Share\n          <\/a>\n        <\/li>\n        <li class=\"twitter\">\n          <a href=\"https:\/\/twitter.com\/intent\/tweet?text=&url=https%3A%2F%2Fwp.me%2Fp245TQ-xo&via=Techcoil_com\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Twitter.png\" alt=\"Twitter icon\"> Tweet\n          <\/a>\n        <\/li>\n        <li class=\"linkedin\">\n          <a href=\"https:\/\/www.linkedin.com\/shareArticle?mini=1&title=&url=https%3A%2F%2Fwp.me%2Fp245TQ-xo&source=https:\/\/www.techcoil.com\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/linkedin.png\" alt=\"Linkedin icon\"> Share\n          <\/a>\n        <\/li>\n        <li class=\"pinterest\">\n          <a href=\"https:\/\/pinterest.com\/pin\/create\/button\/?url=https%3A%2F%2Fwww.techcoil.com%2Fblog%2Fwp-json%2Fwp%2Fv2%2Fposts%2F2070&description=\" class=\"pin-it-button\" target=\"_blank\" role=\"button\" rel=\"nofollow\" count-layout=\"horizontal\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Pinterest.png\" alt=\"Pinterest icon\"> Save\n          <\/a>\n        <\/li>\n      <\/ul>\n    ","protected":false},"excerpt":{"rendered":"<p>When you have a <a href=\"https:\/\/www.amazon.com\/Temperature-Humidity-Sensor-Arduino-Raspberry\/dp\/B07T7ZR7MS\/ref=as_li_ss_tl?ie=UTF8&#038;linkCode=ll1&#038;tag=clivsperswebs-20&#038;linkId=97430b8e1e09bc16aadb36b138825088&#038;language=en_US\" rel=\"noopener\" target=\"_blank\">DHT11<\/a> \/ <a href=\"https:\/\/www.amazon.com\/HiLetgo-Temperature-Humidity-Electronic-Practice\/dp\/B0795F19W6\/ref=as_li_ss_tl?ie=UTF8&#038;linkCode=ll1&#038;tag=clivsperswebs-20&#038;linkId=dc5a9f3a8d61198a3029b2a4b5e03071&#038;language=en_US\" rel=\"noopener\" target=\"_blank\">DHT22<\/a> sensor, you can measure the temperature and humidity of your room. If you attach the sensor to a <a href=\"https:\/\/www.amazon.com\/MakerFocus-Development-0-96inch-Display-Compatible\/dp\/B076KJZ5QM\/ref=as_li_ss_tl?ie=UTF8&#038;linkCode=ll1&#038;tag=clivsperswebs-20&#038;linkId=1e68e6b616e39cacb5d39e71c0c3a126&#038;language=en_US\" rel=\"noopener\" target=\"_blank\">Heltec WiFi Kit 32 development board<\/a>, you will be able to put up the readings on a 0.96 Inch OLED screen.<\/p>\n<p>Given these points, let&#8217;s look at how we can show the temperature and humidity readings of your room on a Heltec WiFi Kit 32 development board attached to a DHTXX sensor.<\/p>\n","protected":false},"author":1,"featured_media":2071,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"footnotes":""},"categories":[4],"tags":[588,555,631,630,745,484],"jetpack_featured_media_url":"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/DHT11-connected-to-Heltec-WiFi-32-Kit-with-readings-on-OLED-display.jpg","jetpack_shortlink":"https:\/\/wp.me\/p245TQ-xo","jetpack-related-posts":[],"jetpack_likes_enabled":true,"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts\/2070"}],"collection":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/comments?post=2070"}],"version-history":[{"count":0,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts\/2070\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/media\/2071"}],"wp:attachment":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/media?parent=2070"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/categories?post=2070"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/tags?post=2070"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}