Trouble getting OLED type SH1106 display work at Adalogger (ATSAMD21G18)

Hey guys,

almost new on programming arduinos, I got stuck here. Can anyone help?

Board: Adafruit Feather M0 Adalogger
Display 128 x 64 OLED Type SH1106
connected via SDA(GPIO #20), SLC(GPIO #21), VDD, GND

Which libraries do I need?
#include <U8g2lib.h>
#include <SPI.h>
#include <Wire.h>

Which constructor is to be used?
Tried this → error message!

//template :U8G2_SH1106_128X64_NONAME_1_HW_I2C(rotation, [reset [, clock, data]])
U8G2_SH1106_128X64_NONAME_1_HW_I2C(U8G2_R0, [U8X8_PIN_NONE [,21, 20]]);

Here the rest of the code I got from some tutorial:

void setup(void) {
u8g2.begin();
}

void loop(void) {
u8g2.firstPage();
do {
u8g2.setFont(u8g2_font_ncenB14_tr);
u8g2.drawStr(0,24,“Hello World!”);
} while ( u8g2.nextPage() );
}

Thanks a lot!
Thomas

Hi

The constructor should be U8G2_SH1106_128X64_NONAME_1_HW_I2C(U8G2_R0)

"[" and "]" mark optional arguments. Although widely accepted, I might have missed to document this.

Oliver

Edit: I have updated the u8g2 documentation.

Well, I see… Optional arguments.

Following your example in the u8g2 documentation, it still will not work.

#include <Arduino.h>
#include <U8g2lib.h>
#include <SPI.h>
#include <Wire.h>

//U8G2_SH1106_128X64_NONAME_1_HW_I2C(rotation, [reset [, clock, data]])
  U8G2_SH1106_128X64_NONAME_1_HW_I2C(U8G2_R0);

Error message:
Arduino: 1.8.5 (Windows 7), Board: “Adafruit Feather M0”

testDisplay:7: error: conflicting declaration ‘U8G2_SH1106_128X64_NONAME_1_HW_I2C& u8g2_cb_r0’

U8G2_SH1106_128X64_NONAME_1_HW_I2C(U8G2_R0);

^

In file included from C:\Users\BenutzerEliteBook\Documents\Arduino\libraries\U8g2\src/U8g2lib.h:52:0,

from C:\Users\BenutzerEliteBook\Documents\Arduino\testDisplay\testDisplay.ino:2:

C:\Users\BenutzerEliteBook\Documents\Arduino\libraries\U8g2\src/clib/u8g2.h:394:24: error: ‘u8g2_cb_r0’ has a previous declaration as ‘const u8g2_cb_t u8g2_cb_r0’

extern const u8g2_cb_t u8g2_cb_r0;

^

C:\Users\BenutzerEliteBook\Documents\Arduino\testDisplay\testDisplay.ino: In function ‘void setup()’:

testDisplay:10: error: ‘u8g2’ was not declared in this scope

u8g2.begin();

^

C:\Users\BenutzerEliteBook\Documents\Arduino\testDisplay\testDisplay.ino: In function ‘void loop()’:

testDisplay:14: error: ‘u8g2’ was not declared in this scope

u8g2.firstPage();

^

exit status 1
conflicting declaration ‘U8G2_SH1106_128X64_NONAME_1_HW_I2C& u8g2_cb_r0’

Use

U8G2_SH1106_128X64_NONAME_1_HW_I2C u8g2(U8G2_R0);

U8G2_SH1106_128X64_NONAME_1_HW_I2C is the classname, but you also need to name an object, which is "u8g2" in this case.

It is a little bit misleading, because
U8G2_SH1106_128X64_NONAME_1_HW_I2C(U8G2_R0);
is called the constructor.

great. this is working fine since...

U8X8_SH1106_128X64_NONAME_HW_I2C u8x8(/* reset=*/ U8X8_PIN_NONE);

tried to step up from U8x8 to U8g2… but failed again. Does anyone could help please :slight_smile:

#include <Arduino.h>
#include <U8g2lib.h>
#include <SPI.h>
#include <Wire.h> 

// U8x8 working example 
// U8X8_SH1106_128X64_NONAME_HW_I2C u8x8(/* reset=*/ U8X8_PIN_NONE);

// Beispiel from manual
//U8G2_ST7920_128X64_1_SW_SPI u8g2(U8G2_R0, 13, 11, 10, 8);

// matching constructor examples form manual 
// U8G2_SH1106_128X64_NONAME_1_SW_I2C(rotation, clock, data [, reset]);
// U8G2_SH1106_128X64_NONAME_1_HW_I2C(rotation, [reset [, clock, data]]);
// U8G2_SH1106_128X64_NONAME_1_2ND_HW_I2C(rotation, [reset]); //If supported, use second hardware I2C (Arduino Wire lib)

U8G2_SH1106_128X64_NONAME_1_SW_I2C(U8G2_R0,/* clock=*/ 21, /* data=*/ 20);

void setup(void) {
  u8g2.begin();
}

void loop(void) {
  u8g2.firstPage();
  do {
    u8g2.setFont(u8g2_font_ncenB14_tr);
    u8g2.drawStr(0,24,"Hello World!");
  } while ( u8g2.nextPage() );
}

error message:

Arduino: 1.8.5 (Windows 7), Board: "Adafruit Feather M0"

Build options changed, rebuilding all
test_display:17: error: expected ')' before ',' token

 U8G2_SH1106_128X64_NONAME_1_SW_I2C(U8G2_R0,/* clock=*/ 21, /* data=*/ 20);

                                           ^

test_display:17: error: expected unqualified-id before numeric constant

 U8G2_SH1106_128X64_NONAME_1_SW_I2C(U8G2_R0,/* clock=*/ 21, /* data=*/ 20);

                                                        ^

D:\Arduino\sketches\test_display\test_display.ino: In function 'void setup()':

test_display:20: error: 'u8g2' was not declared in this scope

   u8g2.begin();

   ^

D:\Arduino\sketches\test_display\test_display.ino: In function 'void loop()':

test_display:24: error: 'u8g2' was not declared in this scope

   u8g2.firstPage();

   ^

exit status 1
expected ')' before ',' token

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

using the second constructor option
U8G2_SH1106_128X64_NONAME_1_HW_I2C(U8G2_R0);

this error message appears

Arduino: 1.8.5 (Windows 7), Board: "Adafruit Feather M0"

test_display:17: error: conflicting declaration 'U8G2_SH1106_128X64_NONAME_1_HW_I2C& u8g2_cb_r0'

 U8G2_SH1106_128X64_NONAME_1_HW_I2C(U8G2_R0);

                                           ^

In file included from C:\Users\BenutzerEliteBook\Documents\Arduino\libraries\U8g2\src/U8g2lib.h:52:0,

                 from D:\Arduino\sketches\test_display\test_display.ino:2:

C:\Users\BenutzerEliteBook\Documents\Arduino\libraries\U8g2\src/clib/u8g2.h:394:24: error: 'u8g2_cb_r0' has a previous declaration as 'const u8g2_cb_t u8g2_cb_r0'

 extern const u8g2_cb_t u8g2_cb_r0;

                        ^

D:\Arduino\sketches\test_display\test_display.ino: In function 'void setup()':

test_display:21: error: 'u8g2' was not declared in this scope

   u8g2.begin();

   ^

D:\Arduino\sketches\test_display\test_display.ino: In function 'void loop()':

test_display:25: error: 'u8g2' was not declared in this scope

   u8g2.firstPage();

   ^

exit status 1
conflicting declaration 'U8G2_SH1106_128X64_NONAME_1_HW_I2C& u8g2_cb_r0'

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

Same answer and solutuion as before: You must use a object name. I suggest to learn a little bit of C++. Remember: Arduino language is C++.

Oliver

for anyone who might search for a similar solution, these both are working:

U8G2_SH1106_128X64_NONAME_1_HW_I2C u8g2(U8G2_R0);
U8G2_SH1106_128X64_NONAME_1_SW_I2C u8g2(U8G2_R0, 21, 20);

compared to the code above, the u8g2 statement in front of the paranthesis was just missing

@Oli: after programming in Matlab/Octave for more than 10 years now, C++ is still able to confuse quite a lot. Any recomondation for tutorials and/or literature to smmothen the transition?

Skydome:
C++ is still able to confuse quite a lot. Any recomondation for tutorials and/or literature to smmothen the transition?

What I have learned on C++ is mostly based on the book from Bjarne Stroustrup "The C++ Programming Language".

But i think some of the tutorials here are also fine: Classes (I) - C++ Tutorials

Oliver

olikraus:
Same answer and solutuion as before: You must use a object name. I suggest to learn a little bit of C++. Remember: Arduino language is C++.

Oliver

Funny, I actually come here to learn a little bit of C++, but then someone always tells me I don’t know enough to come here and ask questions. Hahahahahahaha

You learn much more when you have a specific project in mind.

Follow Oliver's advice on the correct constructor. Get your project working.

Then study the relevant chapter in the textbook to understand how it works.

David.