bump : here is some code that I use with a NANO and the 0.96" OLED and U8g2
// Example testing sketch for various DHT humidity/temperature sensors Written by ladyada, public domain
// from the examples in the IDE
#include "DHT.h"
// +++++++++++++++++++++++++++ LIBRARIES ++++++++++++++++++++++++
#include <Arduino.h>
#include <U8g2lib.h>
#ifdef U8X8_HAVE_HW_SPI
#include <SPI.h>
#endif
#ifdef U8X8_HAVE_HW_I2C
#include <Wire.h>
#endif
// ======================== libraries =============
#define DHTPIN 2 // what digital pin we're connected to
// consructors
#define DHTTYPE DHT11 // DHT 11
U8G2_SSD1306_128X64_NONAME_1_HW_I2C u8g2(U8G2_R2, /* reset=*/ U8X8_PIN_NONE);
//U8G2_SSD1306_128X64_NONAME_1_SW_I2C u8g2(U8G2_R0, /* clock=*/ SCL, /* data=*/ SDA, /* reset=*/ U8X8_PIN_NONE); // All Boards without Reset of the Display
// End of constructor list
// Connect pin 1 (on the left) of the sensor to +5V
// NOTE: If using a board with 3.3V logic like an Arduino Due connect pin 1
// to 3.3V instead of 5V!
// Connect pin 2 of the sensor to whatever your DHTPIN is
// Connect pin 4 (on the right) of the sensor to GROUND
// Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor
// Initialize DHT sensor.
// Note that older versions of this library took an optional third parameter to
// tweak the timings for faster processors. This parameter is no longer needed
// as the current DHT reading algorithm adjusts itself to work on faster procs.
DHT dht(DHTPIN, DHTTYPE);
// ++++++++++++++++++++ variables ++++++++++++++++++++++++++++++++++
// global
float h ; // humidity
float tc ; // TEMPERATURE C
float tf ; // Temperature F
void setup() { // ++++++++++++++++++++++++++++++++++ SETUP +++++++++++++++++
Serial.begin(9600);
Serial.println("DHTxx test!");
u8g2.begin(); // display
dht.begin(); // humidity chip DHT11
}
void draw(void) { // ++++++++++++++++++++++++++++++++++++++ DRAW ++++++++++++++++++++++++
u8g2.firstPage();
do {
// u8g2.drawRFrame(0,0,35,12,5); // start x, start y , width, height, rauius
// u8g2.setFont(u8g2_font_6x13B_tr); //smalleer
u8g2.setFont(u8g2_font_ncenB10_tr); //larger
u8g2.drawStr(0, 11, "RH : ");
u8g2.drawStr(0, 26, "T C ");
u8g2.drawStr(0, 40, "T F ");
u8g2.setCursor(48, 11);
u8g2.print(h);
u8g2.setCursor(50, 26);
u8g2.print(tc);
u8g2.setCursor(48, 40);
u8g2.print(tf);
} while ( u8g2.nextPage() );
delay(2000);
}
void loop() { // ++++++++++++++++++++++++++++++++++ LOOP +++++++++++++++++
delay(2000); // Wait a few seconds between measurements.
h = dht.readHumidity(); // Reading temperature or humidity takes about 250 milliseconds!
tc = dht.readTemperature(); // Read temperature as Celsius (the default)
tf = dht.readTemperature(true); // Read temperature as Fahrenheit (isFahrenheit = true)
if (isnan(h) || isnan(tc) || isnan(tf)) { // // Check if any reads failed and exit early (to try again).
Serial.println("Failed to read from DHT sensor!");
return;
}
float hif = dht.computeHeatIndex(tf, h); // Compute heat index in Fahrenheit (the default)
float hic = dht.computeHeatIndex(tc, h, false); // Compute heat index in Celsius (isFahreheit = false)
Serial.print("Humidity: ");
Serial.print(h);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(tc);
Serial.print(" *C ");
Serial.print(tf);
Serial.print(" *F\t");
Serial.print("Heat index: ");
Serial.print(hic);
Serial.print(" *C ");
Serial.print(hif);
Serial.println(" *F");
draw() ;
} // END LOOP