Problem Displaying Temperature and Humedity onto my SSD1306 OLED 128x32 Screen

Hello,

This is my first time posting on this forum and I had a question regarding displaying temperature and humedity into my SSD1306 OLED 128x32 Screen. I don't have no experience programming, since I'm doing my first Arduino project for a college class. The code I used is mostly compiled from YouTube videos I seen and adjust toward my Arduino. Hopefully, you guys can assist for a fellow beginner on his project. It include the information of my name, but not the temperature and humidity.

Here's my code showing my code I have so far:

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

#define DHT11_PIN 2

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels

// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);

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

if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64
Serial.println(F("SSD1306 allocation failed"));
for(;;);
}
delay(600);
display.clearDisplay();

//OLED Display
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(7, 7);
// Display static text
display.println("ARDUINO");
display.println("SMART");
display.println("WATCH");
display.display();
delay(600);

display.println("(C)2019 by");
display.println("Alexander Gomez");
display.display();
delay(600);

display.begin(128, 64);

}

void loop()
{
int chk = DHT.read11(DHT11_PIN);

display.setCursor(7,7);
display.println("Temp: ");
display.println(DHT.temperature);
display.println((char)223);
display.println("C");
display.setCursor(7,7);
display.println("Humidity: ");
display.println(DHT.humidity);
display.println("%");
delay(1000);
}

It helps if you post you Auto Format code in the IDE and use code tags when posting it here so it looks like this

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

#define DHT11_PIN 2

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels

// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);

void setup()
{
  Serial.begin(9600);
  if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C))  // Address 0x3D for 128x64
  {
    Serial.println(F("SSD1306 allocation failed"));
    for (;;);
  }
  delay(600);
  display.clearDisplay();
  //OLED Display
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(7, 7);
  // Display static text
  display.println("ARDUINO");
  display.println("SMART");
  display.println("WATCH");
  display.display();
  delay(600);
  display.println("(C)2019 by");
  display.println("Alexander Gomez");
  display.display();
  delay(600);
  display.begin(128, 64);
}

void loop()
{
  int chk = DHT.read11(DHT11_PIN);
  display.setCursor(7, 7);
  display.println("Temp: ");
  display.println(DHT.temperature);
  display.println((char)223);
  display.println("C");
  display.setCursor(7, 7);
  display.println("Humidity: ");
  display.println(DHT.humidity);
  display.println("%");
  delay(1000);
}

Note that the code does not now have a smiley in it.

Please read read this before posting a programming question

What happens when you run the code ?

Do you think that it might be an idea to do this

 display.begin(128, 64);

before printing to the screen ?

If the display actually displays your name, it means that it is correctly connected and configured. Maybe the problem comes from the DHT sensor.

What is the exact DHT library that you use? The problem is that there are several libraries with the same .h file name... If you installed the Adafruit library 'DHT-sensor-library' then you should try the example files to make it work properly.

The constructor for the DHT sensor is:

#include "DHT.h"
#define DHTPIN 2     // Digital pin connected to the DHT sensor
#define DHTTYPE DHT11   // DHT 11
DHT dht(DHTPIN, DHTTYPE);

I see that you also use DHT.read11(DHT11_PIN) : this is not from the Adafruit library. So we need to know what exact library you installed for this sensor and also what Arduino module you're using...

What happens when you Serial.print the values?

For TheMemberFormelyKnownAsAWOL, when I print out the value, they all comes out as -999 for temperature and humidity. I'm not sure why did happens, but I read that I'm suppose to change the delay to delay(2000), but that doesn't seem to work.

For lesept, I didn't use the DHT-sensor-library, but I'll consider it when I revised my code, Thank you for the response.

For UKHeliBob, when I put "display.begin(128, 64);", this was suppose to show the dimensions of the OLED screen. Also thank you taking away the smiley face, since I didn't know how to do it. In addition, thank for the response as well.

You seem to have used display.begin() twice in the program with different parameters but the main point that I was making is that you printed to the screen before using the function.

Do the example programs with the library work ?

The examples does work, but I can only find to print the values in the Serial Monitor and not in the screen. However, I tried to use the Serial Monitor yesterday and the values printed out "-999). Currently, I found another code online, but found out that when it print onto the screen, it shows "nan%". I don't know what nan% means.

Also what you mean by display.begin() twice. I only see it once. Can you reference so I can see it. For your information, this is my first time using Arduino. Sorry for about not understanding you.

 if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C))  // Address 0x3D for 128x64
  display.begin(128, 64);

At the moment the display is the least of your problems of you can't get values from the sensor and show them on the Serial monitor. nan means that the value is not a number

Start small and forget the display. Do the DHT examples work ? Where did you get the DHT library from ?

int chk = DHT.read11(DHT11_PIN);The first place where I can find this DHT.read11 instruction is here. It references a library from Rob Tillaart (github here) but the dht.h file only has this method (iwth an underscore):

int dht_read11(uint8_t pin);

Another possible source is here : Circuit Basics. The code snippet for reading the sensor seems very close to the one from the OP. The library DHTLib is available here. It might be the good one...

Lesespt, the code of the temperature and humidity came from the Circuit Basics website actually. I used the template to see if I was able to include the temperature readings and my name onto the OLED screen.

UKHelibob, I got the DHT library from the Circuit Basics website Lesespt mentioned above. I have not tested the DHT examples since I had to modify it from the Circuit Basics website to make it work for my OLED screen.

I learned that the dht.h library that I downloaded from the Circuit Basic website doesn't include an example. It only includes the library.

Some time ago I found out that I bought a DS32321 RTC Module. What did device does is tell time and temperature. I have connected today (at the time typing this) and found out that it this device has it's own component to record temperature.I'll be using the DS32321 RTC to actually do my project.

I'm grateful for all the help you guys have done and if I ever need help, I'll be sure to come back. Thank for very much.

I have also gave those who helped some Karma points :slight_smile: