Adafruit SSD1306 & DHT Sensor libraries, any know issues?

I've experiencing issues reading and display humidity and temperature on an i2c OLED using the Adafruit libraries, are there any know issues with these two libraries working together?

I initially started with the SSD1306 example and have this working perfectly and then developed the sketch to include the DHT sensor at which point I'm only seeing the adafruit logo. If I set f, t & h to 0 I see results on the display but anything that attempts to read the DHT11 fails:

float h = dht.readHumidity();

I have completely disconnected the DHT11 and would assume that the 'Check if any reads failed' would be true but no?

Any ideas or suggestions welcomed.

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

// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET     4 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

// Declaration DHT11 sensor
#define DHTPIN 2
#define DHTTYPE DHT11   // DHT 11
DHT dht(DHTPIN, DHTTYPE);

void setup() {
  Serial.begin(9600);

  // Setup sensor:
  dht.begin();

  // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
  if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3C for 128x32
    Serial.println(F("SSD1306 allocation failed"));
    for(;;); // Don't proceed, loop forever
  }

  // Show initial display buffer contents on the screen --
  // the library initializes this with an Adafruit splash screen.
  display.display();
  delay(2000);
}

void loop() {

  // Clear the buffer
  display.clearDisplay();
  display.setTextSize(1); // Draw 2X-scale text
  display.setTextColor(SSD1306_WHITE);
  
  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  // Read the humidity in %:
  float h = dht.readHumidity();
  // float h = 0;

  // Read the temperature as Celsius:
   float t = dht.readTemperature();
  // float t = 0;

  // Read the temperature as Fahrenheit:
  float f = dht.readTemperature(true);
  //float f = 0;

  // Check if any reads failed and exit early (to try again):
  if (isnan(h) || isnan(t) || isnan(f)) {
    display.print("Failed to read from DHT sensor!");
    display.display();
    return;
  }

  // Compute heat index in Fahrenheit (default):
  float hif = dht.computeHeatIndex(f, h);

  // Compute heat index in Celsius:
  float hic = dht.computeHeatIndex(t, h, false);

  display.setCursor(10, 0);
  display.print("Humidity: ");
  display.print(h);
  display.print(" % ");

  display.setCursor(10, 10);
  display.print("Temperature: ");
  display.print(t);
  display.print("C | ");
  display.print(f);
  display.print("F ");

  display.setCursor(10, 20);
  display.print("Heat index: ");
  display.print(hic);
  display.print("C | ");
  display.print(hif);
  display.println("F");

  display.display();
  delay(1000);  
}

Why do users choose to ignore the advice on posting code ?

The easier you make it to read and copy your code the more likely it is that you will get help

Please follow the advice given in the link below when posting code , use code tags and post the code here

Sorry I wasn't aware of this and corrected my post, thanks for highlighting the issue.

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