Oled 128x32 + DHT22 sensor boot/sync problem

Hi all.
Recently I started a project.
Arduino Uno, DHT22 sensor, OLED display 128x32.
OLED display connected to 3.3v vcc
DHT22 3.3V vcc

Uno as test area, couse I want to use Pro Micro 3.3v later.
Searching examples I found couple and tried them. I saw the problem.
After disconnecting from USB and reconnect I need to push reset button to see something on display or open Serial Monitor, same with external power. In one experiment I just disconnect DHT22 sensor, and reconnect the power, right away the display shows initialized values.
As I understand this is sync problem .
My code below

#include <Adafruit_SSD1306.h>
#include <Adafruit_GFX.h>
#include <Wire.h>
#include <DHT.h>;

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 32 // OLED display height, in pixels
#define OLED_RESET 4
#define DHTPIN 7     
#define DHTTYPE DHT22   // DHT 22  (AM2302)

//Adafruit_SSD1306 display(OLED_RESET);
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
DHT dht(DHTPIN, DHTTYPE); //// Initialize DHT sensor for normal 16mhz Arduino

int t = 99;
int h = 99;

void setup() {
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  display.display();
  delay(2000);
  display.clearDisplay ();   
  dht.begin();
  delay(2000);
  //Serial.begin(9600);
}

void loop() {  
  delay(2000);
  getDHT();
  oneLine();

  //For debug
  //Serial.print(F("Humidity: "));
  //Serial.print(h);
  //Serial.print(F("%  Temperature: "));
  //Serial.print(t);
  //Serial.println(F("°C "));  
  //delay (1000);
}

void oneLine(){
  display.clearDisplay (); // clear display
  display.setCursor (0,7); // position the cursor (X - coordinate, Y - coordinate)
  display.setTextSize (3); // medium size font
  display.setTextColor (WHITE); // white is not default ! 
  display.print(String(t)); //display temperature from sensor
  display.drawCircle (38,10,3, WHITE);
  display.setCursor (43,7);
  display.print("C ");
  display.setCursor (74,7); // position the cursor (X - coordinate, Y - coordinate)
  display.print (String(h)+"%");
  display.display ();
}

void getDHT()
{
  h = dht.readHumidity();
  // Read temperature as Celsius (the default)
  t = dht.readTemperature();
}

*DHT22 sensor comes on board with resistor build in
Try to increase delay, no effect
Thanks for the help

I tried with regular LCD16x2, no problem found, disconnecting and reconnecting power LCD start working right away.

I found LCD I2C display, and tried the same - problem remains, Nothing on screen after reconnecting the power. Reset button pressed I see DHT readings. So for my understanding this issue between I2C protocol and DHT library, some collision.

Issue solved by using different DHT library - DHTNEW by RobTillaart found here and it has more accurate readings

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.