U8G2 Black SSD1306 OLED Display (Worked earlier)

Hi,

This is a problem that I expected the least, and I refrained as much I could not to bother the professionals here at the forum. But as unforeseen as it may seem, a code that ran well (of course with the help of forum members), compiles well, uploads successfully but does not show anything on the OLED.

The Code below functions to display GPS speed (more details here) and it had been working well on one of the setups. Then I needed to replicate it and odds played against it. I could only get to work the sample codes built into the U8G2 example library. I do not know why something that hasn’t altered has refused to function at all!

//  PICK ONE of these serial ports:

// BEST: AltSoftSerial on pins 8 & 9 (on an UNO)
//#include <AltSoftSerial.h>
//static AltSoftSerial gpsPort;

static const int RXPin = 4, TXPin = 3;

// 2nd BEST: NeoSWSerial on any two pins
#include <NeoSWSerial.h>
static NeoSWSerial gpsPort(RXPin, TXPin);
static const int GPSBaud = 9600;

// WORST: SoftwareSerial NOT RECOMMENDED

static const bool useOLED = true;

#include <U8g2lib.h>
#define OLED_RESET 5 // Should not be RXPin!

U8G2_SSD1306_128X64_NONAME_1_HW_I2C u8g2( U8G2_R2, U8X8_PIN_NONE ); // 180 degree rotation
static const int timezonehr = +5; //Timezone hour offset
static const int timezonemn = 30; //Timezone minute offset

#include <NMEAGPS.h>
static NMEAGPS         gps;
static gps_fix         fix;
static NeoGPS::time_t  localTime;

// Display: This section will contain elements for the OLED
static void draw(void) {
  // graphic commands to redraw the complete screen should be placed here


  //Separator (Vertical Line)
  u8g2.drawLine(68,27,68,64);


  //GPS:: Checks if GPS has valid fix, then display selective elements
  if (fix.valid.location) {
    u8g2.drawCircle(122,5,5, U8G2_DRAW_ALL); // GPS fix circle Top Right
    u8g2.drawDisc(122,5,3, U8G2_DRAW_ALL); 
  } else {
    u8g2.drawCircle(122,5,5, U8G2_DRAW_ALL);
  }
  u8g2.setFont(u8g2_font_7x13_tr);
  u8g2.setCursor(72, 9);
  u8g2.print( F(" SAT:") );
  if (fix.valid.satellites)
    u8g2.print( fix.satellites );
  

  if (fix.valid.time) {
    // TIME:: Time Elements including conversion of UTC to Local (IST +5.30)
    u8g2.setFont(u8g2_font_crox4h_tn); // instead of profont22r?
    u8g2.setCursor(0, 14);

    if (localTime.hours < 10)
      u8g2.print( '0' );
    u8g2.print( localTime.hours );
    u8g2.print( ':' );
    if (localTime.minutes < 10)
      u8g2.print( '0' );
    u8g2.print( localTime.minutes );
    u8g2.print( ' ' );
    u8g2.setFont(u8g2_font_7x13_tr); 
    if (localTime.seconds < 10)
      u8g2.print( '0' ); u8g2.print( localTime.seconds );
  }
    
  u8g2.setCursor(44, 11);

  if (fix.valid.speed) {
    //SPEED:: Displays Speed and other related graphics
    float speed = fix.speed_kph();
    u8g2.drawFrame(2,59,48,4);
    u8g2.drawBox(2,59,(speed/2.5),4);
    
    if ( (int)speed  > 100)
    {
      u8g2.drawFrame(0,22,64,35);
      u8g2.setFont(u8g2_font_logisoso30_tn);
      u8g2.setCursor(0, 54);
      u8g2.print( (int)speed );
    }
    else
    {
      u8g2.setFont(u8g2_font_logisoso30_tn);
      u8g2.setCursor(0, 56);
      u8g2.print( (int)speed );
    }
    u8g2.setColorIndex(1);
    
    //u8g2.setFont(u8g_font_orgv01r);
    u8g2.setCursor(0, 22);
    //u8g2.print("SPEED >>");
    //u8g2.setFont(u8g_font_fur30n);
    u8g2.setCursor(0, 56);
    u8g2.print( (int)speed ); 
    u8g2.setFont(u8g2_font_5x7_mr);
    u8g2.setCursor(53, 64);
    u8g2.print(F("KM"));
  }  

  //GPS DATA:: Other GPS related data and statistics
  u8g2.setFont(u8g2_font_5x7_mr);
  u8g2.setCursor(74, 34); u8g2.print( F("HDOP ") );
  if (fix.valid.hdop) u8g2.print(fix.hdop / 1000.0);

  u8g2.setCursor(74, 44); u8g2.print( F("ALT ") );
  if (fix.valid.altitude) u8g2.print(fix.altitude()); u8g2.print( 'm' );

  u8g2.setCursor(74, 54); u8g2.print( F("GPS MODE:") );
  if (fix.valid.status) u8g2.print(fix.status);
  
  u8g2.setCursor(74, 64); u8g2.print( F("GPS ERR:") );
  if (fix.valid.lat_err) u8g2.print(fix.lat_err());
    // lon_err and alt_err also available

}


void setup() 
{
  Serial.begin(115200);
  gpsPort.begin(GPSBaud);
  u8g2.begin();

  Serial.println( F("NeoGPS display\n") );
}


void loop() 
{
  if (gps.available( gpsPort )) {
    fix = gps.read(); // get the entire, assembled fix structure

    // Shift the date/time to local time
    NeoGPS::clock_t localSeconds;
    {
      using namespace NeoGPS; // save a little typing below...

      localSeconds  = (clock_t) fix.dateTime;  // convert structure to a second count
      localSeconds +=
        timezonehr * SECONDS_PER_HOUR + 
        timezonemn * SECONDS_PER_MINUTE;       // shift to desired timezone
      localTime     = localSeconds;            // convert back to a structure
    }

    Serial.println(); 
    Serial.print( F("ALT=")       );
    if (fix.valid.altitude) Serial.print(fix.altitude()); 
    Serial.print( F(" PDOP=")     );
    if (fix.valid.pdop) Serial.print(fix.pdop * 0.001); 
    Serial.print( F(" HDOP=")     );
    if (fix.valid.hdop) Serial.print(fix.hdop * 0.001); 
    Serial.print( F(" VDOP=")     );
    if (fix.valid.vdop) Serial.print(fix.vdop * 0.001);
    Serial.print( F(" SATS=")     );
    if (fix.valid.satellites) Serial.print(fix.satellites);
    Serial.print( F(" GPS Mode=") );
    if (fix.valid.status) Serial.print(fix.status);
    Serial.print( F(" GPS Data=") );
    if (fix.valid.lat_err) Serial.print(fix.lat_err()); // m
    Serial.print( F(" TIME=")     );
    if (fix.valid.time) Serial << localTime;

    if (useOLED) {
      // Update the display, all at once, during the GPS quiet time
      u8g2.firstPage();
      do{
        draw();
      } 
      while(u8g2.nextPage());
    }
  }
}

Please help me rectify the issue as I have no idea what might be outof order in this case.

Thank you for your considerations.

Ok, at least some of us did consider the problem by witnessing it :)

The problem was somehow solved by adapting the code to one of the example codes from U8G2 library.

Thanks guys!

Antrix: The problem was somehow solved by adapting the code to one of the example codes from U8G2 library.

Could you post the new code? The code above is identical to the last thing you posted in your previous question.

Thanks ! Yeah, sure!, here it is. I still need to test it with the GPS module which I’m awaiting to be delivered. But at least, the display works. I do not know if this adapted code is bug free as programming is not my domain, but Im hopeful nonethelss.

Please take a look at the code:

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

#include <NeoSWSerial.h>
#include <NMEAGPS.h>
static const int RXPin = 4, TXPin = 3;
static NeoSWSerial gpsPort(RXPin, TXPin);
static const int GPSBaud = 9600;
static const bool useOLED = true;

static const int timezonehr = +5; //Timezone hour offset
static const int timezonemn = 30; //Timezone minute offset

static NMEAGPS         gps;
static gps_fix         fix;
static NeoGPS::time_t  localTime;

U8G2_SSD1306_128X64_NONAME_1_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE);



void setup(void)
{

  Serial.begin(115200);
  gpsPort.begin(GPSBaud);
  u8g2.begin();

  Serial.println( F("NeoGPS display\n") );

  u8g2.begin();  
}

void loop(void) {
u8g2.firstPage();
do
{


  
  //Separator (Vertical Line)
  u8g2.drawLine(68,27,68,64);


  //GPS:: Checks if GPS has valid fix, then display selective elements
  if (fix.valid.location) {
    u8g2.drawCircle(122,5,5, U8G2_DRAW_ALL); // GPS fix circle Top Right
    u8g2.drawDisc(122,5,3, U8G2_DRAW_ALL); 
  } else {
    u8g2.drawCircle(122,5,5, U8G2_DRAW_ALL);
  }
  u8g2.setFont(u8g2_font_7x13_tr);
  u8g2.setCursor(72, 9);
  u8g2.print( F(" SAT:") );
  if (fix.valid.satellites)
    u8g2.print( fix.satellites );
  

  if (fix.valid.time) {
    // TIME:: Time Elements including conversion of UTC to Local (IST +5.30)
    u8g2.setFont(u8g2_font_crox4h_tn); // instead of profont22r?
    u8g2.setCursor(0, 14);

    if (localTime.hours < 10)
      u8g2.print( '0' );
    u8g2.print( localTime.hours );
    u8g2.print( ':' );
    if (localTime.minutes < 10)
      u8g2.print( '0' );
    u8g2.print( localTime.minutes );
    u8g2.print( ' ' );
    u8g2.setFont(u8g2_font_7x13_tr); 
    if (localTime.seconds < 10)
      u8g2.print( '0' ); u8g2.print( localTime.seconds );
  }
    
  u8g2.setCursor(44, 11);

  if (fix.valid.speed) {
    //SPEED:: Displays Speed and other related graphics
    float speed = fix.speed_kph();
    u8g2.drawFrame(2,59,48,4);
    u8g2.drawBox(2,59,(speed/2.5),4);
    
    if ( (int)speed  > 100)
    {
      u8g2.drawFrame(0,22,64,35);
      u8g2.setFont(u8g2_font_logisoso30_tn);
      u8g2.setCursor(0, 54);
      u8g2.print( (int)speed );
    }
    else
    {
      u8g2.setFont(u8g2_font_logisoso30_tn);
      u8g2.setCursor(0, 56);
      u8g2.print( (int)speed );
    }
    u8g2.setColorIndex(1);
    
    //u8g2.setFont(u8g_font_orgv01r);
    u8g2.setCursor(0, 22);
    //u8g2.print("SPEED >>");
    //u8g2.setFont(u8g_font_fur30n);
    u8g2.setCursor(0, 56);
    u8g2.print( (int)speed ); 
    u8g2.setFont(u8g2_font_5x7_mr);
    u8g2.setCursor(53, 64);
    u8g2.print(F("KM"));
  }  

  //GPS DATA:: Other GPS related data and statistics
  u8g2.setFont(u8g2_font_5x7_mr);
  u8g2.setCursor(74, 34); u8g2.print( F("HDOP ") );
  if (fix.valid.hdop) u8g2.print(fix.hdop / 1000.0);

  u8g2.setCursor(74, 44); u8g2.print( F("ALT ") );
  if (fix.valid.altitude) u8g2.print(fix.altitude()); u8g2.print( 'm' );

  u8g2.setCursor(74, 54); u8g2.print( F("GPS MODE:") );
  if (fix.valid.status) u8g2.print(fix.status);
  
  u8g2.setCursor(74, 64); u8g2.print( F("GPS ERR:") );
  if (fix.valid.lat_err) u8g2.print(fix.lat_err());
    // lon_err and alt_err also available



  if (gps.available( gpsPort )) {
    fix = gps.read(); // get the entire, assembled fix structure

    // Shift the date/time to local time
    NeoGPS::clock_t localSeconds;
    {
      using namespace NeoGPS; // save a little typing below...

      localSeconds  = (clock_t) fix.dateTime;  // convert structure to a second count
      localSeconds +=
        timezonehr * SECONDS_PER_HOUR + 
        timezonemn * SECONDS_PER_MINUTE;       // shift to desired timezone
      localTime     = localSeconds;            // convert back to a structure
    }

    Serial.println(); 
    Serial.print( F("ALT=")       );
    if (fix.valid.altitude) Serial.print(fix.altitude()); 
    Serial.print( F(" PDOP=")     );
    if (fix.valid.pdop) Serial.print(fix.pdop * 0.001); 
    Serial.print( F(" HDOP=")     );
    if (fix.valid.hdop) Serial.print(fix.hdop * 0.001); 
    Serial.print( F(" VDOP=")     );
    if (fix.valid.vdop) Serial.print(fix.vdop * 0.001);
    Serial.print( F(" SATS=")     );
    if (fix.valid.satellites) Serial.print(fix.satellites);
    Serial.print( F(" GPS Mode=") );
    if (fix.valid.status) Serial.print(fix.status);
    Serial.print( F(" GPS Data=") );
    if (fix.valid.lat_err) Serial.print(fix.lat_err()); // m
    Serial.print( F(" TIME=")     );
    if (fix.valid.time) Serial << localTime;

    if (useOLED) {
      // Update the display, all at once, during the GPS quiet time
      u8g2.firstPage();
      do{
        void loop(void);
      } 
      while(u8g2.nextPage());
    }
  }

  
}

  while ( u8g2.nextPage() );
  delay(100);
}