Interface SSD1306 OLED display with STM32
I already covered how to interface LCD 16×2 using I2C with STM32 and how to interface 1.3″ SH1106 oled display with STM32. Today I have another Oled display with me, It is a 0.96″ SSD1306 OLED display and we will interface it using I2C.
- This display uses the I2C protocol to communicate to the microcontroller. So here you need only 2 pins i.e. SDA and SCL from the microcontroller and the VCC and GND.
- I am using STM32f103c8t6 but the code will remain the same for all STM32 devices.
- I searched a lot for the libraries but all I could find were that of arduino. Than at last I found one by Alexander Lutsai, and I modified it so that it can be used with the STM32 CubeMx
CubeMX Configuration
Below is the image showing the I2C configuration.
- Set the I2C in the FAST MODE, as it is required for the OLED Display
- The SCL and SDA Pins will get automatically selected.
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 ssd1306.c and change the I2C handler according to your setup.
extern I2C_HandleTypeDef hi2c1;
#define SSD1306_I2C &hi2c1
Also configure the display resolution in the ssd1306.h file.
/* SSD1306 width in pixels */
#ifndef SSD1306_WIDTH
#define SSD1306_WIDTH 128
#endif
/* SSD1306 LCD height in pixels */
#ifndef SSD1306_HEIGHT
#define SSD1306_HEIGHT 64
#endif
You can see the functions available in the functions tab under ssd1306.c or in the ssd1306.h file.
Print simple strings
#include "ssd1306.h"
#include "fonts.h"
int main()
{
.....
SSD1306_Init (); // initialise the display
SSD1306_GotoXY (10,10); // goto 10, 10
SSD1306_Puts ("HELLO", &Font_11x18, 1); // print Hello
SSD1306_GotoXY (10, 30);
SSD1306_Puts ("WORLD !!", &Font_11x18, 1);
SSD1306_UpdateScreen(); // update screen
while (1) { }
}
- Here we will first initialise the display in the main function.
- Then set the cursor at x=10, y=10 and print “HELLO” at this location.
- Set the cursor at x=10, y=30 and print “WORLD !!” at this location.
- We need to call the function SSD1306_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 "ssd1306.h"
#include "fonts.h"
#include "stdio.h" // for sprintf
int main()
{
.....
int num = 123456; float flt = 123.45;
char bufnum[7]; char bufflt[7];
SSD1306_Init (); // initialise the display
sprintf (bufnum, "%d", num);
sprintf (bufflt, "%.2f", flt);
SSD1306_GotoXY (10,10); // goto 10, 10
SSD1306_Puts (bufnum, &Font_11x18, 1); // print Hello
SSD1306_GotoXY (10, 30);
SSD1306_Puts (bufflt, &Font_11x18, 1);
SSD1306_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=10, y=10 and print num at this location.
- Set the cursor at x=10, y=30 and print flt at this location.
- We need to call the function SSD1306_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 "ssd1306.h"
#include "fonts.h"
#include "stdio.h" // for sprintf
#include "bitmap.h"
int main()
{
.....
SSD1306_Init();
SSD1306_DrawBitmap(0, 0, logo, 128, 64, 1);
SSD1306_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=0,y=0.
- The size is 128pixels x 64pixels.
- We need to call the function SSD1306_UpdateScreen to update the display with the modified content.
Below is the image showing the output of the above code.
Scrolling and Animation
The SSD1306 has support for scrolling at hardware level. There are registers in the SSD1306 IC which enable the display to scroll in different directions.
The following are the functions available to do so.
#include "ssd1306.h"
#include "fonts.h"
#include "stdio.h" // for sprintf
#include "bitmap.h"
int main()
{
.....
SSD1306_Init();
SSD1306_DrawBitmap(0, 0, logo, 128, 64, 1);
SSD1306_UpdateScreen(); // update screen
SSD1306_ScrollRight(0x00, 0x07); // scroll entire screen (Page0 to Page7) right
HAL_Delay (5000);
SSD1306_Stopscroll();
SSD1306_ScrollLeft(0x00, 0x07); // scroll entire screen (Page0 to Page7) right
HAL_Delay (5000);
SSD1306_Stopscroll();
while (1) { }
}
In the code above, we are scrolling the entire screen in the right direction for 5 seconds, and then in the left direction for another 5 seconds. The screen contains the bitmap on it and therefore we will see the bitmap scrolling.
The Animation part is explained in the video shown below. Please check out the video to see the complete working.
Result