Interface 1.3″ SH1106 OLED display with STM32
I have already covered how to interface 0.96″ SSD1306 Oled Display with STM32. Today I have another Oled display with me, It is a 1.3″ SH1106 OLED display and we will interface it using I2C.
- This display uses the I2C protocol to communicate to the microcontroller. So here we need only 2 pins i.e. SDA and SCL from the microcontroller along with VCC and GND.
- I am using STM32f103c8t6 but the code will remain the same for all STM32 devices.
CubeMX Configuration
Below is the image showing the I2C configuration.
- I am using I2C1 to interface the display.
- The I2C is configured in the FAST MODE. This is as required by the OLED Display
- The pins PB6 and PB7 are configured as SCL and SDA pins.
The Connection is as shown Below
As shown in the image above:
- The display is powered with 3.3V from the STM32 board itself.
- The SCL and SDA pins are connected to their respective counterparts on the board.
The Code
Open the SH1106.c and change the I2C handler according to your setup.
extern I2C_HandleTypeDef hi2c1;
#define SH1106_I2C &hi2c1
Also configure the display resolution in the SH1106.h file.
/* SH1106 width in pixels */
#ifndef SH1106_WIDTH
#define SH1106_WIDTH 128
#endif
/* SH1106 LCD height in pixels */
#ifndef SH1106_HEIGHT
#define SH1106_HEIGHT 64
#endif
The SH1106 IC supports 132×64 pixels. Therefore we would need to centre our 128×64 region within this range. In doing so, there will be an offset (of 2px) in the x-axis from both the ends. Although we do not need to worry about the offset in the end, but we definitely need to consider the offset in the beginning.
So if you want print something at x=0, instead you would need to print it at x=2.
You can see the functions available in the functions tab under SH1106.c or in the SH1106.h file.
Print simple strings
#include "SH1106.h"
#include "fonts.h"
int main()
{
.....
SH1106_Init (); // initialise the display
SH1106_GotoXY (12,10); // goto 10, 10
SH1106_Puts ("HELLO", &Font_11x18, 1); // print Hello
SH1106_GotoXY (12, 30);
SH1106_Puts ("WORLD !!", &Font_11x18, 1);
SH1106_UpdateScreen(); // update screen
while (1) { }
}
- Here we will first initialise the display in the main function.
- Then set the cursor at x=12, y=10 and print “HELLO” at this location. Considering the offset, the cursor is actually at 10,10.
- Set the cursor at x=12, y=30 and print “WORLD !!” at this location.
- We need to call the function SH1106_UpdateScreen to update the display with the modified content.
Below is the image showing the output of the above code.
Print Numbers
Any display does not support printing the numbers directly on it. Instead we need to change the number into the equivalent Ascii characters and then print it.
Below the code shows how to print integer and floats.
#include "SH1106.h"
#include "fonts.h"
#include "stdio.h" // for sprintf
int main()
{
.....
int num = 123456; float flt = 123.45;
char bufnum[7]; char bufflt[7];
SH1106_Init (); // initialise the display
sprintf (bufnum, "%d", num);
sprintf (bufflt, "%.2f", flt);
SH1106_GotoXY (12,10); // goto 10, 10
SH1106_Puts (bufnum, &Font_11x18, 1); // print Hello
SH1106_GotoXY (12, 30);
SH1106_Puts (bufflt, &Font_11x18, 1);
SH1106_UpdateScreen(); // update screen
while (1) { }
}
- Here we will first initialise the display in the main function.
- Then define the character buffers to store the converted integer and float values.
- The sprintf function is used to convert the integer(%d) and float(%f) values to the respective Ascii format.
- Then set the cursor at x=12, y=10 and print num at this location.
- Set the cursor at x=12, y=30 and print flt at this location.
- We need to call the function SH1106_UpdateScreen to update the display with the modified content.
Below is the image showing the output of the above code.
Bitmaps
We can draw a bitmap image anywhere on the display. We just need to convert the bitmap image to c array format. This tool can do that.
Make sure to choose the configuration as shown below.
Once you click Convert, the c file containing the image array will download. We will copy the array to our project.
#include "SH1106.h"
#include "fonts.h"
#include "stdio.h" // for sprintf
#include "bitmap.h"
int main()
{
.....
SH1106_Init();
SH1106_DrawBitmap(2, 0, logo, 128, 64, 1); // offset 2, so cursor is at 0,0
SH1106_UpdateScreen(); // update screen while (1) { }
}
- Here we will first initialise the display in the main function.
- Then draw the bitmap defined in the bitmap.h file.
- The bitmap start position is set to x=2,y=0.
- The size is 128pixels x 64pixels.
- We need to call the function SH1106_UpdateScreen to update the display with the modified content.
Below is the image showing the output of the above code.
Result