Friday, May 27, 2016

Connect an SSD1306 OLED display to your STM32F103 board

Oled graphic screens are cheap to get from Chinese suppliers these days. For about 5USD, you can get a 1" sized monochrome display with 128x64 pixel resolution. The connection is also easy because they have I2C interfaces (or SPI; selectable), so only 4 wires, including power is all that's needed.


Display, displaying some text and rounded rectangle courtesy u8glib, ARM port.

STM32F103 is a very nice microcontroller; quite powerful, fast, easy to use, and there are many examples on various applications. This project shows how a popular SSD1306 OLED display can be connected to STM32F103, over I2C port 2, and using u8glib, by olikarus, at: https://github.com/olikraus/u8glib

There are many cheap development boards for the STM32F103 on the Internet, but another source of good development boards is to repurpose existing boards. I used the Naze32 (or one of its clones to be exact, the Flip 32), which come at a price of about US$20. It was designed for quadcopters and thus has many sensors integrated, voltage regulator clock etc. It is a good bargain. The SSD1306 OLED module is already running on Naze32, thanks to CleanFlight control program, and theoretically if you want the same functionality, you can pick out parts from CleanFlight. Unfortunately CleanFlight is quite large, and it was made to support many processors. It is easy to get lost in the source. So I decided to write my own.

The graphics library is u8glib by olikarus. it is hugely popular, but with one significant drawback: It was written for Arduino. There is an ARM port, (the version I used is: u8glib_arm1.18.1), but it requires you to write a few extensions for your particular processor, namely three delay functions, initialization of the connection peripheral, and a function to write to the display. These have been implemented in the files u8g_arm.c and u8g_arm.h. There are many similar samples on the Internet, but there seem to be differences in all, so I had to write them from scratch. Timer 2 is used for the delay functions.


 
The nice thing about this project is that the display can be easily connected to the board; there are only 4 wires to connect, and they are on matching 2.4mm header pins on the display and the board. Just buy the I2C display and the Flip32, and plug them together. It is therefore mainly a software project, and easy to build. The video shows the simplicity; a battery, the display and NAZE32. It can all be powered from a single LiPo cell (also visible in the video, but not connected), so quite convenient to build portable devices with. A small detail, the red LED flashes show when u8glib is active, so indicate the processor work load.

The sample code displays a few strings on the screen, draws a rounded rectangle around them, and animates them. U8glib allows for many more geometrical shapes and they can be easily inserted in your project with a few commands.
Download the source code at Github.

The project requires that GCC ARM Embedded is installed, together with the ARM port of u8glib and its fonts, as stated above. You should modify the Makefile to specify the respective tool locations. Specifically, the path to u8glib is specified in U8GLIB, and to the fonts directory in U8GFONT variables. How to install and use GCC cross compiler is shown in another blog post of mine.

This project was used as a part of a DIY aviation altimeter, which is at another blog post of minethat you can reach from this link.

3 comments:

  1. This comment has been removed by the author.

    ReplyDelete
    Replies
    1. Nice! Thank you very much for sharing the code, I tested and it worked flawlessly. Keep up the good work.

      Delete
  2. thank you for your interesting infomation. clear oled display

    ReplyDelete