OLED I2C +DHT22 + Arduino Pro Mini

Hello !

This is my code what I wrote for I2C OLED display and DHT22 temperature and humidity sensor. This project I am running with Arduino Pro Mini.

#include <U8glib.h> //https://github.com/olikraus/u8glib/wiki
#include <dht.h> //http://playground.arduino.cc/Main/DHTLib

U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NO_ACK);

# define dataPin 8

dht DHT;

void setup() {

}
void loop() {

  u8g.firstPage();
  do {
    draw();
  } while ( u8g.nextPage() );
  delay(1); 
}


void draw() {

  int readData = DHT.read22(dataPin);

  float t = DHT.temperature;
  float h = DHT.humidity;

  u8g.setFont(u8g_font_fub25); //https://github.com/olikraus/u8glib/wiki/fontsize
  u8g.setPrintPos(10, 32);
  u8g.print(t, 1);
  u8g.setFont(u8g_font_fub25);
  u8g.setPrintPos(10, 64);
  u8g.print(h, 1);
  u8g.setFont(u8g_font_fub25);
  u8g.drawStr(95, 32, "C");
  u8g.setFont(u8g_font_ncenB12);
  u8g.drawStr(85, 10, "o");
  u8g.setFont(u8g_font_fub25);
  u8g.drawStr(85, 64, "%");

}

I have a problem with my oled display (watch video :wink: )!

It worked very good and without problems when I tested it for u8glib examples such as Rotation, GraphicsTest and with others.

But now, display can’t change values so quickly and I can see fault simbols. If I added some delay time, display worked really slow and change values with long delay time. And print fault simbols !

So what I can do? I think, problem is in code. Maybe someone have experience with this?

Thanks !

The problem is that the sensor is being read in the display draw loop.

The display is drawn in sections from top to bottom and the sensor value is changing before the update of the display image is completed, hence you may get say the top half of a “1” and the bottom half of a “2” displayed. Also the reading of the sensor is quite slow process, this is not normally a problem as the temperature and humidity typically do not change fast.

So read the sensors outside of the draw loop. This is a simple way of doing this:

#include <U8glib.h> //https://github.com/olikraus/u8glib/wiki
#include <dht.h> //http://playground.arduino.cc/Main/DHTLib

U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NO_ACK);

# define dataPin 8

dht DHT;

  float t = 0;
  float h = 0;

void setup() {

}
void loop() {

  int readData = DHT.read22(dataPin);

  t = DHT.temperature;
  h = DHT.humidity;

  u8g.firstPage();
  do {
    draw();
  } while ( u8g.nextPage() );
  delay(1);
}


void draw() {

  u8g.setFont(u8g_font_fub25); //https://github.com/olikraus/u8glib/wiki/fontsize
  u8g.setPrintPos(10, 32);
  u8g.print(t, 1);
  u8g.setFont(u8g_font_fub25);
  u8g.setPrintPos(10, 64);
  u8g.print(h, 1);
  u8g.setFont(u8g_font_fub25);
  u8g.drawStr(95, 32, "C");
  u8g.setFont(u8g_font_ncenB12);
  u8g.drawStr(85, 10, "o");
  u8g.setFont(u8g_font_fub25);
  u8g.drawStr(85, 64, "%");

}

Thank you bodmer ! You are the beast :wink: Now everything works great ! 8)

OK, great to hear you are happy! I hope you meant "best" :slight_smile:

Good luck with your project.

bodmer:
OK, great to hear you are happy! I hope you meant "best" :slight_smile:

Good luck with your project.

I meant beast and if you like best! :wink: :smiley:

But thank you anyway.