New Adafruit generic OLED display driver for Raspberry PI

  • After some years of using my SSD1306 library driver on Raspberry Pi, I needed to get it working with the new more and more popular 1.3″ I2C OLED driver that we can find for some bucks on Chinese ebay sellers.… Continue Reading

    Click here to see the full blog post

  • I'm trying to get your library to function. I've already created Python programs for the Adafruit SSD1306 in SPI mode by using the Adafruit python librarys. Now I'm trying to program the display in C++.

    But your wiring diagram differs from Adafruit's. What you're calling the RPi pinouts is actually different. Like the RST, DATA, CLK. If I forget the RPi pinout and just follow the name of the function, the only two things I had to change was DC from RPi 16 to 18, and RST from 18 to 22. Then all the named pins matched to Adafruit's pinout. But I don't get any output on my OLED.

    What am I doing wrong?

  • I meant I ignored your OLED pinout. Pins 1-8 on the OLED.

    Example; OLED pin 4, my OLED has CS written on it, but you're calling it DATA.
    My OLED pin 8 has DATA written next to it. You're calling it CS.

    Adafruit says DATA should go to RPi pin 19, which is what you say DATA should go to. But you're calling DATA on the OLED as pin 4.

    What is the REAL/CORRECT wiring for the RPi2?

  • Tom,

    The link you provided (ie this one refers to pin name printed on PCB, mine the pin that I call Pi P1 Header Pin is the pin number of the connector P1, not the number printed on the PCB (which is the GPIO name) take a look on the connector
    As You see Adafruit and mine are most of time the same (I refer to the P1 connector pin numbers in the circle in the previous image link).

    The only change For SPI, is that Adafruit connected RST to GPIO24 (P1 pin 18) and DC to GPIO23 (P1 pin 16) and mine is RST to GPIO25 (P1 pin 22) and DC to GPIO24 (P1 pin 18) so you just need to change these 2 ones in your wiring

    • RST to GPIO25 (P1 pin 22)
    • D/C GPIO24 (P1 pin 18)

    And this should be working fine

    PS : you can also keep Adafruit connection and change these 2 lines in the code

    #define OLED_SPI_RESET RPI_V2_GPIO_P1_22 /* GPIO 25 pin 22  */
    #define OLED_SPI_DC    RPI_V2_GPIO_P1_18 /* GPIO 24 pin 18  */


    #define OLED_SPI_RESET RPI_V2_GPIO_P1_18 /* GPIO 24 pin 18  */
    #define OLED_SPI_DC    RPI_V2_GPIO_P1_16 /* GPIO 23 pin 16  */

    in the file ArduiPi_SSD1306.h


  • Ok. I changed the Raspberry Pi wiring back to Adafruit. I can run Adafruit's Python examples okay. I changed those 2 lines in 'ArduiPi_OLED_lib.h'. I 'sudo make' the library files, and 'sudo make' the example. When I run 'sudo ./oled_demo -o 1' nothing happens.

    I put some 'printf' commands in the example for debugging, and it seems to lock up after it calls 'display.begin()'.

    I can't find where the function display.begin() is so I can diagnose/debug it further.

  • I downloaded the older ArduiPi_SSD1306 and tried that, but same thing. Freezes after it runs 'display.begin()'. I changed the RESET and DC in the .h file as you mentioned. I don't understand why I can't access the display in C++, but Python works perfectly. I'm using a Raspberry Pi 2 if that makes a difference, and an SSD1306 1.3" 128x64 purchased from Adafruit.

  • Update: I setup my OLED in I2C mode and changed the address to 3D, and it worked. But I would still rather use SPI mode.

  • That's an interesting point, I did not tried on a PI V2, I have one at home, I need to test just in case, because the driver relies on old BCM2835 library that may be I need to update for PI V2, worth checking.

    Good to know it works with I2C but I understand you prefer SPI, for sure it's so much faster.

  • Hi, Charles. Any progress with testing the library on a PI v2? Is the BCM2836 extremely different than the BCM2835?

  • Thank you for the detailed blog post. I followed your instructions and got 2 problems :

    1. there is no libi2c available, the only thing similar I've found is libi2c-dev so I installed that;
    2. I got error messages while trying to "sudo make" within the example folder, here is a photo showing the result.

    Btw I am on a Pi B V2. One strange thing is that when I ran "sudo i2cdetect -y 1" there is nothing on the table. But while I tried to use Adafruit_SSD1306 library, the oled would respond (weirdly).


  • @Meng-Qi

    the i2c smbus_ is a know problem

    you just need to intall again lm-sensors package (even if correctly installed) then all shoud go fine. Something obvious I’d like to understand, but it seems to work.

    look below how I resolved it.

    root@pi03:~/github/ArduiPi_SSD1306/examples# make
    g++ -Ofast -mfpu=vfp -mfloat-abi=hard -march=armv6zk -mtune=arm1176jzf-s -Wall  -lssd1306 ssd1306_demo.cpp -o ssd1306_demo
    /usr/local/lib/ undefined reference to `i2c_smbus_write_i2c_block_data'
    /usr/local/lib/ undefined reference to `i2c_smbus_write_byte_data'
    /usr/local/lib/ undefined reference to `i2c_smbus_write_word_data'
    collect2: ld returned 1 exit status
    make: *** [ssd1306_demo] Error 1
    root@pi03:~/github/ArduiPi_SSD1306/examples# dpkg --get-selections | grep i2c
    i2c-tools                                       install
    libi2c-dev                                      install
    root@pi03:~/github/ArduiPi_SSD1306/examples#  dpkg --get-selections | grep lm
    lm-sensors                                      install
    root@pi03:~/github/ArduiPi_SSD1306/examples# apt-get install lm-sensors
    Reading package lists... Done
    Building dependency tree
    Reading state information... Done
    lm-sensors is already the newest version.
    The following packages were automatically installed and are no longer required:
      libblas3gf liblapack3gf
    Use 'apt-get autoremove' to remove them.
    0 upgraded, 0 newly installed, 0 to remove and 14 not upgraded.
    root@pi03:~/github/ArduiPi_SSD1306/examples# make clean
    rm -rf ssd1306_demo teleinfo-oled
    root@pi03:~/github/ArduiPi_SSD1306/examples# cd ..
    root@pi03:~/github/ArduiPi_SSD1306# make clean
    rm -rf *.o libssd1306.* /usr/local/lib/libssd1306.*
    root@pi03:~/github/ArduiPi_SSD1306# make
    g++ -Wall -fPIC -fno-rtti -Ofast -mfpu=vfp -mfloat-abi=hard -march=armv6zk -mtune=arm1176jzf-s -c Adafruit_SSD1306.cpp
    ./ArduiPi_SSD1306.h:51:21: warning: ‘oled_type_str’ defined but not used [-Wunused-variable]
    g++ -Wall -fPIC -fno-rtti -Ofast -mfpu=vfp -mfloat-abi=hard -march=armv6zk -mtune=arm1176jzf-s -c Adafruit_GFX.cpp
    ./ArduiPi_SSD1306.h:51:21: warning: ‘oled_type_str’ defined but not used [-Wunused-variable]
    gcc -Wall -fPIC -Ofast -mfpu=vfp -mfloat-abi=hard -march=armv6zk -mtune=arm1176jzf-s -c bcm2835.c
    g++ -shared -Wl,-soname, -Ofast -mfpu=vfp -mfloat-abi=hard -march=armv6zk -mtune=arm1176jzf-s  -o Adafruit_SSD1306.o Adafruit_GFX.o bcm2835.o
    [Install Library]
    [Install Headers]
    root@pi03:~/github/ArduiPi_SSD1306# cd examples/
    root@pi03:~/github/ArduiPi_SSD1306/examples# make
    g++ -Ofast -mfpu=vfp -mfloat-abi=hard -march=armv6zk -mtune=arm1176jzf-s -Wall  -lssd1306 ssd1306_demo.cpp -o ssd1306_demo
    g++ -Ofast -mfpu=vfp -mfloat-abi=hard -march=armv6zk -mtune=arm1176jzf-s -Wall  -lssd1306 teleinfo-oled.cpp -o teleinfo-oled

    For the i2cbus detection, nothing to do with my lib, i2cdetect is a tool distribued with your linux distro.

    So to be sure I've just gone on my PI V2 to test

    root@pi01(rw):~/ArduiPi_OLED/examples# i2cdetect -y 1
         0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
    00:          -- -- -- -- -- -- -- -- -- -- -- -- --
    10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    30: -- -- -- -- -- -- -- -- -- -- -- -- 3c -- -- --
    40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    70: -- -- -- -- -- -- -- --
    root@pi01(rw):~/ArduiPi_OLED/examples# ./oled_demo -o 3
    x: 9y: 0dy: 3
    x: 30y: 0dy: 2
    x: 111y: 0dy: 2
    x: 81y: 0dy: 4
    x: 41y: 0dy: 2
    x: 85y: 0dy: 5

    So @Mr_Tom yes it works on Pi V2 (at least in I2C mode) but should be the same for SPI; look below


  • I've got the lm-sensors module in there, and the make on examples worked.

    But only still I can't get my Pi to recognize my oled screen, and I have another device on the i2c bus which works perfectly.
    I just ordered another oled (a seeed 96*96) to see if that would work.

    Thank you for your detailed reply!

  • Meng,

    Even if Seeed OLED will be seen, it won't work because it"s another driver in it (SSD1308) and I didn't port this one in the example code, something I wanted to do but never had time do do it.

    By the way Seeed OLED 96x96 is the best OLED I ever saw, excellent display and gray level.

  • Meng
    did you tried your non working OLED on an Arduino with I2CScan sketch just to see ?

  • Hi, Charles,

    I've tested my oled with I2CScan, and it wasn't detected... maybe a broken one. And I have another oled screen here which can be detected on Arduino, but not on Pi.

    Here is a library by Seeed themselves :
    I'll see if that would work once the oled screen arrives. Happy to know that it looks amazing.

  • Meng,
    yeah Seeed library works fine on Arduino, but not sure there is a port on Raspberry Pi

  • This 96*96 oled screen is detected by my Pi and it is working with Seeed's own library, and it would also put out stuff while running your oled_demo.

    Does your library has a manual? It doesn't looks like python when I peek into your library files... Sorry I am quite a beginner. Thank you.

    I use my Pi to generate sound, and if I do oled operations within the same piece of .py file that does sound, any oled writing operations would block the sound. But if I open 2 terminal instances (I am using SSH), and run sound code on one, oled code on another, it wouldn't block the sound. So it is certainly not a CPU speed issue...

    Why does this happen? And if I make my Pi running some python code at boot, how I can make the codes running like within multiple terminals?

    Thank you!

  • I also try to run it on RPi2 with an SPI OLED 128x64 and ran into the same problem. The program freezes in display.begin(). What can be wrong?

  • Hi gbona,
    Did you checked that your I2C oled is visible with i2cdetect ? If so does his I2C address is 0x3C ?