Adafruit sells monochrome OLED displays with SPI and I2C interfaces. I have bought one for educational reasons. Hooking it up to an Arduino is quite simple as Adafruit provides a library and sample file. In this article I will document how I connected the display to the Raspberry PI.
The display can be interfaced using the Serial Peripheral Interface (SPI). Besides the SPI pins two GPIO pins are required — one for reset and one for data/command switch. The display operates at 3.3V and 5V. This means it can be connected directly to the Raspberry PI which operates at 3.3V. I have assembled a simple plug connector on a proto board so that I can bring out the header pins of the Raspberry PI board onto a breadboard.
Interconnecting the display and the Raspberry PI is straight forward:
|Raspberry PI Pin (function||Adafruit SSD1306 function|
I have test that the pins are correct by counting twice and by employing the wiringPi gpio tool and connecting an LED. The current must be limited by a resistor (e.g. 330 Ohm).
The Adafruit OLED display uses a Solomon Systech SSD1306. Somebody already implemented a port of the Adafruit Arduino library for the Raspberry PI. Their code only supports the 128 x 32 OLED. The application is also written in Python and requires wiringPi, the Python bindings for wiringPi as well as py-gaugette and these all have dependencies themselves. Because I wanted a pure C-library for the SSD1306 I reimplemented some of the stuff that was already in the Python library. My ssd1306 library was first based on wiringPi but again to keep the external dependencies to a minimum I have only taken the SPI communication code and have used the sample code provided on the eLinux Raspberry PI Wiki to control the GPIO pins.
When using the library no external build dependencies are required. Only
cmake are required besides the usual
build-essential package. This means I have kept the drawing capabilities at a minimum because I believe this should be handled by a different library. Right now the library is in early alpha stage and is provided as is.
Download the library from github. Then configure, compile and link the library and test applications:
pi@raspberrypi ~ $ git clone https://github.com/michaelKle/libssd1306.git
Cloning into 'libssd1306'...
remote: Counting objects: 26, done.
remote: Compressing objects: 100% (20/20), done.
remote: Total 26 (delta 6), reused 21 (delta 3)
Unpacking objects: 100% (26/26), done.
pi@raspberrypi ~ $ cd libssd1306/
pi@raspberrypi ~/libssd1306 $ mkdir build
pi@raspberrypi ~/libssd1306 $ cd build/
pi@raspberrypi ~/libssd1306/build $ cmake ..
-- The C compiler identification is GNU 4.6.3
-- The CXX compiler identification is GNU 4.6.3
-- Check for working C compiler: /usr/bin/gcc
-- Check for working C compiler: /usr/bin/gcc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Configuring done
-- Generating done
-- Build files have been written to: /home/pi/libssd1306/build
pi@raspberrypi ~/libssd1306/build $ make
Scanning dependencies of target ssd1306
[ 14%] Building C object src/CMakeFiles/ssd1306.dir/ssd1306.o
[ 28%] Building C object src/CMakeFiles/ssd1306.dir/rpiIO.o
Linking C shared library libssd1306.so
[ 28%] Built target ssd1306
Scanning dependencies of target ssd1306s
[ 42%] Building C object src/CMakeFiles/ssd1306s.dir/ssd1306.o
[ 57%] Building C object src/CMakeFiles/ssd1306s.dir/rpiIO.o
Linking C static library libssd1306s.a
[ 57%] Built target ssd1306s
Scanning dependencies of target testGPIO
[ 71%] Building C object src/CMakeFiles/testGPIO.dir/testGPIO.o
[ 85%] Building C object src/CMakeFiles/testGPIO.dir/rpiIO.o
Linking C executable testGPIO
[ 85%] Built target testGPIO
Scanning dependencies of target testSSD1306
[100%] Building C object src/CMakeFiles/testSSD1306.dir/testSSD1306.o
Linking C executable testSSD1306
[100%] Built target testSSD1306
When everything went like above you can try the test application:
pi@raspberrypi ~/libssd1306/build $ sudo src/testSSD1306
Starting SSD1306 Test
Using SPI Channel 0
Reset Pin = 17, DC Pin = 21
Display 10 white Boxes
Start Scrool Right
Start Scrool Left
sudo is required because
/dev/mem is accessed which requires root privileges. The test will first display 10 boxes. Then draw a line fan with a small delay between each line (caused by transmission time) and finally scroll the whole image to the right and then to the left. Then line fan looks something like this: