OLED SH1106 I2C Using U8G2 Lib

Hi Everyone.

I have the SH1106 SPI OLED.

The code does compile and work in SPI Mode.
There is a jumper resistor on the pcb to change to I2C Mode.
Now I cannot find the right construct in the U8G2Lib for changing to I2C.

I copy the Construct line from the U8g2Lib.h line 2105 that is were the No_name OLED's
start.
That is the only place were I could find the constructs but do I need to specify pins with the construct?

My Working code SPI Mode.

#include "U8g2lib.h"            // U8glib library for the OLED

int seconds = 0;
int minutes = 0;
int piezoPin = 2;

U8G2_SH1106_128X64_NONAME_F_4W_HW_SPI u8g2(U8G2_R0, /* cs=*/ 10, /* dc=*/ 9, /* reset=*/ 8);//This does compile


//U8G2_SH1106_128X64_NONAME_1_HW_I2C ;// gives error does not compile
//U8G2_SH1106_128X64_NONAME_1_2ND_HW_I2C ;//gives error does not compile
//U8G2_SH1106_128X64_NONAME_F_SW_I2C ;// gives error does not compile

void draw(void)//This drawlines takes 18/19 mili Seconds to finish
{
  //u8g2.setFont(u8g2_font_5x7_tf);//6 pixels 123,abc
  //u8g2.setFont(u8g2_font_6x10_tn);//10 pixels no abc
  // u8g2.setFont(u8g2_font_t0_11_tr);//10 pixels 123,abc
  //u8g2.setFont( u8g2_font_VCR_OSD_tf );//15 pixels same as (profont22_tf)
  //u8g2.setFont(u8g2_font_8x13B_mf);//10 pixels 123,abc (squire ish abc)
  //u8g2.setFont(u8g2_font_sirclivethebold_tr);//not nice
  //u8g2.setFont(u8g2_font_courB24_tf);//20 pixels//"VOLTAGE" BUITE SCREEN 0.00V Can adjust
  //u8g2.setFont(u8g2_font_9x15_tr );//10 pixels 123,abc usable
  //u8g2.setFont(u8g2_font_t0_15b_mr );//10 pixels 123,abc

  //unsigned long currentMillis = millis();

  u8g2.setFont(u8g2_font_profont22_tf);        // select font
  u8g2.drawStr(18, 15, "TIMER");//(u8g = 18,12)
  u8g2.setCursor(33, 40);
  u8g2.drawRFrame(15, 20, 100, 30, 10);     // draws frame with rounded edges

  u8g2.println(minutes);
  u8g2.println(":");
  if (seconds < 10)
  {
    u8g2.println("0");
  }
  u8g2.println(seconds);

}
//-------------------------
void done()
{
  while (1)
  {
    tone(piezoPin, 1000, 500);
    delay(500);
    tone(piezoPin, 0, 500);
    delay(500);

  }
}
//-------------------------
void setup()
{
  u8g2.begin();
  pinMode(7, OUTPUT);
  //Serial.begin(9600);
}
void loop()
{
  delay(981);//1000 - 19 to redraw screen(981)
  seconds = seconds + 1;
  if (seconds > 59)
  {
    seconds = 0;
    minutes = minutes + 1;
  }
  if (minutes > 14)
  {
    done();
  }
  u8g2.firstPage();
  do
  {
    draw();
  }
  while ( u8g2.nextPage() );

}

This is the Error code when the construct is I2C.

Arduino: 1.8.15 (Windows 10), Board: "Arduino Nano, ATmega328P (Old Bootloader)"





















C:\Mike\C Programming\ARDUINO\ARDUINO MIKE\OLED_TimerU8g2\OLED_TimerU8g2.ino:16:1: warning: declaration does not declare anything [-fpermissive]

 U8G2_SH1106_128X64_NONAME_F_SW_I2C ;

 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

C:\Mike\C Programming\ARDUINO\ARDUINO MIKE\OLED_TimerU8g2\OLED_TimerU8g2.ino: In function 'void draw()':

OLED_TimerU8g2:31:3: error: 'u8g2' was not declared in this scope

   u8g2.setFont(u8g2_font_profont22_tf);        // select font

   ^~~~

C:\Mike\C Programming\ARDUINO\ARDUINO MIKE\OLED_TimerU8g2\OLED_TimerU8g2.ino:31:3: note: suggested alternative: 'U8G2'

   u8g2.setFont(u8g2_font_profont22_tf);        // select font

   ^~~~

   U8G2

C:\Mike\C Programming\ARDUINO\ARDUINO MIKE\OLED_TimerU8g2\OLED_TimerU8g2.ino: In function 'void setup()':

OLED_TimerU8g2:60:3: error: 'u8g2' was not declared in this scope

   u8g2.begin();

   ^~~~

C:\Mike\C Programming\ARDUINO\ARDUINO MIKE\OLED_TimerU8g2\OLED_TimerU8g2.ino:60:3: note: suggested alternative: 'U8G2'

   u8g2.begin();

   ^~~~

   U8G2

C:\Mike\C Programming\ARDUINO\ARDUINO MIKE\OLED_TimerU8g2\OLED_TimerU8g2.ino: In function 'void loop()':

OLED_TimerU8g2:77:3: error: 'u8g2' was not declared in this scope

   u8g2.firstPage();

   ^~~~

C:\Mike\C Programming\ARDUINO\ARDUINO MIKE\OLED_TimerU8g2\OLED_TimerU8g2.ino:77:3: note: suggested alternative: 'U8G2'

   u8g2.firstPage();

   ^~~~

   U8G2

exit status 1

'u8g2' was not declared in this scope



This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

The following two constructors are from the HelloWorld example sketch. You do not need to specify the pins when using hardware I2C.

U8G2_SH1106_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE);
U8G2_SH1106_128X32_VISIONOX_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE); 
  u8g2.firstPage();
  do
  {
    draw();
  }
  while ( u8g2.nextPage() );

This should not be used with a full buffer (the _F_ in the constructor), it is only needed when using a paged buffer ( _1_ in the constructor).

Thanks.

I found a Serial example in the U8g2 Library (Dont now why I did not look there first )

I used this construct , The code does compile but the OLED shows nothing .The same as the one you posted.

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

What should happen to the un used pins like CS?

I'm not familiar with how to convert from SPI to I2C on that display, hopefully someone else can help with that.

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