Library 61- SSD1306 OLED I2C LCD for STM32F4xx

Yeah, I got it too. These little, small and with nice contrast devices are really great.

I2C communication from my module isn’t really powerful because of slow I2C, even at 400kHz. To update full LCD, there are 1024 bytes of data to be transfered. And this can really take a while. But anyway, for having this LCD just to showing small updates and notifications for users in your project, I2C is still nice. Probably I should make it using I2C DMA transfer.

The heart of this OLED LCD is SSD1306 driver. Driver itself allows also some other communications like 3-wire SPI or 4-wire SPI as well as 6080 parallel interface any maybe more. Parallel interface is really not necessary here because of small amount of data to be transfered since each pixel is a bit in a bytes area.

In my LCD, I don’t like the fact that about 1/3 of LCD is yellow when pixel is ON and the rest is blue. This looks really crazy and not so useful. Anyway, library is here and ready to use.

Since this LCD is very small, LCD rotation is not possible with this library. By having interest in this feature I will add this too.

Library

Features

  • Draw strings to LCD at any position you want
  • Draw graphic things like lines, rectangles and circles
  • Invert pixels in one function call
  • Data for LCD are stored in STM’s ram, after each change, UPDATE command is necessary to get changes on LCD

Dependencies

  • CMSIS
    • STM32F4xx
    • STM32F4xx I2C
  • TM
    • TM GPIO
    • TM I2C
    • TM FONTS
    • defines.h
  • C
    • string.h
    • stdlib.h
SSD1306 STM32F4 Description
VCC 3.3V
GND GND
SCL PA8 Serial clock line
SDA PC9 Serial data line

LCD works on I2C and it has options to select custom I2C address by changing BIT1 in I2C address. Default address (also used in this library) is:

Functions and enumerations

Example

SSD1306 OLED I2C LCD with STM32F4

SSD1306 OLED I2C LCD with STM32F4



Project is available on my Github, download library below.

Icon
TM STM32F4 SSD1306 OLED Library

tilz0R

Owner of this site. Also electronic enthusiasts, web developer, 3D printer fan, handball player and more. Big fan of STM32F4 devices. In anticipation of the new Discovery board for STM32F7 lines.

You may also like...

Read before commenting!

Before you make a new comment, make sure you agree with things listed below:

  • - Read post to make sure if it is already posted what you are asking for,
  • - Make sure you have the latest version of libraries used in your project,
  • - Make a clean and grammatically correct written message,
  • - Report as many details as possible, including what have you done so far,
  • - Do NOT post any code here. Use Pastebin,
  • - Do NOT post any error codes here. Use Pastebin,
  • - Specify STM32Fxxx family and used Discovery/EVAL/Nucleo or custom made board,
  • - Make sure your clock is set correct for PLL,
  • - If you are using my HAL drivers, please check this post how to start.
Comment will be deleted on breaking these rules without notification!
  • za_el

    Hi,

    First of all god job (y) 😉

    I’m getting started with STM32F4 discovery board, and i’m working in a project which i want to display image on a LCD TFT http://www.displaytech-us.com/2-4-inch-tft , with ILI9341 controller. Could you please help me about how can’i interface the board and the TFT LCD screen.
    Thank you ^^

    • Well, If you take a look at my website, maybe you will find what you need 😉

      • za_el

        thanks for reply ^^
        i’m in it for a long time 😉
        but i think you use STM32F429 board so is not the MCU :/

        • Read site again please.
          Do not post anymore about ILI9341 here.

          • za_el

            ok i found it thanks 😉 and sorry about a mess 😀

  • Miha Medved

    Hi,

    I followed your post and at the end, when I built project get error like this:

    In function I2C_Init':

    [cc] D:stmcoocoxcmsis_libsource/stm32f4xx_i2c.c:196: undefined reference to RCC_GetClocksFreq’

    [cc] collect2.exe: error: ld returned 1 exit status

    could you please send me whole project, with all included libraries. Thank you,

    Miha

    • Hi Miha, probably from Slovenia? if so, we can speak in Slo language 😉

      You are missing stm32f4xx_rcc.c function in your project.

      • Miha Medved

        zdravo tilen,

        kot si napisal, vse drži. Sem iz Laškega in res mi je manjkal file stm32f4xx_rcc. Programček lepo dela. Hvala za pomoč.

        PS: če si na p&c se lahko dobima na kakšnem piru 🙂

  • abdurrahman korkmaz

    Hi Friend , please help about qc12864b lcd.

    • I’m thinking..would you mind to tell me what to help you?

      • abdurrahman korkmaz

        i couldnt run qc12864 lcd . i tried a lot of lib .

        • So, what you’ve done so far? What is not working? communication, register writing, reading?

          • abdurrahman korkmaz

            is there any library for qc12864b lcd?

          • Waiting your answer.

          • abdurrahman korkmaz

            it means library61 can be use for qc12864b or ST7920 lcd. am i right?

          • How you come to this decision? Why would SSD1306 be the same as your QC…?

          • abdurrahman korkmaz

            im only looking for a library for st7920 lcd? please help me.i dont know how im looking for.

          • I’m still waiting for your answer 😉

  • abdurrahman korkmaz

    any library about qc12864b lcd

  • Tyrion lannister

    How to display pictures for ssd1306? Any help will be much appreciated

    • You wilk have to convert them to bytes and save as array to flash.

  • Salman Khaja

    Hello, how do you created fonts for this project. I need to add my own fonts to the project.

    Also, can you please explain the algorithm used for writing the ” TM_SSD1306_Putc() ” function in tm_stm32_ssd1306.c file?

    • I used fonts like on my all other LCD libraries. Fonts were generated some time ago with one small program, found on web.

      What exactly you wanna know here?

      • Salman Khaja

        Thanks for reply.

        What I actually wanna know is, how that function works? It seems confusing to understand.

        • I think, it is pretty straight-forward. Code is taking one fond and draws pixel where bit is set.

          • Salman Khaja

            OK, thanks.

  • Kyaw Kyaw Aung

    Hi, TM. For STM32F4 Discovery, I’d like to display the ADC values on screen. Since you said that this board is slow because of I2C to update the full board, is there any OLED boards that can make the refresh rate faster?

    • The idea is, to find SPI lcd. But you can use this for start 🙂

      • Kyaw Kyaw Aung

        Hello again, TM. I just bought a display with the same driver (SSD1306) and it has 2 more pins (RST and D/C for SPI communication). However, if I wanna try with I2C (exactly your library), should I just leave the pins floating or should I connect the pins to some voltage? Thank you so much. 🙂

        • Thb, Im not sure. Never tried this so far. I suggest you you take a schematics of i2c lcd and set pins like there are.

          • Kyaw Kyaw Aung

            Alright. thank you very much.

          • Kyaw Kyaw Aung

            Hi, TM. sorry to bother you again but could you give me some hints to implement this driver with your SPI library? Thanks in advance.

  • SirVolta

    Hello,
    Many thanks for the library. It was quickly ported to STM32F103xx and works perfectly.
    But blocking the CPU while all of the 1024 bytes of the framebuffer transfer is really not an option so I implemented DMA for I2C transmission.
    As you have already stated this is a much better solution and practically free. Unless you really need all of those DMA channels of course 😉
    I’ve also implemented a shift framebuffer function that shifts the content of the framebuffer up n pixels. This is very useful for logging text and basic command lines.
    If anyone is interested, I can make the source code available. Do note that it is for STM103xx. Other controllers will need some adjustment.

    • I can publish it here if you send it to me to my email address.

      • SirVolta

        I have added it to my github and edited the link into my first comment

  • Entropy

    Greetings from all the way to New Zealand again, well today I decided to give a try one of my TFT 128×128 OLED LCD i have had for sometimes now using your library here but i don;t seem to have any luck maybe if you could point out where i am doing wrong that would be appreciated, the LCD was bought last year here

    http://www.aliexpress.com/item/1-44-inch-TFT-SPI-serial-module-full-color-TFT-replacement-OLED/32622819277.html

    As you can see it is an SPI based with the following pins:

    1.3.3V
    2.GND
    3.SDI SPI data input
    4.SCL SPI Clock
    5.CS SPI Chip Select
    6.RST Reset
    7.RS screen module command / data signal input selection
    8.BLK backlight control signal input

    having tested BLK pins if pulled low it dims the OLED, while i don’t know for sure what RS pin really does but being familiar with HDD44780 having the same pin name, prob makes it “Register Select”? not sure and can’t imagine why the LCD with SPI capability would need one anyway.

    could you provide some means in which this I2C library could be compatible / ported to similar SSD13xx SPI-based OLED LCD?

    Many thanks.

    • Entropy

      since i read that the SSD13xx series have both SPI and I2C capability, i tried to see if i would have any luck with using PC9 to SDI pin and PA8 to SCL as your library state here, but nothing was displayed on the LCD. I have also update the “defines” to 128 by 128 scale accordingly.

      • 1. I don’t see your LCD is SSD1306,
        2. I don’t see datasheet at all for this LCD,
        3. It’s not all to change resolution and will work.

  • Zero Tiger

    Thaks for your great work, I want to get more big font size , so would you share font generate program( below explaned

    ->
    I used fonts like on my all other LCD libraries. Fonts were generated some time ago with one small program, found on web.)