I am relatively new to the Arduino environment, and I recently bought a 1.3" 128x64 OLED Monochrome display, and when I tested some code, the screen was filled with a mostly white screen with scattered black spots. I am almost certain that my wiring is correct (GND - ground, VCC - 5v, CLK - 11, MOSI - 9, RES - 13, DC - 11 and CS - 12). The following is my code:
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
// Declaration for SSD1306 display connected using software SPI (default case):
#define OLED_MOSI 9
#define OLED_CLK 10
#define OLED_DC 11
#define OLED_CS 12
#define OLED_RESET 13
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT,
OLED_MOSI, OLED_CLK, OLED_DC, OLED_RESET, OLED_CS);
void setup() {
Serial.begin(9600);
// initialize OLED display with address 0x3C for 128x64
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
while (true);
}
delay(2000);
display.setTextSize(2); // text size
display.setTextColor(WHITE); // text color
display.setCursor(0, 10); // position to display
display.println("Hello World!"); // text to display
display.display(); // show on OLED
}
void loop() {
}
Thank you for your help, however, after I had downloaded the required library from here - (U8glib - Arduino Reference), there are a lot of error messages and it didn't compile altogether. Since the error was about "mui.h" I removed the MUIU8g2 library, and the error messages became:
Arduino: 1.8.12 (Mac OS X), Board: "Arduino Mega or Mega 2560, ATmega2560 (Mega 2560)"
ssd1306_128x64_spi:5:1: error: 'U8GLIB_SH1106_128X64' does not name a type; did you mean 'U8X8_KS0108_128X64'?
U8GLIB_SH1106_128X64 u8g(13, 11, 10, 9); // SCK = 13, MOSI = 11, CS = 10, A0 = 9
^~~~~~~~~~~~~~~~~~~~
U8X8_KS0108_128X64
/var/folders/76/2830y9d144l2_vcf2spgwhk40000gp/T/arduino_modified_sketch_69896/ssd1306_128x64_spi.ino: In function 'void draw()':
ssd1306_128x64_spi:8:2: error: 'u8g' was not declared in this scope
u8g.setFont(u8g_font_unifont);
^~~
/var/folders/76/2830y9d144l2_vcf2spgwhk40000gp/T/arduino_modified_sketch_69896/ssd1306_128x64_spi.ino:8:2: note: suggested alternative: 'u8'
u8g.setFont(u8g_font_unifont);
^~~
u8
/var/folders/76/2830y9d144l2_vcf2spgwhk40000gp/T/arduino_modified_sketch_69896/ssd1306_128x64_spi.ino: In function 'void setup()':
ssd1306_128x64_spi:14:1: error: 'u8g' was not declared in this scope
u8g.setRot180();
^~~
/var/folders/76/2830y9d144l2_vcf2spgwhk40000gp/T/arduino_modified_sketch_69896/ssd1306_128x64_spi.ino:14:1: note: suggested alternative: 'u8'
u8g.setRot180();
^~~
u8
ssd1306_128x64_spi:18:24: error: 'U8G_MODE_R3G3B2' was not declared in this scope
if ( u8g.getMode() == U8G_MODE_R3G3B2 ) {
^~~~~~~~~~~~~~~
ssd1306_128x64_spi:21:29: error: 'U8G_MODE_GRAY2BIT' was not declared in this scope
else if ( u8g.getMode() == U8G_MODE_GRAY2BIT ) {
^~~~~~~~~~~~~~~~~
ssd1306_128x64_spi:24:29: error: 'U8G_MODE_BW' was not declared in this scope
else if ( u8g.getMode() == U8G_MODE_BW ) {
^~~~~~~~~~~
/var/folders/76/2830y9d144l2_vcf2spgwhk40000gp/T/arduino_modified_sketch_69896/ssd1306_128x64_spi.ino:24:29: note: suggested alternative: 'LB_MODE_1'
else if ( u8g.getMode() == U8G_MODE_BW ) {
^~~~~~~~~~~
LB_MODE_1
ssd1306_128x64_spi:27:29: error: 'U8G_MODE_HICOLOR' was not declared in this scope
else if ( u8g.getMode() == U8G_MODE_HICOLOR ) {
^~~~~~~~~~~~~~~~
/var/folders/76/2830y9d144l2_vcf2spgwhk40000gp/T/arduino_modified_sketch_69896/ssd1306_128x64_spi.ino: In function 'void loop()':
ssd1306_128x64_spi:32:2: error: 'u8g' was not declared in this scope
u8g.firstPage();
^~~
/var/folders/76/2830y9d144l2_vcf2spgwhk40000gp/T/arduino_modified_sketch_69896/ssd1306_128x64_spi.ino:32:2: note: suggested alternative: 'u8'
u8g.firstPage();
^~~
u8
exit status 1
'U8GLIB_SH1106_128X64' does not name a type; did you mean 'U8X8_KS0108_128X64'?
This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.
Thank you to everyone who has helped me in my problem. As instructed in the user manual, the required library was U8glib, rather than the U2g2 library I had accidentally downloaded, as it was the first result when I searched "U2glib" in the manage libraries section. The OLED turned out to be a SH1106, rather than a SSD1306 that I had initially assumed it was. Thank you again for all your help.