OLED display not predictable. Sometimes fine, sometimes display looks garbled

Greetings,

I just picked up an OLED display that I want to use for a basic temperature/pressure monitor. I have everything hooked up, and have been using U8GLIB to communicate with the display. However, sometimes the text that is displayed looks fine, other times, maybe one or two digits don’t look right.

I don’t think that it’s a problem with the LED display, but maybe there is something that I can do differently with my code to correct this issue.

In addition to my code included below, here is a picture showing an example of how the display text is getting garbled up. In the first picture, everything looks fine. And then with the next reading, the last digit looks messed up. It’s not always the last digit that gets garbled.


#include "U8glib.h"
#include <Wire.h>
#include <SPI.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BMP280.h>

#define BMP_SCK 13
#define BMP_MISO 12
#define BMP_MOSI 11 
#define BMP_CS 10

Adafruit_BMP280 bme(BMP_CS, BMP_MOSI, BMP_MISO,  BMP_SCK);
U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NONE|U8G_I2C_OPT_DEV_0);

void setup() {
  // put your setup code here, to run once:
  ;
  u8g.setColorIndex(1);

  if (!bme.begin()) {  
    u8g.drawStr( 0, 15, "Could not find");
    u8g.drawStr( 0, 30, "sensor, check wiring!"); 
    u8g.drawStr( 0, 45, "wiring!"); 
   while (1);
  }
}

void draw_temp(void){
  
  u8g.setFont(u8g_font_unifont);
  u8g.drawStr( 0, 15, "Temperature:");   

  float temp = bme.readTemperature() * 9/5 + 32;
  u8g.setScale2x2();
  u8g.setPrintPos(0,25);
  u8g.print(temp);
  u8g.undoScale();

}

void draw_pressue(void){
  
  u8g.setFont(u8g_font_unifont);
  u8g.drawStr( 0, 15, "Pressure:");  

  float pres = bme.readPressure() / 100;
  u8g.setScale2x2();
  u8g.setPrintPos(0,25);
  u8g.print(pres);
  u8g.undoScale();
}


void loop(void) {
  // put your main code here, to run repeatedly:  
  u8g.firstPage();
    do {  
      draw_temp();    
    } while( u8g.nextPage() );
    delay(3000); 
    
    u8g.firstPage();
    do {  
      draw_pressue();    
    } while( u8g.nextPage() ); 
   delay(3000); 
}

Part of the problem is that the value is changing in the “picture loop” (see tutorial here). The display is updated in sections to reduce the RAM buffer size required, so what is hapening is the the value has changed between drawing the top and bottom halves of the screen image.

So read the sensor value, then enter the picture loop, so then the value being printed stays constant during the “do” loop.