DHT11 temperature/humidity sensor with 0.96'' OLED screen

Hey everyone, new to arduinos here and working on my first project. Trying to connect a DHT11 temperature/humidity sensor to a 0.96" OLED screen and had a few questions. So far, Ive connected the DHT11 sensor to my Uno using the Adafruit unified sensor library example, and have successfully gotten it to readout to the serial monitor. I have also (separately) connected the OLED screen to both the u8g2 and SS1306 libraries and run both example sketches (and currently have the wiring to the SSD1306 configuration) but now im not sure how i should proceed in displaying the one to the other. Ive also seen the pinned post about running multiple sketches, but would it have something to do with that, or would i have to write something new entirely? Im new to C++ as well, but have been teaching myself and have a fairly good grip of it, but have a more conceptual than practical understanding so far (as far as looking at code and somewhat understanding it, but not writing anything myself yet). Thanks!

Also, not sure if it's worth mentioning, but my screen is currently connected via SPI, not i2c as well. Thanks!

Hi,
Can you post your code and post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Even if it is both of your working codes ,we can advise on how to combine them.

Thanks Tom.. :slight_smile:

Sorry, gonna have to do it in pieces because of the max character limitations of the forum. And sorry, I tried to draw up a (crude) circuit, but it turned into a bit of a mess and i messed up, didnt wanna make it any messier. Can still have another go at it if itd help, though.

But for the OLED, I have 7 pins, plugged into the arduino in their respective places as follows:

OLED UNO

VCC 3.3V
GND GND
D0 D10
D1 D9
DC D11
CS D12
RES D13

and my DHT11 is as follows:

DHT11 UNO

VCC 5V
DATA D2
GND GND

Okay nevermind, sorry, the header and .cpp files are way too long to try to post up here, even in pieces. Did you want those or just the two example programs I ran (just a graphics test and a temp/humidity program)?

Here we go, sorry for so many files/posts.

DHT.txt (8.35 KB)

DHT.h (1.53 KB)

DHT_U.cpp (5.61 KB)

DHT_U.h (2.24 KB)

DHT_Unified_Sensor.ino (3.1 KB)

Adafruit_SSD1306.h (5.63 KB)

Adafruit_SSD1306.cpp (24.2 KB)

ssd1306_128x64_spi.ino (9.44 KB)

Sorry, not sure how I overlooked the obvious, but a quick Google search of the two gave me some tutorials, will try that and post here

Okay, Ive found two sketches that are along the same lines of what Im trying to do, but neither will work for me. One is because its set up for i2c and Im using spi (the DHT11 with OLED display program) and the other just returns "error compiling for board Arduino/Genuino Uno (the file with the long string of characters as its name).

The first was found here: GitHub - rydepier/DHT11-with-OLED-Display: Arduino with Humidity and Temperature sensor DHT11, uses OLED Display.
And the second here: GitHub - rydepier/DHT11-with-OLED-Display: Arduino with Humidity and Temperature sensor DHT11, uses OLED Display.

DHT11withOLEDDisplay.ino (3.65 KB)

FA2UEOYIJUCS21Q.ino (1.21 KB)

Edit: second one here: http://www.instructables.com/id/Arduino-OLED-Thermometer-and-Hygrometer-With-DHT11/

Humble bump

Rather than just bump, why don't you update us on your progress?

AWOL:
Rather than just bump, why don't you update us on your progress?

AWOL:
Rather than just bump, why don't you update us on your progress?

Everything in here is as far as my progress has gone. Not sure what to do from here

the problem is in your draw statement.

// now display Humidity
u8g.setFont(u8g_font_profont29);
thisHumidity = String(humidity) + "%";
const char* newHumidity = (const char*) thisHumidity.c_str();
u8g.drawStr(15,50, newHumidity);
u8g.setFont(u8g_font_profont12);
u8g.drawStr(65,38, "humidity"); /// this posts the word humidity, not a variable.

"humidity" is the ASCII letters humidity , just the word.

humidity = dht11_humidity ; /// loads an analog input into the variable humidity.

I believe you need to use the print function

ug8.setcursor(65,38); // sets the cursor location
u8g.print(humidity) ; // prints the variable in parentheses.

I am at work, can post the exact code tonite.
it uses the Arduino print command.

if you cannot wait, see the tutorials on youtube by Jullian. I think the code is in tutorial #2.

sorry for no help, but until I can get home and get my code, I am useless.

alternatively, you can try the
ug8. drawStr(65,38,humidity) ;

but you did not say that your attempt at newHumidity worked.......

[edited post to fix grammar and spelling]

here is my function

op's just realized I am using u8g2 .

void draw(void) {
 u8g2.firstPage();
 do {
   u8g2.setFont(u8g2_font_ncenB10_tr); //larger
   u8g2.drawStr(5, 18, "Temp : ");
   u8g2.drawStr(5, 33, "Humid : ");
   u8g2.drawStr(5, 48, "Pres :");
   u8g2.setCursor(55, 18);
   u8g2.print(T1);
   u8g2.setCursor(55, 33);
   u8g2.print(RH1);
   u8g2.setCursor(55, 48);
   u8g2.print(PR1);
 } while ( u8g2.nextPage() );
 delay(2000);
}

There are other ways to change your data into text.

one is concatenate. this is just taking two things and making it one string.

another if the printf, similar to concatenate

there is also the dtostrf() function.
this takes your numerical values, float or whatever and turns it into a string.
then you can add strings together.

int potValue = analogRead(pinSensor);
float volts = 5*(float)potValue/1023;
char tmp[25] = "Pot Voltage = "; // creates 25 character wide array and loads text into the first area
dtostrf(volts,1,2, &tmp[12]); // play with [12] to get positioning
Serial.println(tmp);   // prints the new line

Hmm, thanks for all the info/help! Still a few questions though. For your first and second replies, was that in the "DHT11withOLEDDisplay", or the "FA2..." program? I found something comparable in both, but closer in the second program. I also have both the U8g and U8g2 libraries installed, and I know support for the the first library is no longer available (as the U8g2 library was how i got the test sketch working that way as well, and how i have my cables hooked up now), but since both programs are the first library, would this take me going in and just changing everything to refer to the second library?

Also, the first program is I2C, but im not sure what the second is. I have mine set up to SPI. Would this affect anything in what youre talking about?

And last, did you mean doing that in a new program, or as an addition to either of the others? Thanks again!

If you have

ug8.whatever

you can just change by adding the 2, be sure to match the way it is worded on any of your other sketched.
fonts do matter. I do not believe that they are all supported back and forth between versions.

ug2.whatever

but everywhere, in the initial above setup
in setup and in every other location.

I would recomennd you open hellow world
and post my code snippet
then add your sensors and put in the variavbles as you need.

just one such as temperature would prove it out.

========

#2 - the connection, I2c, SPI, whaterver is not relavant. the software that does the draw or print does not care.
but you need to make sure your constructor matches and you have any wire libraries, and such.

============

#3, is my response #1

if you still have issues, I would recomend you post your code with the u8g2 lib
and tonite, I could try it out. I have a DH11 so that would make it simple .

Great, thanks! Will try tonight and update soon. Also, sorry for my inexperience, still quite new at this, but appreciate the help!

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

Here is my completed code, which i wrote myself from the ground up. Had to change the control structure to account for conflicting delays, then just did everything else using the u8g2 library functions as outlined in olikraus' wonderful reference manual:

// DHT Temperature & Humidity Sensor
// Unified Sensor Library Example
// Written by Tony DiCola for Adafruit Industries
// Released under an MIT license.

// Depends on the following Arduino libraries:
// - Adafruit Unified Sensor Library: https://github.com/adafruit/Adafruit_Sensor
// - DHT Sensor Library: https://github.com/adafruit/DHT-sensor-library


#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <DHT_U.h>
#include <Arduino.h>
#include <U8g2lib.h>

#ifdef U8X8_HAVE_HW_SPI
#include <SPI.h>
#endif
#ifdef U8X8_HAVE_HW_I2C
#include <Wire.h>
#endif

#define DHTPIN            2         // Pin which is connected to the DHT sensor.

// Uncomment the type of sensor in use:
#define DHTTYPE           DHT11     // DHT 11 
//#define DHTTYPE           DHT22     // DHT 22 (AM2302)
//#define DHTTYPE           DHT21     // DHT 21 (AM2301)

// See guide for details on sensor wiring and usage:
//   https://learn.adafruit.com/dht/overview

U8G2_SSD1306_128X64_NONAME_F_4W_SW_SPI u8g2(U8G2_R0, /* clock=*/ 13, /* data=*/ 11, /* cs=*/ 10, /* dc=*/ 9, /* reset=*/ 8);
DHT_Unified dht(DHTPIN, DHTTYPE);

const long screenInterval = 1000;           // interval at which to buffer (milliseconds)
unsigned long sensorInterval;
unsigned long previousMillis;

void setup() {
  u8g2.begin();
  dht.begin();
  u8g2.enableUTF8Print();
  u8g2.println("DHTxx Unified Sensor Example");
  // Print temperature sensor details.
  sensor_t sensor;
  dht.temperature().getSensor(&sensor);
  u8g2.println("------------------------------------");
  u8g2.println("Temperature");
  u8g2.print  ("Sensor:       "); u8g2.println(sensor.name);
  u8g2.print  ("Driver Ver:   "); u8g2.println(sensor.version);
  u8g2.print  ("Unique ID:    "); u8g2.println(sensor.sensor_id);
  u8g2.print  ("Max Value:    "); u8g2.print(sensor.max_value); u8g2.println(" *C");
  u8g2.print  ("Min Value:    "); u8g2.print(sensor.min_value); u8g2.println(" *C");
  u8g2.print  ("Resolution:   "); u8g2.print(sensor.resolution); u8g2.println(" *C");  
  u8g2.println("------------------------------------");
  // Print humidity sensor details.
  dht.humidity().getSensor(&sensor);
  u8g2.println("------------------------------------");
  u8g2.println("Humidity");
  u8g2.print  ("Sensor:       "); u8g2.println(sensor.name);
  u8g2.print  ("Driver Ver:   "); u8g2.println(sensor.version);
  u8g2.print  ("Unique ID:    "); u8g2.println(sensor.sensor_id);
  u8g2.print  ("Max Value:    "); u8g2.print(sensor.max_value); u8g2.println("%");
  u8g2.print  ("Min Value:    "); u8g2.print(sensor.min_value); u8g2.println("%");
  u8g2.print  ("Resolution:   "); u8g2.print(sensor.resolution); u8g2.println("%");  
  u8g2.println("------------------------------------");
    // Set delay between sensor readings based on sensor details.
 sensorInterval = sensor.min_delay / 1000;
}

void loop() {
  for(static unsigned long previousMillis=millis();
      millis()-previousMillis>=sensorInterval;
      previousMillis=millis()){
        u8g2.clearBuffer();
        // Get temperature event and print its value.
        sensors_event_t event;  
        dht.temperature().getEvent(&event);
        if(isnan(event.temperature)) {u8g2.setFont(u8g2_font_5x7_tf);
           u8g2.setCursor(0,15);
           u8g2.println("Error reading temperature!");
           }
           else {u8g2.setFont(u8g2_font_5x7_tf);
           u8g2.setCursor(0,15);
           u8g2.print("Temperature: ");
           u8g2.print(event.temperature);
           u8g2.println(" *C");
           }
           // Get humidity event and print its value.
           dht.humidity().getEvent(&event);
           if (isnan(event.relative_humidity)) {u8g2.setFont(u8g2_font_5x7_tf);
           u8g2.setCursor(0,25);
           u8g2.println("Error reading humidity!");
           }
           else {u8g2.setFont(u8g2_font_5x7_tf);
           u8g2.setCursor(0,25);
           u8g2.print("Humidity: ");
           u8g2.print(event.relative_humidity);
           u8g2.println("%");
           }
  }

  for(static unsigned long previousMillis=millis();
      millis()-previousMillis>=screenInterval/4;
      previousMillis=millis()){u8g2.sendBuffer();} 
}