Oled 1.3" display + DHT11 don't work together

Hello everyone,

I am trying to display the temp and humidity from DHT11 on a OLED display.

The Oled display works fine, I can print some text, but the readings from DHT don't work.
As soon as I activate the function to get the readings from DHT , the display won't work anymore.
If I comment that function then the display will show some text again.
I have tried to move the code where I read temp and humidity in in different places, but without success.
I need help from you to move on with my project.

Thank you in advance for your help!

#include <U8g2lib.h>
#include <Wire.h>
#include "DHT.h"

#define DHTPIN 2  // DATA PIN from  DHT11
#define DHTTYPE DHT11  // DHT 11

// Initialize DHT sensor for normal 16mhz Arduino
DHT dht(DHTPIN, DHTTYPE);

U8G2_SH1106_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, U8X8_PIN_NONE);
const char COPYRIGHT_SYMBOL[] = { 0xa9, '\0' };

#define time_delay 1000
char destHumid[16];
char destTemp[16];
char text[16];
float i = 0.0;
float h = 0.0;
float t = 0.0;

void get_temp_humidity()
{
  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  h = dht.readHumidity();
  // Read temperature as Celsius
  t = dht.readTemperature();  

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

  char buffer_temp[16];
  char buffer_humid[16];  
  
  dtostrf(t,4,1,buffer_temp);  
  //temp = "Temp: " + buffer_temp +" °C ";
  char* temp_text1 = "Temp : ";
  char* temp_text2 = " *C ";

  strcpy( destTemp, temp_text1 );
  strcat( destTemp, buffer_temp );
  strcat( destTemp, temp_text2);  

  dtostrf(h,4,1,buffer_humid);  
  //humid = "Humidity: " + buffer_humid +" RH% ";
  char* humid_text1 = " Humidity: ";
  char* humid_text2 = " RH% ";

  strcpy( destHumid, humid_text1 );
  strcat( destHumid, buffer_humid );
  strcat( destHumid, humid_text2);

}

void u8g2_prepare() {
  u8g2.setFont(u8g2_font_6x10_tf);
  u8g2.setFontRefHeightExtendedText();
  u8g2.setDrawColor(1);
  u8g2.setFontPosTop();
  u8g2.setFontDirection(0);
}

void setup(void) {
  Serial.begin(9600);
  dht.begin();
  u8g2.begin();  
  u8g2_prepare();
}

void loop(void) {  
  //get_temp_humidity();  //read temp and humidity from sensor
  u8g2.clearBuffer();
  u8g2_prepare();
  u8g2.setCursor(0, 0);  
  u8g2.print(i);  //show a live counter 
  i+= 1;  
  u8g2.setCursor(0, 30);
  u8g2.print("Temperature: ");
  u8g2.print(t);
  u8g2.print(" C ");
  u8g2.setCursor(0, 45);
  u8g2.print("Humidity: ");
  u8g2.print(h);
  u8g2.print(" RH%");
  u8g2.sendBuffer();
  delay(time_delay);

}

What does your Output monitor report show when you compile with all devices in the code? OLED takes a lot of memory. Fonts take a lot of memory. To save program memory, find smaller libraries. Also, each Serial.print("x"); can be placed in non-program memory by using this macro Serial.print(F("x"); (also with .println(); ).

This is happening when the function is comment

Sketch uses 13704 bytes (42%) of program storage space. Maximum is 32256 bytes.
Global variables use 1800 bytes (87%) of dynamic memory, leaving 248 bytes for local variables. Maximum is 2048 bytes.

otherwise, if I uncomment that function I get this result

Sketch uses 16674 bytes (51%) of program storage space. Maximum is 32256 bytes.
Global variables use 1862 bytes (90%) of dynamic memory, leaving 186 bytes for local variables. Maximum is 2048 bytes.

Now do the "F" macro change... re-compile and check the report.

A long shot... change the DHT to another pin (not 2 or 3... in case one of the libraries is using hardware interrupt 0 or 1).

Look at the U8g2 examples for a paged buffer, that will take much less ram. A full buffer for that display needs 1024 bytes of ram.

Be careful of the size of destTemp and destHumid, destHumid is already too small to hold

" Humidity:  RH% "

without any space for the humidity itself.

I made the following changes: DHT to digital PIN 8, Serial.print(F("x")); and paged buffer.
Nothing is changed, the program displays some text only if that line where I read the DHT values is commented.

#include <U8g2lib.h>
#include <Wire.h>
#include "DHT.h"

#define DHTPIN 8  // DATA PIN from  DHT11
#define DHTTYPE DHT11  // DHT 11


DHT dht(DHTPIN, DHTTYPE); // Initialize DHT sensor for normal 16mhz Arduino
U8G2_SH1106_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, U8X8_PIN_NONE);

float i = 0.0;
float h = 0.0;
float t = 0.0;

int get_DHT_data()
{
  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  h = dht.readHumidity();
  // Read temperature as Celsius
  t = dht.readTemperature();  

  // Check if any reads failed and exit early (to try again).  
  if (isnan(h) || isnan(t)) {
    Serial.println(F("Failed to read from DHT sensor!"));
    return 0;    
  }
  Serial.println(F("temperature:"));
  Serial.println(t);
  Serial.println(F(" C"));
  Serial.println(F(" Humidity: "));
  Serial.println(h);
  Serial.println(F(" RH%"));

  return 1;
}

void u8g2_prepare() {
  u8g2.setFont(u8g2_font_6x10_tf);
  u8g2.setFontRefHeightExtendedText();
  u8g2.setDrawColor(1);
  u8g2.setFontPosTop();
  u8g2.setFontDirection(0);
}

void setup(void) {
  Serial.begin(9600);
  dht.begin();
  u8g2.begin();  
  u8g2_prepare();
  //int err = get_DHT_data();  //read temp and humidity from sensor
}

void loop(void) {     
  u8g2.firstPage();
  do 
  {   
    u8g2.setCursor(0, 0);  
    u8g2.print(i);  //show a live counter 
    i+= 1;  
    u8g2.setCursor(0, 30);
    u8g2.print("Temperature: ");
    u8g2.print(t);
    u8g2.print(" C ");
    u8g2.setCursor(0, 45);
    u8g2.print("Humidity: ");
    u8g2.print(h);
    u8g2.print(" RH%");
  } 
  while ( u8g2.nextPage() );
  delay(1000);
}

[quote="woosamike, post:6, topic:1313079"]
get_DHT_data
[/quote]

I do not see that call in any "dht.h" library.
Sorry... it is your function call...

I guess you have used the DHT on its own, without loading any OLED library?

Where does the DS3231 RTC fit in this?

U8G2_SH1106_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, U8X8_PIN_NONE);

This is the constructor for the full buffer, try this instead

U8G2_SH1106_128X64_NONAME_1_HW_I2C u8g2(U8G2_R0, U8X8_PIN_NONE);
1 Like

Thank you all for your help. Thanks to you I 've learned new things today!
I have an Arduino Uno R3 board. Is there any other version with higher amount of memory?

My mistake, I wrote the wrong sensor in the title, but I am working on that too... now that I can move forward

Near the bottom of this...
https://docs.arduino.cc/learn/programming/memory-guide/

Sketch uses 15224 bytes (47%) of program storage space. Maximum is 32256 bytes.
Global variables use 880 bytes (42%) of dynamic memory, leaving 1168 bytes for local variables. Maximum is 2048 bytes.

That is half from the full buffer

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