OLED SH1106G random pixels at the left border

Hi everybody,
I have a strange behavior on a 1.54" OLED Display which on other ones does not appear:

same code, just plugging in a 1.3" OLED :slight_smile:

The code loaded on an ESP32C supermini is this one:

// Digital Clock Galactico
// Test for ESP32C3 Supermini
// November 2023
// Bernardo Gullasch
// including Wifi, OLED, Setbuttons

#include <DS3231.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SH110X.h>
#include <Adafruit_SSD1306.h>
#include <SPI.h>
#include <WiFi.h>


// Create the OLED display
Adafruit_SH1106G oled = Adafruit_SH1106G(128, 64, &Wire, -1);



// WiFi setup data
const char* ssid = "Galactico_Salon"; // Name of the Wi-Fi box
const char* password = "saturno002"; // MDP of the Wi-Fi box

const char* ntpServer = "cl.pool.ntp.org";
const long  gmtOffset_sec = 3600 * -3;
const int   daylightOffset_sec = 3600 * 1;

int AdjustPin = 1; // black button
int SetPin = 0; // yellow button

bool Century = false;
bool h12; // RTC read Hour Parameter
bool PM;  // RTC read Hour Parameter

DS3231 Clock;

int DoW;
const char *WeekDay[] = {"Domingo  ", "Lunes    ", "Martes   ", "Miercoles", "Jueves   ", "Viernes  ", "Sabado   "};
bool END;
int Block[7];
int setVal=0;
int dispPos=0;


int showHour;
int showMinute;
int count;



// --------- SETUP ---------
void setup() 
{
   oled.begin();
   oled.setTextSize(1);
   oled.setTextColor(SH110X_WHITE, SH110X_BLACK);
   oled.clearDisplay();
   oled.display();

  Wire.begin();
  delay(100);

  pinMode(AdjustPin, INPUT); // right button
  pinMode(SetPin, INPUT); // left button

//  Serial.println(" ***** Start Setup *****");
  
  WiFi.mode(WIFI_STA); // Optional
  WiFi.begin(ssid, password);
  oled.setCursor(0,0);
  oled.println("  Time init");
  oled.println("  Connecting");
  while (WiFi.status() != WL_CONNECTED) 
  {
    oled.print("."); oled.display();
    count++;
    if(count>100) {oled.clearDisplay(); oled.setCursor(0,0); oled.println("connection failed"); oled.display(); delay(2000); break;}
    delay(100);
  }
  if(WiFi.status() == WL_CONNECTED)
  {
  oled.clearDisplay();
  oled.setCursor(0,0);  
  oled.println("Connected to ");
  oled.println(); 
  oled.println(ssid);
  oled.println();
  oled.println("Local ESP32 IP: ");
  oled.println();
  oled.println(WiFi.localIP());
  oled.display();

  delay(3000);
  oled.clearDisplay();
  oled.setCursor(0,0);
  oled.println("get Time from NTP...");
  oled.display();

  // We configure the NTP server
  configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
  struct tm timeinfo;
  if (!getLocalTime(&timeinfo)) 
  {
    oled.clearDisplay();
    oled.setCursor(0,0);
    oled.println("Failed to obtain time");
    oled.println("adjust time manually");
    oled.display();
    delay(2000);
    InputTime();   
  }
  else
  {
  Block[0] = timeinfo.tm_year -100;
  Block[1] = timeinfo.tm_mon + 1;
  Block[2] = timeinfo.tm_mday;
  Block[3] = timeinfo.tm_wday;
  Block[4] = timeinfo.tm_hour;
  Block[5] = timeinfo.tm_min;
  Block[6] = timeinfo.tm_sec;
  SetClock();
  oled.clearDisplay();
  oled.setCursor(0,0);
  oled.println("Time set OK");
  oled.display();
  }
  }
  else
  {
    InputTime();
  }

  //delay(3000);

// while (!digitalRead(SetPin)) {;}
  
  Clock.setClockMode(false); // Clock 24 Hour Mode
  Serial.println("ready for Setup ...");
  
    Block[0] = Clock.getYear(), DEC;
    Block[1] = Clock.getMonth(Century), DEC;
    Block[2] = Clock.getDate(), DEC;
    Block[3] = Clock.getDoW(), DEC;
    Block[4] = Clock.getHour(h12,PM), DEC;
    Block[5] = Clock.getMinute(),DEC;
    Block[6] = Clock.getSecond(),DEC;
    
   oled.clearDisplay();
   //oled.drawRect(1,1,126,30,WHITE);
   oled.setCursor(4,5);   
   oled.print("RTC actual time is");
   oled.setCursor(4,12);
   oled.print("-------------------");
   oled.setCursor(4,19);
   
   if (Block[2]<10) {oled.print("0");} 
   oled.print(Block[2]);
   oled.print(".");
   if (Block[1]<10) {oled.print("0");}
   oled.print(Block[1]);
   oled.print(". ");
   oled.print("20"); oled.print(Block[0]);
   oled.print(" ");

   if(Block[4]<10) {oled.print("0");} oled.print(Block[4]); 
   oled.print(":");
   if(Block[5]<10) {oled.print("0");} oled.print(Block[5]);
   oled.print(":");
   if(Block[6]<10) {oled.print("0");} oled.println(Block[6]);

   oled.drawRect(0,0,128,32,WHITE);

   oled.display(); 


/*  while (!digitalRead(SetPin) && !digitalRead(AdjustPin)) {;}

// **************** NOW INPUT DATA *******************
   if (digitalRead(SetPin))
   {
    oled.clearDisplay();
    delay(200);
    InputTime();
   }
 */
  delay(5000);
    oled.clearDisplay();
    oled.setCursor(0,20);
    oled.print("* Time Set ready *");
    oled.display();
  delay(1000);
  // LET´S GO
} // --------------------- finish Setup ------------------------ 

void loop() 
{
  showTime();
} 
// ------------------------------------- End Loop ------------------------------


void InputTime()
  {
       Block[0]=23;
       Block[1]=11;
       Block[2]=18;
       Block[3]=6;
       setVal=3;
       while (setVal<7) // input date/time values
        {
        if (digitalRead(AdjustPin)) // ajust actual value
         {
          Block[setVal]++ ;
          if (Block[0] > 32) {Block[0] = 22;}
          if (Block[1] > 12) {Block[1] = 1;}
          if (Block[2] > 31) {Block[2] = 1;}
          if (Block[3] > 7 ) {Block[3] = 1;}
          if (Block[4] > 23) {Block[4] = 0;}
          if (Block[5] > 59) {Block[5] = 0;}
          if (Block[6] > 59) {Block[6] = 0;}      
          
          delay(100);      
         }
         
         if (digitalRead(SetPin)) // set value and goto next 
         {
          setVal++;
          delay(100);
         }  
         showInput();
         delay(200);    
        } // ready input date/time values 0 to 5

    oled.clearDisplay();
    oled.setCursor(0,0);
    oled.print("Ready for adjust time");
    oled.display();
    while(!digitalRead(SetPin)) {;}
    
    SetClock();
  }


void showInput()
{
  oled.clearDisplay();
  oled.setCursor(0,0);
  oled.println("Hora Minuto Segundo");
  oled.println("---------------------");
  for (dispPos=4; dispPos<7; dispPos++)
  {
  oled.setCursor(((dispPos-4)*36),19);
  if (dispPos==setVal) {oled.setTextColor(SH110X_BLACK, SH110X_WHITE);} else {oled.setTextColor(SH110X_WHITE, SH110X_BLACK);}
  if (Block[dispPos] < 10) {oled.print("0"); oled.print(Block[dispPos]);} else  {oled.print(Block[dispPos]);}
  }
  oled.display();
  oled.setTextColor(SH110X_WHITE, SH110X_BLACK);
 } 

void SetClock()
  {
   Clock.setClockMode(false);  // set to 24h
   Clock.setSecond(Block[6]);
   Clock.setMinute(Block[5]);
   Clock.setHour(Block[4]);
   Clock.setDoW(Block[3]);
   Clock.setDate(Block[2]);
   Clock.setMonth(Block[1]);
   Clock.setYear(Block[0]);

   oled.setCursor(30,24);
   oled.print("* READY *");
   oled.display();
   delay(1000);      
  }

void showTime()
{   
   
   oled.clearDisplay();
   oled.setTextSize(1);
   oled.setCursor(0,8);
   if(Clock.getDate() < 10) {oled.print("0");}
   oled.print(Clock.getDate(), DEC);
   oled.print(".");
   oled.print(Clock.getMonth(Century), DEC);
   if(Clock.getMonth(Century) < 10) {oled.print("0");}
   oled.print(". ");
   oled.print("20"); oled.print(Clock.getYear(), DEC);
   oled.print(" ");
   oled.print(WeekDay[Clock.getDoW()]);
   
   oled.drawRoundRect(0,25,128,32,10,WHITE);
   oled.drawRoundRect(1,26,126,30,10,WHITE);
   oled.setTextSize(2);
   oled.setCursor(15,33);
   if ((Clock.getHour(h12, PM)) < 10){oled.print("0");}
   oled.print(Clock.getHour(h12, PM));
   oled.print(":");
   if ((Clock.getMinute()) < 10){oled.print("0");} 
   oled.print(Clock.getMinute());
   oled.print(":");
   if ((Clock.getSecond()) < 10){oled.print("0");}
   oled.print(Clock.getSecond());
   oled.display();
}

the display is not broke, I can reproduce the issue on another one of the same type.
the effect appears only using the Adafruit library, with other libraries the respecting examples work fine.
Any idea what this is about ?

1 Like

Are you certain the display uses an SH1106G and not an SSD1306?

yes I am definitely sure, actually I changed the library to use a SSD1306 and got all scrambled.
plugging a SSD1306-OLED and start the code with changed library again works good. I made this little board to test different OLEDs

Do you get the random pixels if you run the Adafruit SH1106 example sketches?

Maybe a SSD1309... but I think a SH1106G without the 2 bit offset is more likely. Look in the library's begin() method and comment out the assignment of _page_start_offset near the start and see what that does.

Slightly off-topic, but why are you using an RTC with an ESP? You don't need one.

yes, the same effect with the Adafruit examples

Sounds like a problem with the Adafruit library. And the problem occurs with their example code. Sounds like you should report this on the Adafruit forum.

...that´s why I like this forum so much, in less than 1 hour I got the solution :slight_smile:
I found this variable _page_start_offset and set it to "0" and it works with the 1.54" display.
However now the effect appears on the right edge of my 1.3" display.
Anyway, now I know how to handle it and adapt the code to the display I am using.

Thanks a lot Mr. Van Der Decken

my problem is solved,
now - off topic - what do you mean with I don´t need a RTC with my ESP32 processor ?

So long as the ESP32 has WiFi/internet access, you can use the built-in software RTC that comes packaged with the ESP32 Arduino core. It keeps itself accurate by regularly synchronising with network time servers. The result is even more accurate than any RTC module sold for use with Arduino.

Because it's built into the ESP Arduino core, it's simple to use, requiring only a few lines of code to configure it for your locale.

1 Like

Thanks to you, Paul
the side effect of this topic is that on top of the solution of my main problem I learned that my ESP32C processor has a RTC included. I checked and found all I need to change my code and made it even more compact .
Really great, I love it, thanks to everybody and especially to Paul and "van_der_decken"

1 Like

Here's the simplest way to get time in your local timezone including daylight savings time:

https://werner.rothschopf.net/microcontroller/202103_arduino_esp32_ntp_en.htm

I had the same problem with SSD1306. I couldn't get my 128x64 oled to work until I found your code, so thanks very much :slight_smile:

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.