Problems with u8g2 library and printing sensor values.

Hey everyone, Im trying to use the u8g2 library to print the values of a temperature/humidity sensor to an ssd1306 oled screen. Ive successfully gotten the program to compile and upload, but the screen just displays a random assortment of pixels along the bottom 1/4 or so of the screen.

I am very new to this as well, so I apologize in advance if what Ive done is just a mess of nonsense, or if I have some things that are redundant/unnecessary, Ive just been trying for quite some time now and am not sure what to do next (or what ive really even been doing, honestly). And even if just telling me what ive made is just a mess, thatd still help, thanks!

Both libraries’ reference manuals (including setup/objects and functions) are here:

u8g2: https://github.com/olikraus/u8g2/wiki/u8g2reference
Adafruit Unified Sensor: https://github.com/adafruit/Adafruit_Sensor/blob/master/README.md
DHT temperature/humidty: GitHub - adafruit/DHT-sensor-library: Arduino library for DHT11, DHT22, etc Temperature & Humidity Sensors

// DHT Temperature & Humidity Sensor
// Unified Sensor Library Example
// Written by Tony DiCola for Adafruit Industries
// Released under an MIT license.

// Depends on the following Arduino libraries:
// - Adafruit Unified Sensor Library: https://github.com/adafruit/Adafruit_Sensor
// - DHT Sensor Library: https://github.com/adafruit/DHT-sensor-library

#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <DHT_U.h>
#include <Arduino.h>
#include <U8g2lib.h>

#ifdef U8X8_HAVE_HW_SPI
#include <SPI.h>
#endif
#ifdef U8X8_HAVE_HW_I2C
#include <Wire.h>
#endif

#define DHTPIN            2         // Pin which is connected to the DHT sensor.

// Uncomment the type of sensor in use:
//#define DHTTYPE           DHT11     // DHT 11 
#define DHTTYPE           DHT22     // DHT 22 (AM2302)
//#define DHTTYPE           DHT21     // DHT 21 (AM2301)

// See guide for details on sensor wiring and usage:
//   https://learn.adafruit.com/dht/overview

U8G2_SSD1306_128X64_NONAME_F_4W_SW_SPI u8g2(U8G2_R0, /* clock=*/ 13, /* data=*/ 11, /* cs=*/ 10, /* dc=*/ 9, /* reset=*/ 8);
DHT_Unified dht(DHTPIN, DHTTYPE);

uint32_t delayMS;
void U8G2::sendBuffer(void);
uint8_t *U8G2::getBufferPtr(void);

void setup() {
  u8g2.begin();
  dht.begin();
  u8g2.println("DHTxx Unified Sensor Example");
  // Print temperature sensor details.
  sensor_t sensor;
  dht.temperature().getSensor(&sensor);
  u8g2.println("------------------------------------");
  u8g2.println("Temperature");
  u8g2.print  ("Sensor:       "); u8g2.println(sensor.name);
  u8g2.print  ("Driver Ver:   "); u8g2.println(sensor.version);
  u8g2.print  ("Unique ID:    "); u8g2.println(sensor.sensor_id);
  u8g2.print  ("Max Value:    "); u8g2.print(sensor.max_value); u8g2.println(" *C");
  u8g2.print  ("Min Value:    "); u8g2.print(sensor.min_value); u8g2.println(" *C");
  u8g2.print  ("Resolution:   "); u8g2.print(sensor.resolution); u8g2.println(" *C");  
  u8g2.println("------------------------------------");
  // Print humidity sensor details.
  dht.humidity().getSensor(&sensor);
  u8g2.println("------------------------------------");
  u8g2.println("Humidity");
  u8g2.print  ("Sensor:       "); u8g2.println(sensor.name);
  u8g2.print  ("Driver Ver:   "); u8g2.println(sensor.version);
  u8g2.print  ("Unique ID:    "); u8g2.println(sensor.sensor_id);
  u8g2.print  ("Max Value:    "); u8g2.print(sensor.max_value); u8g2.println("%");
  u8g2.print  ("Min Value:    "); u8g2.print(sensor.min_value); u8g2.println("%");
  u8g2.print  ("Resolution:   "); u8g2.print(sensor.resolution); u8g2.println("%");  
  u8g2.println("------------------------------------");
  // Set delay between sensor readings based on sensor details.
  delayMS = sensor.min_delay / 1000;
}

void loop() {
  u8g2.clearBuffer();
  u8g2.clearDisplay();
  u8g2.home();
  
  {// Delay between measurements.
  delay(delayMS);
  // Get temperature event and print its value.
  drawStr
  sensors_event_t event;  
  dht.temperature().getEvent(&event);
  if (isnan(event.temperature)) {
    u8g2.println("Error reading temperature!");
  }
  else {
    u8g2.print("Temperature: ");
    u8g2.print(event.temperature);
    u8g2.println(" *C");
  }
  // Get humidity event and print its value.
  dht.humidity().getEvent(&event);
  if (isnan(event.relative_humidity)) {
    u8g2.println("Error reading humidity!");
  }
  else {
    u8g2.print("Humidity: ");
    u8g2.print(event.relative_humidity);
    u8g2.println("%");
  }

  u8g2.sendBuffer();          // transfer internal memory to the display
  delay(1000); 
  }
}

Here is an updated code with what progress I have made. The noise pixels are still at the bottom,
but honestly i think its more likely just a shitty par (cuz its just a cheap screen). I have gotten the temperature and humidity to display, but the units are in the several hundreds, and the screen flickers every now and then (and im not sure how often the sensor refreshes), so i think the two timers may have synched up or gotten thrown off somehow.

// DHT Temperature & Humidity Sensor
// Unified Sensor Library Example
// Written by Tony DiCola for Adafruit Industries
// Released under an MIT license.

// Depends on the following Arduino libraries:
// - Adafruit Unified Sensor Library: https://github.com/adafruit/Adafruit_Sensor
// - DHT Sensor Library: https://github.com/adafruit/DHT-sensor-library

#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <DHT_U.h>
#include <Arduino.h>
#include <U8g2lib.h>

#ifdef U8X8_HAVE_HW_SPI
#include <SPI.h>
#endif
#ifdef U8X8_HAVE_HW_I2C
#include <Wire.h>
#endif

#define DHTPIN            2         // Pin which is connected to the DHT sensor.

// Uncomment the type of sensor in use:
//#define DHTTYPE           DHT11     // DHT 11 
#define DHTTYPE           DHT22     // DHT 22 (AM2302)
//#define DHTTYPE           DHT21     // DHT 21 (AM2301)

// See guide for details on sensor wiring and usage:
//   https://learn.adafruit.com/dht/overview

U8G2_SSD1306_128X64_NONAME_F_4W_SW_SPI u8g2(U8G2_R0, /* clock=*/ 13, /* data=*/ 11, /* cs=*/ 10, /* dc=*/ 9, /* reset=*/ 8);
DHT_Unified dht(DHTPIN, DHTTYPE);

uint32_t delayMS;

void setup() {
  u8g2.begin();
  u8g2.enableUTF8Print();
  dht.begin();
  u8g2.println("DHTxx Unified Sensor Example");
  // Print temperature sensor details.
  sensor_t sensor;
  dht.temperature().getSensor(&sensor);
  u8g2.println("------------------------------------");
  u8g2.println("Temperature");
  u8g2.print  ("Sensor:       "); u8g2.println(sensor.name);
  u8g2.print  ("Driver Ver:   "); u8g2.println(sensor.version);
  u8g2.print  ("Unique ID:    "); u8g2.println(sensor.sensor_id);
  u8g2.print  ("Max Value:    "); u8g2.print(sensor.max_value); u8g2.println(" *C");
  u8g2.print  ("Min Value:    "); u8g2.print(sensor.min_value); u8g2.println(" *C");
  u8g2.print  ("Resolution:   "); u8g2.print(sensor.resolution); u8g2.println(" *C");  
  u8g2.println("------------------------------------");
  // Print humidity sensor details.
  dht.humidity().getSensor(&sensor);
  u8g2.println("------------------------------------");
  u8g2.println("Humidity");
  u8g2.print  ("Sensor:       "); u8g2.println(sensor.name);
  u8g2.print  ("Driver Ver:   "); u8g2.println(sensor.version);
  u8g2.print  ("Unique ID:    "); u8g2.println(sensor.sensor_id);
  u8g2.print  ("Max Value:    "); u8g2.print(sensor.max_value); u8g2.println("%");
  u8g2.print  ("Min Value:    "); u8g2.print(sensor.min_value); u8g2.println("%");
  u8g2.print  ("Resolution:   "); u8g2.print(sensor.resolution); u8g2.println("%");  
  u8g2.println("------------------------------------");
  // Set delay between sensor readings based on sensor details.
  delayMS = sensor.min_delay / 1000;
}

void loop() {
  // Delay between measurements.
  delay(delayMS);
  u8g2.clearBuffer();
  u8g2.clearDisplay();
  u8g2.home();
  u8g2.setFont(u8g2_font_u8glib_4_hr);
  u8g2.setCursor(0,15);
    {// Get temperature event and print its value.
    sensors_event_t event;  
    dht.temperature().getEvent(&event);
    if (isnan(event.temperature)) {
      u8g2.println("Error reading temperature!");
    }
    else {
      u8g2.print("Temperature: ");
      u8g2.print(event.temperature);
      u8g2.println(" *C");
    }
    // Get humidity event and print its value.
    dht.humidity().getEvent(&event);
    if (isnan(event.relative_humidity)) {
      u8g2.println("Error reading humidity!");
    }
    else {
      u8g2.print("Humidity: ");
      u8g2.print(event.relative_humidity);
      u8g2.println("%");
    }  
  } 
  u8g2.sendBuffer();      // transfer internal memory to the display
  delay(1000);
}

Well, Ive at least figured out that the problem has to do with timing between the two programs/libraries. Working on a solution, though

Okay, I found the issues… first, somehow between the time I put the project down and picked it up again, I mistook which sensor I was using. Now that I have the right one selected, the readings are correct.

However, the screen still blinks intermittently. Ive seen all the beginner/novice how-to’s on replacing the delays with flow control, but Ive been having issues with that.

This program successfully compiles/uploads, but does nothing. I tried to replace the screen delay() with a time interval using millis(), because the sensor delay is dependent on the sensor readings themselves.

Can anyone help point out where Im going wrong? Thanks!

// DHT Temperature & Humidity Sensor
// Unified Sensor Library Example
// Written by Tony DiCola for Adafruit Industries
// Released under an MIT license.

// Depends on the following Arduino libraries:
// - Adafruit Unified Sensor Library: https://github.com/adafruit/Adafruit_Sensor
// - DHT Sensor Library: https://github.com/adafruit/DHT-sensor-library


#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <DHT_U.h>
#include <Arduino.h>
#include <U8g2lib.h>

#ifdef U8X8_HAVE_HW_SPI
#include <SPI.h>
#endif
#ifdef U8X8_HAVE_HW_I2C
#include <Wire.h>
#endif

#define DHTPIN            2         // Pin which is connected to the DHT sensor.

// Uncomment the type of sensor in use:
#define DHTTYPE           DHT11     // DHT 11 
//#define DHTTYPE           DHT22     // DHT 22 (AM2302)
//#define DHTTYPE           DHT21     // DHT 21 (AM2301)

// See guide for details on sensor wiring and usage:
//   https://learn.adafruit.com/dht/overview

U8G2_SSD1306_128X64_NONAME_F_4W_SW_SPI u8g2(U8G2_R0, /* clock=*/ 13, /* data=*/ 11, /* cs=*/ 10, /* dc=*/ 9, /* reset=*/ 8);
DHT_Unified dht(DHTPIN, DHTTYPE);

unsigned long delayMS;
// constants won't change:
const long interval = 1000;           // interval at which to buffer (milliseconds)
bool Buffer=false;
unsigned long currentMillis = millis();
unsigned long previousMillis = millis();

void setup() {
  u8g2.begin();
  dht.begin();
  u8g2.enableUTF8Print();
  u8g2.println("DHTxx Unified Sensor Example");
  // Print temperature sensor details.
  sensor_t sensor;
  dht.temperature().getSensor(&sensor);
  u8g2.println("------------------------------------");
  u8g2.println("Temperature");
  u8g2.print  ("Sensor:       "); u8g2.println(sensor.name);
  u8g2.print  ("Driver Ver:   "); u8g2.println(sensor.version);
  u8g2.print  ("Unique ID:    "); u8g2.println(sensor.sensor_id);
  u8g2.print  ("Max Value:    "); u8g2.print(sensor.max_value); u8g2.println(" *C");
  u8g2.print  ("Min Value:    "); u8g2.print(sensor.min_value); u8g2.println(" *C");
  u8g2.print  ("Resolution:   "); u8g2.print(sensor.resolution); u8g2.println(" *C");  
  u8g2.println("------------------------------------");
  // Print humidity sensor details.
  dht.humidity().getSensor(&sensor);
  u8g2.println("------------------------------------");
  u8g2.println("Humidity");
  u8g2.print  ("Sensor:       "); u8g2.println(sensor.name);
  u8g2.print  ("Driver Ver:   "); u8g2.println(sensor.version);
  u8g2.print  ("Unique ID:    "); u8g2.println(sensor.sensor_id);
  u8g2.print  ("Max Value:    "); u8g2.print(sensor.max_value); u8g2.println("%");
  u8g2.print  ("Min Value:    "); u8g2.print(sensor.min_value); u8g2.println("%");
  u8g2.print  ("Resolution:   "); u8g2.print(sensor.resolution); u8g2.println("%");  
  u8g2.println("------------------------------------");
    // Set delay between sensor readings based on sensor details.
 delayMS = sensor.min_delay / 1000;
}

void loop(){
     if (currentMillis - previousMillis >= interval, Buffer==true){
       // save the last time
       previousMillis = currentMillis;
       digitalWrite(Buffer, false);}
     else if (Buffer==false){
        digitalWrite(Buffer, true);
        delay(delayMS);
        // Get temperature event and print its value.
        sensors_event_t event;  
        dht.temperature().getEvent(&event);
        if(isnan(event.temperature)) {
        u8g2.println("Error reading temperature!");
        }
        else {
        u8g2.print("Temperature: ");
        u8g2.print(event.temperature);
        u8g2.println(" *C");
        }
        // Get humidity event and print its value.
        dht.humidity().getEvent(&event);
        if (isnan(event.relative_humidity)) {
        u8g2.println("Error reading humidity!");
        }
        else {
        u8g2.print("Humidity: ");
        u8g2.print(event.relative_humidity);
        u8g2.println("%");
        }
        digitalWrite(Buffer, true);
     }
  while(Buffer==true){u8g2.sendBuffer();      // transfer internal memory to the display}
  }
}

Also, just to clarify, I tried to use the if/else to check the time interval and set/read the state of the boolean Buffer, to determine if the appropriate interval had passed.

Any pointers?

Im thinking its probably just getting locked up between the conditional statement and how I set my bool to true and/or false, and I may be overly complicating it there. Just gonna step back again and try to reevaluate the flow control and see if I cant come up with something better. In the meantime, if anyone can simplify my mistake(s) for me, thatd be much appreciated, and sure to save me hours of time. Thanks.

Alright, I'm just stuck, and been turning this in my brain to mush. Would anyone help me? Sorry for asking