can't display Rh from DHT22 on OLED using U8glib - SOLVED

Good day.

I am trying to display the Rh from a DHT22 sensor on my Sainsmart 0.96 inch OLED using the u8glib.

I can get all the examples to run on this display. My problem is that I am trying to use a global variable to store the DHT22 sensor results and then display that information on the OLED. I know the OLED works and I am getting Rh and Temp data via SerialPrint.

I suspect I am not declaring and or using the global variable correctly.

I would greatly appreciate some help in debugging this. Thank you

Here is my current test sketch.

#include "DHT.h"
#include "U8glib.h"
#define DHTPIN 2     // what pin we're connected to
#define DHTTYPE DHT22   // DHT 22  (AM2302)
DHT dht(DHTPIN, DHTTYPE);

U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NONE | U8G_I2C_OPT_DEV_0);  // I2C / TWI

float h;
float t;
float f;



void draw(void) {
  // graphic commands to redraw the complete screen should be placed here
  u8g.setFont(u8g_font_6x12);
  u8g.setPrintPos(0, 8);
  u8g.print("Rh is ");
  u8g.print(h); u8g.print(" %");
}

void setup() {
  Serial.begin(9600);
  Serial.println("DHTxx test!");

  if ( u8g.getMode() == U8G_MODE_R3G3B2 ) {
    u8g.setColorIndex(255);     // white
  }
  else if ( u8g.getMode() == U8G_MODE_GRAY2BIT ) {
    u8g.setColorIndex(3);         // max intensity
  }
  else if ( u8g.getMode() == U8G_MODE_BW ) {
    u8g.setColorIndex(1);         // pixel on
  }
  else if ( u8g.getMode() == U8G_MODE_HICOLOR ) {
    u8g.setHiColorByRGB(255, 255, 255);
  }

  dht.begin();
}

void loop() {
  // Wait a few seconds between measurements.
  delay(2000);

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

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

  }
  { // picture loop
    u8g.firstPage();
    do {
      draw();
    } while ( u8g.nextPage() );
  }

  // Compute heat index
  // Must send in temp in Fahrenheit!
  float hi = dht.computeHeatIndex(f, h);

  Serial.print("Humidity: ");
  Serial.print(h);
  Serial.print(" %\t");
  Serial.print("Temperature: ");
  Serial.print(t);
  Serial.print(" *C ");
  Serial.print(f);
  Serial.print(" *F\t");
  Serial.print("Heat index: ");
  Serial.print(hi);
  Serial.println(" *F");



}

Question: did you try to print a normal float on the screen e.g.

u8g.print(3.14159265);

does that work?

Hi Bob. Thank you for your reply.

I should also has said that that sketch would print on the display "Rh is 0.00 %". But instead of the current Rh % it would only print "0.00".

Mark MacKenzie Alcalde, NM

It was my lack of understanding of how to use a variable in this usage.

Here is the code that now works as intended:

#include "DHT.h"
#include "U8glib.h"
#define DHTPIN 2     // what pin we're connected to
#define DHTTYPE DHT22   // DHT 22  (AM2302)
DHT dht(DHTPIN, DHTTYPE);

U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NONE | U8G_I2C_OPT_DEV_0);  // I2C / TWI





void draw(void) {
  //  // graphic commands to redraw the complete screen should be placed here
  float h = dht.readHumidity();
  // Read temperature as Celsius
  float t = dht.readTemperature();
  // Read temperature as Fahrenheit
  float f = dht.readTemperature(true);

  u8g.setFont(u8g_font_6x12);
  u8g.setPrintPos(0, 8);
  u8g.print("Rh is "); u8g.print(h); u8g.print(" %");
}

void setup() {
  Serial.begin(9600);
  Serial.println("DHTxx test!");

  if ( u8g.getMode() == U8G_MODE_R3G3B2 ) {
    u8g.setColorIndex(255);     // white
  }
  else if ( u8g.getMode() == U8G_MODE_GRAY2BIT ) {
    u8g.setColorIndex(3);         // max intensity
  }
  else if ( u8g.getMode() == U8G_MODE_BW ) {
    u8g.setColorIndex(1);         // pixel on
  }
  else if ( u8g.getMode() == U8G_MODE_HICOLOR ) {
    u8g.setHiColorByRGB(255, 255, 255);
  }

  dht.begin();
}

void loop() {
  // Wait a few seconds between measurements.
  delay(2000);

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

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

  }
  // Compute heat index
  // Must send in temp in Fahrenheit!
  float hi = dht.computeHeatIndex(f, h);

  Serial.print("Humidity: ");
  Serial.print(h);
  Serial.print(" %\t");
  Serial.print("Temperature: ");
  Serial.print(t);
  Serial.print(" *C ");
  Serial.print(f);
  Serial.print(" *F\t");
  Serial.print("Heat index: ");
  Serial.print(hi);
  Serial.println(" *F");

  { // picture loop
    u8g.firstPage();
    do {
      draw();
    } while ( u8g.nextPage() );
  }

}