Setting up an OLED Screen with Arduino and SPI

Aeris
3 min readAug 23, 2016

and I are working on an Arduino project and we scraped together these bits of information from around the internet to finally get the screen to turn on! (Yay!) To save you the trouble we went through, here is everything we’ve learned. And I always appreciate feedback, so let me know if there is anything I can clarify or fix.

In this example, we are specifically using an Arduino Micro and an Adafruit Monochrome 128x64 OLED screen.

Arduino Micro Pins

To start, you should take a peek at the pin diagram from Arduino’s site. The pins that you care about are the following:

  • 3V — Provides 3.3V to power your screen
  • RS (Reset) — Allows you to build your own external reset button
  • MOSI (Master Out Slave In) — The Master line for sending data to the peripherals
  • MISO (Master In Slave Out) — The Slave line for sending data to the master
  • SCK (Serial Clock) — The clock pulses which synchronize data transmission generated by the master
  • CS (Chip Select or Slave Select) — the pin on each device that the master can use to enable and disable specific devices

Install Adafruit SSD1306 Library

Download the zip from GitHub. Rename the uncompressed folder Adafruit_SSD1306 and check that the Adafruit_SSD1306 folder contains Adafruit_SSD1306.cpp and Adafruit_SSD1306.h

Place the Adafruit_SSD1306 library folder your arduinosketchfolder/libraries/ folder. You may need to create the /libraries subfolder if its your first library. Restart the IDE. (1)

Install Adafruit GFX

Download the Adafruit GFX library next.

Rename the uncompressed folder Adafruit_GFX and check that the Adafruit_GFX folder contains Adafruit_GFX.cpp and Adafruit_GFX.h.

Place the Adafruit_GFX library folder your arduinosketchfolder/libraries/ folder like you did with the SSD1306 library. (1)

Adafruit SSD1306 Example Code

Open up the Arduino IDE. Navigate to File > Examples > Adafruit SSD1306 and you will see a list of examples. The examples are either for a 128x32 or 128x64 pixel screen. SPI refers to the Serial Peripheral Interface Bus, which is is a synchronous serial communication interface specification used for short distance communication. I2C refers to the Inter-Integrated Curcuit Protocal, which is a protocol intended to allow multiple “slave” digital integrated circuits (“chips”) to communicate with one or more “master” chips. Like SPI, it is only intended for short distance communications within a single device (2). I2C is also referred to as TWI, which is essentially the same bus implemented on various system-on-chip processors from Atmel and other vendors, but renamed for trademark reasons.

The Arduino Micro’s chip, ATmega32U4, supports both I2C (TWI) and SPI communication. The Arduino Software (IDE) includes a Wire library to simplify use of the I2C bus.

With this knowledge, open up the example ssd1306_128x64_spi.

To get this example to run, we need to edit the file Adafruit_SSD1306.h in the Adafruit SSD1306 library. Comment in the screen size that is appropriate for your setup from line 69–71. For us, we needed 128x64 as follows:

#define SSD1306_128_64
// #define SSD1306_128_32
// #define SSD1306_96_16

How to hook up your OLED Screen

The sample code lets you know which pins you’ll need to use. Comment out the block of code from line 24–30 for software I2C.

// If using software SPI (the 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(OLED_MOSI, OLED_CLK, OLED_DC, OLED_RESET, OLED_CS);

And then, comment in the code to use hardware SPI on lines 32–36.

/* Uncomment this block to use hardware SPI*/
#define OLED_DC 6
#define OLED_CS 7
#define OLED_RESET 8
Adafruit_SSD1306 display(OLED_DC, OLED_RESET, OLED_CS);

Finally, use the following configuration to hook up your screen.

And voila, your screen should light up! Let me know if these steps worked for you. Have fun!

--

--

Aeris

Will probably use this blog to write about video games.