# Generic STM32F103C8T6 Board with SSD1306 OLED Display

Keeping moving forward with these cheap STM32F103C8T6 little boards…

Today, the challenge is to hook up a tiny 0.91″ 128×32 OLED displays featuring an SSD1306 controller that you can find on AliExpress for \$3.

With the IL9341, the SSD1306 is a very common controller for OLED displays (datasheet here). Hopefully, we don’t have to write everything from scratch, as Adafruit wrote an Arduino SSD1306 driver library available in their Github repository. Along with their higher level graphic library also available on Github, we have all the tools to draw nice graphics on these tiny OLED screens.

For standard AVR-based Arduino boards, these libraries work out of the box, they are even directly available in the Arduino IDE (search in the Library Manager for “SSD1306” and “GFX”):

For the Generic STM32F103C8T6 boards, making these work is a little bit more involved, though (we are sitting on the bleeding edge, are we?).

Because the SSD1306 library is relying on the standard “Wire” Arduino library to abstract out the underlying I2C bus complexity, using this OLED library directly on the STM32 that features 2x real hardware I2C bus results in a massive waste of efficiency (read: power consumption and processing power).

In order to get the most efficiency out of this board, a much better choice is to use the “Hardwire” library that implements the I2C interface using the dedicated hardware instead of software bit-banging..

Hopefully, this job has already been done in the Arduino STM32 package, even if there are a few bugs lurking around…

Locate the corresponding “ssd1306_128x64_i2c_STM32” project in the “Examples for Generic STM32F103C Series” menu:

As you noticed it from the project title, this example is aimed at the 128×64 version of the display, so we will have to change that to match our screen resolution of 128×32 pixels. This involves removing the stupid test located at lines 58-62 of this sketch:

#if (SSD1306_LCDHEIGHT != 64)
#endif

The effective choice of the screen resolution is located into the library header file “~/Arduino/hardware/Arduino_STM32/STM32F1/libraries/Adafruit_SSD1306/AdaFruit_SSD1306_STM32.h” that you need to edit using an external text editor around line 64.

The last problem is to correct a missing declaration for the “swap” macro in the main library file “~/Arduino/hardware/Arduino_STM32/STM32F1/libraries/Adafruit_SSD1306/AdaFruit_SSD1306_STM32.c” that you again need to edit using an external text editor. The change is located near line 105:

#define swap(a, b) { int16_t t = a; a = b; b = t; }

Back to the Arduino IDE, you should now be able to compile (“Verify” in awkward Arduino terminology) the sketch without other problem.

The Adafruit_SSD1306_STM32 library is set to use the second I2C interface on the STM32. According to the board pinout diagram, the wiring is as depicted below:

Here is a short demonstration video: