STM32 Blue Pill with SH1107 OLED Display – I2C Mode

STM32 Blue Pill with SH1107 OLED Display – I2C Mode Example

This STM32 project shows how to interface the STM32 Blue Pill board with SH1107 monochrome OLED display module that has a size of 1.5-inch and resolution of 128×128 pixel.
The example shows how to print texts and draw some shapes (circles, rectangular…) on the SH1107 OLED display which is configured to work in I2C mode.

The STM32 Blue Pill development board is based on STMicroelectronics ARM Cortex-M3 microcontroller STM32F103C8T6 running at maximum clock frequency of 72MHz. This particular development board gained popularity due to its low cost and compact size, making it a popular choice for hobbyists, students, and developers.

To use the STM32 Blue Pill, a hardware programmer, such as ST-Link, is required to upload project code to the board. A USB-to-Serial converter module can also be used to upload the code to the board, an example of this converter is the popular one from FTDI which is FT232RL module.

SH1107 OLED display with STM32F103C8T6 Blue Pill hardware circuit

Abbreviations:
OLED: Organic light Emitting Diode.
PLED: Polymer OLED.
TFT: Thin Film Transistor.
I2C: Inter-Integrated Circuit.
SPI: Serial Peripheral Interface.
IoT: Internet of things.

The SH1107 OLED Display Module:
The SH1107 chip is a CMOS OLED/PLED driver with controller commonly used in small to medium-sized Organic/Polymer light-emitting diode dot-matrix display systems.
The SH1107 chip supports a maximum display resolution of 128×128 pixel and it is designed to drive monochrome OLED panels and is widely used in embedded systems, IoT devices, and consumer electronics.

The SH1107 OLED display module is a compact monochrome graphic display module that uses the SH1107 driver chip. These display modules are typically 128×64 or 128×128 pixels and are commonly available in I2C or SPI communication interfaces. These displays are commonly found in small, low-power devices as they have high contrast, wide viewing angles, and compact size. They are often used in projects involving microcontrollers like Arduino, ESP32, Raspberry Pi, and others.

Properties of the SH1107 OLED Displays Modules:

  • Resolution: Usually 128×64 or 128×128 pixels, smaller resolutions may be also available.
  • Display Type: Monochrome (one color only that usually white, blue, or yellow pixels on a black background).
  • Interface: Usually supports I2C and/or SPI communication with the I2C be the most common interface.
  • Operating Voltage: Usually 3.3V or 5V, depending on the module.
  • Power Consumption: Very low power usage as the display does not require backlight, making it ideal for battery-powered devices.

The SH1107 OLED display module used in this project is shown below:

SH1107 OLED display module I2C interface 128x128 pixel

The SH1107 OLED display shown above works with I2C interface protocol only. Below is the pinout for the display module, which is essential for connecting it to a microcontroller like Arduino, ESP32, Raspberry Pi, or Microchip PIC/dsPIC:

  • VCC: Power supply pin, may be 3.3V or 5V (refer to your display datasheet for power supply voltage range, some modules works only with 3.3V).
  • GND: Ground pin, connected to circuit common ground.
  • SCL: Serial Clock line of the I2C bus.
  • SDA: Serial Data line of the I2C bus.

The default I2C address of this module is 0x3C, but we can change it to 0x3D by shorting resistor R31, or removing resistor R30 (10k) and placing it on R31 (see the image below).
Also, if you’re using the same SH1107 display module shown above, it will be more better to change resistors R26 and R27 (1k each) by another ones not exceeding 100 Ohm (for example use 33 Ohm), or simply short each one of them.

SH1107 OLED change I2C address recommended actions PCB

Note that some SH1107 display modules supports both interfaces I2C and SPI where the user can choose between the two protocols with few changes in the module PCB (for example closing and/or opening jumpers).

Interfacing STM32 Blue Pill Board with SH1107 OLED Display – I2C Mode Example:
This post shows how to easily interface STM32 Blue Pill with SH1107 OLED display where the development board is connected to the display module via I2C interface.
Example circuit schematic is shown below.

Interfacing STM32 Blue Pill with SH1107 OLED display I2C circuit

The SH1107 OLED display module used in this project has 4 pins connected to the STM32 Blue Pill board as follows:
GND pin is connected to GND pin of the STM32 board.
VCC pin is connected to 3.3V pin of the STM32 board.
SCL (I2C Serial clock) pin is connected to pin B6 of the STM32 board.
SDA (I2C Serial data) pin is connected to pin B7 of the STM32 board.

Note that pins B6 and B7 of the STM32 Blue Pill board are hardware I2C pins of the microcontroller STM32F103C8T6, respectively for SCL and SDA lines.

Hardware Required:
This is a summary of the parts required to build this project.

Interfacing STM32 Blue Pill Board with SH1107 OLED Display Code:
Arduino IDE (Integrated Development Environment) is used to write project code, the STM32 Blue Pill board has to be added to the IDE before compiling the code.
The STM32 Blue Pill board can be installed using Arduino IDE Boards Manager.

The FT232RL USB to serial UART converter is used to program the STM32F103C8T6 microcontroller, the ST-LINK V2 programmer also can be used and it is supported by Arduino IDE.

To be able to compile project Arduino code, two libraries are required from Adafruit Industries:
The first library is a driver for the SH1107 OLED display and it can be installed from Arduino IDE library manager (Sketch —> Include Library —> Manage Libraries…, in the search box write “sh1107” and install the one from Adafruit).
The second library is Adafruit graphics library which can be installed also from Arduino IDE library manager.
During installation of the Adafruit SH1107 library, Arduino IDE may ask for installing some other libraries form Adafruit Industries (dependencies).

Project code was tested with the following library versions:
Adafruit GFX Library: Version 1.12.0.
Adafruit SH110X OLED display Library: Version 2.1.12.
Adafruit BusIO: Version 1.17.0.

Programming Hints:
The used libraries are included in the Arduino code as shown below:

 

 

The SH1107 display library is initialized with width of 128 pixel & height of 128 pixel, and I2C clock frequency of 400kHz:

 

 

The SH1107 OLED display must be initialized before any print operation, if the initialization failed then the display will show only black screen. The initialization function of the display is the one below with I2C address of 0x3C:

 

 

Note that the Arduino code given below is originally comes as an example with the Adafruit SH1107 library. Minor modification are made to the original code.

Full Arduino code:

 

 

Interfacing STM32 Blue Pill Board with SH1107 OLED Display Video:
The video below shows a test circuit of the STM32 Blue Pill board with SH1107 OLED display module (I2C interface).

Related Projects: