Why do I have to open Serial Monitor to display non-NaN values retrieved from DHT22,
what am I doing wrong in the code below?

#include <DHT.h>
#include <Wire.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_GFX.h>
// OLED display TWI address
#define OLED_ADDR   0x3C
// reset pin not used on 4-pin OLED module
Adafruit_SSD1306 display(-1);  // -1 = no reset pin
// 128 x 64 pixel display
#if (SSD1306_LCDHEIGHT != 64)
#error("Height incorrect, please fix Adafruit_SSD1306.h!");
#define DHTPIN D3
#define DHTTYPE DHT22
void setup() {

  pinMode (DHTPIN, OUTPUT);
  /// initialize and clear display
  display.begin(SSD1306_SWITCHCAPVCC, OLED_ADDR);
  //  display.drawRect(1, 1, display.width()-1, display.height()-1, WHITE);   // draws the outer rectangular boundary on the screen
  // display a pixel in each corner of the screen
  display.drawPixel(0, 0, WHITE);
  display.drawPixel(127, 0, WHITE);
  display.drawPixel(0, 63, WHITE);
  display.drawPixel(127, 63, WHITE);
  // display a line of text
  display.setCursor(20, 18);
  display.setCursor(25, 40);

  // update display with all of the above graphics
void loop() {
  // put your main code here, to run repeatedly:
  float h = dht.readHumidity();
  float t = dht.readTemperature();
  display.setCursor(0, 1);
  display.print("Showing Temperature");
  // display Temperature
  display.setCursor(1, 18);
  display.print("T : ");
  display.setCursor(1, 40);
  display.print("H : ");
  display.print(" %");
  // update display with all of the above graphics

After uploading code it does will display NaN but when I open serial monitor it works fine displaying actual values from DHT22 :o

below is the fritzing of the hookup, I do not have witty cloud in fritzing, so I have used wemos D1Mini instead. on wemos d1 mini 3v3 is VCC on witty cloud dev board.

enter image description here

It looks like loop function doesn't execute by itself unless I open the serial monitor, why is it so? please help

  • What happens if you remove/comment out "Serial.begin(115200);"? – Mikael Patel Nov 18 '17 at 20:33
  • Please show your wiring schematic. Do you use pull-up resistors for RX and TX? – AltAir Nov 18 '17 at 22:15
  • 2
    Also asked at: forum.arduino.cc/index.php?topic=512284 If you're going to do that then please be considerate enough to add links to the other places you cross posted. This will let us avoid wasting time due to duplicate effort and also help others who have the same questions and find your post to discover all the relevant information. – per1234 Nov 18 '17 at 23:30
  • if i do not have serial monitor open loop function doesnt work. if I have serial monitor open the OLED updates, if i close serial monitor window the text on doest updates from the loop function. why is loop function dependent on serial monitor window being open ? – Ciasto piekarz Nov 19 '17 at 5:48
  • @MikaelPatel I have tried commenting that too didnt help, long time back I had similar issue and commented the serial.begin and it worked but with witty cloud dev board it didnt. – Ciasto piekarz Nov 19 '17 at 5:56

Hard to tell from your fritzing which actual ESP8266 pin you are using, but you require a pull up resistor on the data line of the DHT22.

My guess is that you are using one of the serial UART pins for the data connection, and opening the serial monitor activates the output of the onboard UART chip which then acts as your missing pullup resistor.

| improve this answer | |
  • Used D7 i.e. GPIO13 on witty cloud. – Ciasto piekarz Nov 19 '17 at 10:14

Serial monitor sends a RESET signal during its initialization - maybe this is the problem. Sometimes these Witty boards need an implicit RESET after power up / programming.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.