OLED issue with DHT11

Hey Guys,

I have wired up a project to use the DHT11, a range sensor HC-SR04 and an OLED screen too eventually display their readings on an mini OLED screen

But when I introduce the OLED code, the DHT11 readings through the serial.display shows “nan”

But if I remove the OLED code, it works

// ----------------------------------------------------------------------------------
// The LORA pre-Setup  --------------------------------------------------------------
// ----------------------------------------------------------------------------------
#include <TheThingsNetwork.h>
// Set your AppEUI and AppKey
#define loraSerial Serial1
#define debugSerial Serial
#define freqPlan TTN_FP_EU868
TheThingsNetwork ttn(loraSerial, debugSerial, freqPlan);


// ----------------------------------------------------------------------------------
// The OLED pre-Setup ---------------------------------------------------------------
// ----------------------------------------------------------------------------------
#include <Adafruit_GFX.h>  // Include core graphics library for the display
#include <Adafruit_SSD1306.h>  // Include Adafruit_SSD1306 library to drive the display
Adafruit_SSD1306 display(128, 64);  // Create display
#include <Fonts/FreeMonoBold12pt7b.h>  // Add a custom font
#include <Fonts/FreeMono9pt7b.h>  // Add a custom font




// ----------------------------------------------------------------------------------
// The Range Sensor -----------------------------------------------------------------
// ----------------------------------------------------------------------------------
const int trigPin = 0;
const int echoPin = 1;
long duration;
int distance;


// ----------------------------------------------------------------------------------
// Temp & Humidity Sensor -----------------------------------------------------------
// ----------------------------------------------------------------------------------
#include "DHT.h"
#define DHTPIN 3     // Digital pin connected to the DHT sensor
#define DHTTYPE DHT11   // DHT 11
DHT dht(DHTPIN, DHTTYPE);




void setup()
{

  // ----------------------------------------------------------------------------------
  // The OLED Setup ---------------------------------------------------------------
  // ----------------------------------------------------------------------------------
  delay(100);  // This delay is needed to let the display to initialize
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);  // Initialize display with the I2C address of 0x3C
  display.clearDisplay();  // Clear the buffer
  display.setTextColor(WHITE);  // Set color of the text
  display.setRotation(0);  // Set orientation. Goes from 0, 1, 2 or 3
  display.setTextWrap(false);  // By default, long lines of text are set to automatically “wrap” back to the leftmost column.
  // To override this behavior (so text will run off the right side of the display - useful for
  // scrolling marquee effects), use setTextWrap(false). The normal wrapping behavior is restored
  // with setTextWrap(true).
  display.dim(0);  //Set brightness (0 is maximun and 1 is a little dim)









  // ----------------------------------------------------------------------------------
  // The Temp & Humidity Sensor Setup ----------------------------------------------------
  // ----------------------------------------------------------------------------------
  Serial.begin(9600);
  Serial.println(F("DHTxx test!"));
  dht.begin();


  // ----------------------------------------------------------------------------------
  // The Range Sensor Setup ----------------------------------------------------
  // ----------------------------------------------------------------------------------
  pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
  pinMode(echoPin, INPUT); // Sets the echoPin as an Input
  Serial.begin(9600); // Starts the serial communication




  // ----------------------------------------------------------------------------------
  // The LORA Network Setup ----------------------------------------------------
  // ----------------------------------------------------------------------------------
  //  loraSerial.begin(57600);
  //  debugSerial.begin(9600);
  //
  //  // Wait a maximum of 10s for Serial Monitor
  //  while (!debugSerial && millis() < 10000)
  //    ;
  //
  //  debugSerial.println("-- STATUS");
  //  ttn.showStatus();
  //
  //  debugSerial.println("-- JOIN");
  //  ttn.join(appEui, appKey);


}








void loop()
{








  // ----------------------------------------------------------------------------------
  // LOOP for Temp & Humidity Sensor --------------------------------------------------
  // ----------------------------------------------------------------------------------
  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  float h = dht.readHumidity();
  // Read temperature as Celsius (the default)
  float t = dht.readTemperature();
  // Read temperature as Fahrenheit (isFahrenheit = true)
  float f = dht.readTemperature(true);

  // Check if any reads failed and exit early (to try again).
  if (isnan(h) || isnan(t) || isnan(f)) {
    Serial.println(F("Failed to read from DHT sensor!"));
    return;
  }
  // Compute heat index in Fahrenheit (the default)
  float hif = dht.computeHeatIndex(f, h);
  // Compute heat index in Celsius (isFahreheit = false)
  float hic = dht.computeHeatIndex(t, h, false);



  Serial.print(F("Humidity: "));
  Serial.print(h);
  Serial.print(F("%   Temperature: "));
  Serial.print(t);
  Serial.print(F("°C   "));










  // ----------------------------------------------------------------------------------
  // LOOP for Range Sensor ---------------------------------------------------------------
  // -------------------------------------------------------------------------------------
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);

  // Sets the trigPin on HIGH state for 10 micro seconds
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  // Reads the echoPin, returns the sound wave travel time in microseconds
  duration = pulseIn(echoPin, HIGH);

  // Calculating the distance
  distance = duration * 0.034 / 2;

  // Prints the distance on the Serial Monitor
  Serial.print("Distance: ");
  Serial.print(distance);
  Serial.println(F("cm   "));






  // ----------------------------------------------------------------------------------
  // The OLED Loop --------------------------------------------------------------------
  // ----------------------------------------------------------------------------------
  display.clearDisplay();  // Clear the display so we can refresh


  display.setFont(&FreeMono9pt7b);  // Set a custom font
  display.setTextSize(0);  // Set text size. We are using a custom font so you should always use the text size of 0
  // Print text:
  //display.setCursor(0, 10);  // (x,y)
  //display.println("Zachary");  // Text or value to print

  display.setCursor(0, 25);  // (x,y)
  display.print("Temp: ");  // Text or value to print
  //display.println("Temp: ");  // Text or value to prin


  display.display();  // Print everything we set previously








  // ----------------------------------------------------------------------------------
  // Loop for LORA Network ------------------------------------------------------------
  // ----------------------------------------------------------------------------------
  //  uint32_t distance2 = distance * 100;
  //
  //  byte payload[2];
  //  payload[0] = highByte(distance2);
  //  payload[1] = lowByte(distance2);
  //
  //  ttn.sendBytes(payload, sizeof(payload));
  //


  delay(2000);
}

I pray one of you can help

If your AppEUI and AppKey are private and important, you best not post them here.

Cheers