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(); ).
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.
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);
}
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?
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.