How to learn to write on a 128 x 32 SSD1306 OLED?

Hello, I am new to Arduino and I am trying to write some temperature and humidity data on a 128 x 32 OLED display. However, I see there are two libraries: U8GLIB and U8G2, the second being newer. I am using a SSD1306 based OLED, so I tried the "Hello Wolrd" example, but I am getting the error shown below.
I am laso including the code after that.

I have two questions:

  1. Is there a way to fix this?
  2. Is there an easy to understand reference to learn how to use this dysplay ? Available references include many many other drivers and it becomes confusing to understand what applies to the one I am using.

Please see the details below.

----------Error message starts-------------
C:\Users\Marco\AppData\Local\Temp.arduinoIDE-unsaved2024614-10300-1l3c8ad.1k7z\HelloWorld\HelloWorld.ino:47:26: error: expected ')' before ',' token
U8GLIB_SSD1306_128X32(sck, mosi, cs, a0);
^
C:\Users\Marco\AppData\Local\Temp.arduinoIDE-unsaved2024614-10300-1l3c8ad.1k7z\HelloWorld\HelloWorld.ino:47:40: error: expected initializer before ')' token
U8GLIB_SSD1306_128X32(sck, mosi, cs, a0);
^
C:\Users\Marco\AppData\Local\Temp.arduinoIDE-unsaved2024614-10300-1l3c8ad.1k7z\HelloWorld\HelloWorld.ino:48:25: error: expected ')' before ',' token
U8GLIB_SSD1306_128X32(cs, a0);
^
C:\Users\Marco\AppData\Local\Temp.arduinoIDE-unsaved2024614-10300-1l3c8ad.1k7z\HelloWorld\HelloWorld.ino:48:29: error: expected initializer before ')' token
U8GLIB_SSD1306_128X32(cs, a0);
^
C:\Users\Marco\AppData\Local\Temp.arduinoIDE-unsaved2024614-10300-1l3c8ad.1k7z\HelloWorld\HelloWorld.ino:50:29: error: expected ')' before ',' token
U8GLIB_SSD1306_128X32_2X(sck, mosi, cs, a0);
^
C:\Users\Marco\AppData\Local\Temp.arduinoIDE-unsaved2024614-10300-1l3c8ad.1k7z\HelloWorld\HelloWorld.ino:50:31: error: conflicting declaration 'U8GLIB_SSD1306_128X32_2X mosi'
U8GLIB_SSD1306_128X32_2X(sck, mosi, cs, a0);
^~~~
C:\Users\Marco\AppData\Local\Temp.arduinoIDE-unsaved2024614-10300-1l3c8ad.1k7z\HelloWorld\HelloWorld.ino:47:28: note: previous declaration as 'U8GLIB_SSD1306_128X32 mosi'
U8GLIB_SSD1306_128X32(sck, mosi, cs, a0);
^~~~
C:\Users\Marco\AppData\Local\Temp.arduinoIDE-unsaved2024614-10300-1l3c8ad.1k7z\HelloWorld\HelloWorld.ino:50:37: error: conflicting declaration 'U8GLIB_SSD1306_128X32_2X cs'
U8GLIB_SSD1306_128X32_2X(sck, mosi, cs, a0);
^~
C:\Users\Marco\AppData\Local\Temp.arduinoIDE-unsaved2024614-10300-1l3c8ad.1k7z\HelloWorld\HelloWorld.ino:47:34: note: previous declaration as 'U8GLIB_SSD1306_128X32 cs'
U8GLIB_SSD1306_128X32(sck, mosi, cs, a0);
^~
C:\Users\Marco\AppData\Local\Temp.arduinoIDE-unsaved2024614-10300-1l3c8ad.1k7z\HelloWorld\HelloWorld.ino:50:43: error: expected initializer before ')' token
U8GLIB_SSD1306_128X32_2X(sck, mosi, cs, a0);
^
C:\Users\Marco\AppData\Local\Temp.arduinoIDE-unsaved2024614-10300-1l3c8ad.1k7z\HelloWorld\HelloWorld.ino:51:28: error: expected ')' before ',' token
U8GLIB_SSD1306_128X32_2X(cs, a0);
^
C:\Users\Marco\AppData\Local\Temp.arduinoIDE-unsaved2024614-10300-1l3c8ad.1k7z\HelloWorld\HelloWorld.ino:51:32: error: expected initializer before ')' token
U8GLIB_SSD1306_128X32_2X(cs, a0);
^
C:\Users\Marco\AppData\Local\Temp.arduinoIDE-unsaved2024614-10300-1l3c8ad.1k7z\HelloWorld\HelloWorld.ino:52:33: error: conflicting declaration 'U8GLIB_SSD1306_128X32_2X options'
U8GLIB_SSD1306_128X32_2X(options);
^
C:\Users\Marco\AppData\Local\Temp.arduinoIDE-unsaved2024614-10300-1l3c8ad.1k7z\HelloWorld\HelloWorld.ino:49:23: note: previous declaration as 'U8GLIB_SSD1306_128X32 options'
U8GLIB_SSD1306_128X32(options);
^~~~~~~
C:\Users\Marco\AppData\Local\Temp.arduinoIDE-unsaved2024614-10300-1l3c8ad.1k7z\HelloWorld\HelloWorld.ino:62:26: error: redefinition of 'U8GLIB_SSD1306_128X32 u8g'
U8GLIB_SSD1306_128X32 u8g(10, 9); // HW SPI Com: CS = 10, A0 = 9 (Hardware Pins are SCK = 13 and MOSI = 11)
^
C:\Users\Marco\AppData\Local\Temp.arduinoIDE-unsaved2024614-10300-1l3c8ad.1k7z\HelloWorld\HelloWorld.ino:61:23: note: 'U8GLIB_SSD1306_128X32 u8g' previously declared here
U8GLIB_SSD1306_128X32 u8g(13, 11, 10, 9); // SW SPI Com: SCK = 13, MOSI = 11, CS = 10, A0 = 9
^~~
C:\Users\Marco\AppData\Local\Temp.arduinoIDE-unsaved2024614-10300-1l3c8ad.1k7z\HelloWorld\HelloWorld.ino:63:26: error: redefinition of 'U8GLIB_SSD1306_128X32 u8g'
U8GLIB_SSD1306_128X32 u8g(U8G_I2C_OPT_NONE); // I2C / TWI
^
C:\Users\Marco\AppData\Local\Temp.arduinoIDE-unsaved2024614-10300-1l3c8ad.1k7z\HelloWorld\HelloWorld.ino:61:23: note: 'U8GLIB_SSD1306_128X32 u8g' previously declared here
U8GLIB_SSD1306_128X32 u8g(13, 11, 10, 9); // SW SPI Com: SCK = 13, MOSI = 11, CS = 10, A0 = 9
^~~

exit status 1

Compilation error: expected ')' before ',' token
----------Error message ends -------------

I tried removing all the unnnecesary constructor information of other devices and tried to leave the code as simple as possible. Here it is:

----------Code starts -------------

#include "U8glib.h"
// setup u8g object, please remove comment from one of the following constructor calls
// IMPORTANT NOTE: The following list is incomplete. The complete list of supported 
// devices with all constructor calls is here: https://github.com/olikraus/u8glib/wiki/device

U8GLIB_SSD1306_128X32(sck, mosi, cs, a0);
U8GLIB_SSD1306_128X32(cs, a0);
U8GLIB_SSD1306_128X32(options);
U8GLIB_SSD1306_128X32_2X(sck, mosi, cs, a0);
U8GLIB_SSD1306_128X32_2X(cs, a0);
U8GLIB_SSD1306_128X32_2X(options);


U8GLIB_SSD1306_128X32 u8g(13, 11, 10, 9);	// SW SPI Com: SCK = 13, MOSI = 11, CS = 10, A0 = 9
U8GLIB_SSD1306_128X32 u8g(10, 9);             // HW SPI Com: CS = 10, A0 = 9 (Hardware Pins are  SCK = 13 and MOSI = 11)
U8GLIB_SSD1306_128X32 u8g(U8G_I2C_OPT_NONE);	// I2C / TWI 


void draw(void) {
  // graphic commands to redraw the complete screen should be placed here  
  u8g.setFont(u8g_font_unifont);
  //u8g.setFont(u8g_font_osb21);
  u8g.drawStr( 0, 22, "Hello World!");
}

void setup(void) {
  // flip screen, if required
  // u8g.setRot180();
  
  // set SPI backup if required
  //u8g.setHardwareBackup(u8g_backup_avr_spi);

  // assign default color value
  if ( u8g.getMode() == U8G_MODE_R3G3B2 ) {
    u8g.setColorIndex(255);     // white
  }
  else if ( u8g.getMode() == U8G_MODE_GRAY2BIT ) {
    u8g.setColorIndex(3);         // max intensity
  }
  else if ( u8g.getMode() == U8G_MODE_BW ) {
    u8g.setColorIndex(1);         // pixel on
  }
  else if ( u8g.getMode() == U8G_MODE_HICOLOR ) {
    u8g.setHiColorByRGB(255,255,255);
  }
  
  pinMode(8, OUTPUT);
}

void loop(void) {
  // picture loop
  u8g.firstPage();  
  do {
    draw();
  } while( u8g.nextPage() );
  
  // rebuild the picture after some delay
  //delay(50);
}

----------Code ends -------------

I added the following lines beacuse I think this is what was suggested in the following libarry link:

U8GLIB_SSD1306_128X32(sck, mosi, cs, a0);
U8GLIB_SSD1306_128X32(cs, a0);
U8GLIB_SSD1306_128X32(options);
U8GLIB_SSD1306_128X32_2X(sck, mosi, cs, a0);
U8GLIB_SSD1306_128X32_2X(cs, a0);
U8GLIB_SSD1306_128X32_2X(options);

But I really have no idea how the code is working.

I have two questions:

  1. Is there a way to fix this?
  2. Is there an easy to understand reference to learn how to use this dysplay ? Available references include many many other drivers and it becomes confusing to understand what applies to the one I am using.

Any help would be greatly appreciated.

:roll_eyes:

1 Like

That was a mistake, so start over.

When starting with a new library, run one or more of the library examples without making any changes. The most you should ever need to do is change a device address, or perhaps a pin number.

Please state which Arduino you are using, which display (post links to product pages) and post a wiring diagram. Hand drawn is preferred.

Note: "uncomment" means to remove the double slash "//" at the beginning of a line of code

Try this sketch in an Arduino UNO as an Example:

#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 32 // OLED display height, in pixels

#define OLED_RESET     -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
void setup()
{
  Serial.begin(9600);
  Serial.println("Forum");
  pinMode(13, OUTPUT);

  // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
  if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS))
  {
    Serial.println(F("SSD1306 allocation failed"));
    for (;;); // Don't proceed, loop forever
  }

  Wire.beginTransmission(0x3C);
  byte busStatus = Wire.endTransmission();
  if(busStatus != 0)
  {
    Serial.print("OLED is not found!");
    while(true);   //wait for ever
  }
  Serial.println("OLED is found.");
  
  //display.display();
  delay(5000); // Pause for 5 seconds

  display.clearDisplay();

  // Draw a single pixel in white
  display.setCursor (5, 5);
  display.setTextSize (1);
  display.print("Hello!");
  display.display();
  delay(2000);
}

void loop()
{
  digitalWrite(13, !digitalRead(13));
  delay(1000);
}

Thank you very much for your quick reply and for the training reference.
I uncommented what I thought was left and removed what I had added and nbow the code looks like this:

#include "U8glib.h"
// setup u8g object, please remove comment from one of the following constructor calls
// IMPORTANT NOTE: The following list is incomplete. The complete list of supported 
// devices with all constructor calls is here: https://github.com/olikraus/u8glib/wiki/device

U8GLIB_SSD1306_128X32 u8g(13, 11, 10, 9);	// SW SPI Com: SCK = 13, MOSI = 11, CS = 10, A0 = 9
U8GLIB_SSD1306_128X32 u8g(10, 9);             // HW SPI Com: CS = 10, A0 = 9 (Hardware Pins are  SCK = 13 and MOSI = 11)
U8GLIB_SSD1306_128X32 u8g(U8G_I2C_OPT_NONE);	// I2C / TWI 


void draw(void) {
  // graphic commands to redraw the complete screen should be placed here  
  u8g.setFont(u8g_font_unifont);
  //u8g.setFont(u8g_font_osb21);
  u8g.drawStr( 0, 22, "Hello World!");
}

void setup(void) {
  // flip screen, if required
  // u8g.setRot180();
  
  // set SPI backup if required
  u8g.setHardwareBackup(u8g_backup_avr_spi);

  // assign default color value
  if ( u8g.getMode() == U8G_MODE_R3G3B2 ) {
    u8g.setColorIndex(255);     // white
  }
  else if ( u8g.getMode() == U8G_MODE_GRAY2BIT ) {
    u8g.setColorIndex(3);         // max intensity
  }
  else if ( u8g.getMode() == U8G_MODE_BW ) {
    u8g.setColorIndex(1);         // pixel on
  }
  else if ( u8g.getMode() == U8G_MODE_HICOLOR ) {
    u8g.setHiColorByRGB(255,255,255);
  }
  
  pinMode(8, OUTPUT);
}

void loop(void) {
  // picture loop
  u8g.firstPage();  
  do {
    draw();
  } while( u8g.nextPage() );
  
  // rebuild the picture after some delay
  delay(50);
}

However, after compiling I still get the follwoing error:

C:\Users\Marco\AppData\Local\Temp.arduinoIDE-unsaved2024614-11956-b00xqe.xvf5o\sketch_jul14a\sketch_jul14a.ino:7:26: error: redefinition of 'U8GLIB_SSD1306_128X32 u8g'
U8GLIB_SSD1306_128X32 u8g(10, 9); // HW SPI Com: CS = 10, A0 = 9 (Hardware Pins are SCK = 13 and MOSI = 11)
^
C:\Users\Marco\AppData\Local\Temp.arduinoIDE-unsaved2024614-11956-b00xqe.xvf5o\sketch_jul14a\sketch_jul14a.ino:6:23: note: 'U8GLIB_SSD1306_128X32 u8g' previously declared here
U8GLIB_SSD1306_128X32 u8g(13, 11, 10, 9); // SW SPI Com: SCK = 13, MOSI = 11, CS = 10, A0 = 9
^~~
C:\Users\Marco\AppData\Local\Temp.arduinoIDE-unsaved2024614-11956-b00xqe.xvf5o\sketch_jul14a\sketch_jul14a.ino:8:26: error: redefinition of 'U8GLIB_SSD1306_128X32 u8g'
U8GLIB_SSD1306_128X32 u8g(U8G_I2C_OPT_NONE); // I2C / TWI
^
C:\Users\Marco\AppData\Local\Temp.arduinoIDE-unsaved2024614-11956-b00xqe.xvf5o\sketch_jul14a\sketch_jul14a.ino:6:23: note: 'U8GLIB_SSD1306_128X32 u8g' previously declared here
U8GLIB_SSD1306_128X32 u8g(13, 11, 10, 9); // SW SPI Com: SCK = 13, MOSI = 11, CS = 10, A0 = 9
^~~

exit status 1

Compilation error: redefinition of 'U8GLIB_SSD1306_128X32 u8g'

Should´t I be uncommenting only one of these three options like shown below?

//U8GLIB_SSD1306_128X32 u8g(13, 11, 10, 9);	// SW SPI Com: SCK = 13, MOSI = 11, CS = 10, A0 = 9
//U8GLIB_SSD1306_128X32 u8g(10, 9);             // HW SPI Com: CS = 10, A0 = 9 (Hardware Pins are  SCK = 13 and MOSI = 11)
U8GLIB_SSD1306_128X32 u8g(U8G_I2C_OPT_NONE);	// I2C / TWI 

Like I said, start over, and post the requested details.

There should be only one of a line like this, not three, and the line you choose must be correct for your particular setup. Evidently you do not yet have enough understanding to make that choice.

U8GLIB_SSD1306_128X32 u8g(13, 11, 10, 9);	// SW SPI Com: SCK = 13, MOSI = 11, CS = 10, A0 = 9
U8GLIB_SSD1306_128X32 u8g(10, 9);             // HW SPI Com: CS = 10, A0 = 9 (Hardware Pins are  SCK = 13 and MOSI = 11)
U8GLIB_SSD1306_128X32 u8g(U8G_I2C_OPT_NONE);	// I2C / TWI 
1 Like

Yes, sorry. I forgot to mention I am using Arduino UNO board.
I started again with the following code and now I was able to compile and load without errors, but the display is not showing up.
The SCL and SDA pins of the OLED are connected directly to the SCL and SDA pins of the UNO board becasue that is what worked with a previous example using
"U8GLIB_SSD1306_128X32 u8g(U8G_I2C_OPT_NONE)", which is the option I left uncommented.

#include "U8glib.h"
// setup u8g object, please remove comment from one of the following constructor calls
// IMPORTANT NOTE: The following list is incomplete. The complete list of supported 
// devices with all constructor calls is here: https://github.com/olikraus/u8glib/wiki/device

//U8GLIB_SSD1306_128X32 u8g(13, 11, 10, 9);	// SW SPI Com: SCK = 13, MOSI = 11, CS = 10, A0 = 9
//U8GLIB_SSD1306_128X32 u8g(10, 9);             // HW SPI Com: CS = 10, A0 = 9 (Hardware Pins are  SCK = 13 and MOSI = 11)
U8GLIB_SSD1306_128X32 u8g(U8G_I2C_OPT_NONE);	// I2C / TWI 


void draw(void) {
  // graphic commands to redraw the complete screen should be placed here  
  u8g.setFont(u8g_font_unifont);
  //u8g.setFont(u8g_font_osb21);
  u8g.drawStr( 0, 22, "Hello World!");
}

void setup(void) {
  // flip screen, if required
  // u8g.setRot180();
  
  // set SPI backup if required
  // u8g.setHardwareBackup(u8g_backup_avr_spi);

  // assign default color value
  if ( u8g.getMode() == U8G_MODE_R3G3B2 ) {
    u8g.setColorIndex(255);     // white
  }
  else if ( u8g.getMode() == U8G_MODE_GRAY2BIT ) {
    u8g.setColorIndex(3);         // max intensity
  }
  else if ( u8g.getMode() == U8G_MODE_BW ) {
    u8g.setColorIndex(1);         // pixel on
  }
  else if ( u8g.getMode() == U8G_MODE_HICOLOR ) {
    u8g.setHiColorByRGB(255,255,255);
  }
  
  pinMode(8, OUTPUT);
}

void loop(void) {
  // picture loop
  u8g.firstPage();  
  do {
    draw();
  } while( u8g.nextPage() );
  
  // rebuild the picture after some delay
  delay(50);
}
1 Like

Thank you for your input. I changed the SDA and CLK wires to A4 and A5, unplugged, powered again, reloaded the program and it worked.
I will go through the references provided. Thank you!

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