How to get an OLED SPI Working with Arduino Mega?

Hello,

I have a 0.96inch OLED display from Waveshare (https://www.waveshare.com/wiki/0.96inch_OLED_(B)). I would like to make it work with my Arduino Mega through the 4 wire SPI protocol. I am very new to Arduino. I have connected the DC pin on the OLED to digital pin 53, CS to digital pin 50, CLK to digital pin 52, DIN to digital pin 51, GND to GND, VCC to 5v, and RES to the RESET pin beside the 3.3v pin.

Below is the test file I am working with (and I have not modified the ssd1306 files). Thanks!

#include <SPI.h>
#include <Wire.h>
#include "ssd1306.h"

#define VCCSTATE SSD1306_SWITCHCAPVCC
#define WIDTH     128
#define HEIGHT     64
#define PAGES       8

#define OLED_RST   30 
#define OLED_DC    53
#define OLED_CS    50 
#define SPI_MOSI   51
#define SPI_SCK    52

uint8_t oled_buf[WIDTH * HEIGHT / 8];

void setup() {
  Serial.begin(9600);
  Serial.print("OLED Example\n");

  /* display an image of bitmap matrix */
  SSD1306_begin();
  SSD1306_clear(oled_buf);
  SSD1306_bitmap(0, 0, Waveshare12864, 128, 64, oled_buf);
  SSD1306_display(oled_buf);
  delay(2000);
  SSD1306_clear(oled_buf);

  /* display images of bitmap matrix */
  SSD1306_bitmap(0, 2, Signal816, 16, 8, oled_buf); 
  SSD1306_bitmap(24, 2,Bluetooth88, 8, 8, oled_buf); 
  SSD1306_bitmap(40, 2, Msg816, 16, 8, oled_buf); 
  SSD1306_bitmap(64, 2, GPRS88, 8, 8, oled_buf); 
  SSD1306_bitmap(90, 2, Alarm88, 8, 8, oled_buf); 
  SSD1306_bitmap(112, 2, Bat816, 16, 8, oled_buf); 

  SSD1306_string(0, 52, "MUSIC", 12, 0, oled_buf); 
  SSD1306_string(52, 52, "MENU", 12, 0, oled_buf); 
  SSD1306_string(98, 52, "PHONE", 12, 0, oled_buf);

  SSD1306_char3216(0, 16, '1', oled_buf);
  SSD1306_char3216(16, 16, '2', oled_buf);
  SSD1306_char3216(32, 16, ':', oled_buf);
  SSD1306_char3216(48, 16, '3', oled_buf);
  SSD1306_char3216(64, 16, '4', oled_buf);
  SSD1306_char3216(80, 16, ':', oled_buf);
  SSD1306_char3216(96, 16, '5', oled_buf);
  SSD1306_char3216(112, 16, '6', oled_buf);

  SSD1306_display(oled_buf); 
}

void loop() {

}

I strongly advise you to use Adafruit_SSD1306 or U8g2lib libraries. Both are available via the IDE Library Manager.

Run all the library examples. (with the default library wiring or by editing the constructor to match your wiring.

The "ssd1306" library from Waveshare is a bit crap.
Either use the Waveshare wiring i.e. pins 8, 9, 10, 11, 13
Or edit ssd1306.h to match your wiring.

There are 1000s of programs out there for Adafruit or U8g2
Very few for Waveshare "ssd1306.h"

If you have a problem:

  1. quote which library
  2. quote which library example
  3. paste any "changed" lines to your message.
  4. photo of your actual wiring.

David.

Ok I will try those libraries out and I make sure to include all of that information if I have any future issues. Thanks.