OLED display using I2C with STM32
I already covered how to connect LCD 16×2 using I2C in STM32. Today we will take a step forward and interface OLED display using I2C with STM32.
The display I will be using is a 0.96″ 128×64 oled display with black and blue colors.
- 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 as I mentioned before, the code will remain 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
Let’s see the Setup Process First.
Below is the image from the CubeMX
- Select the FAST MODE in the I2C, as it is required for the OLED Display
- The SCL and SDA Pins will get automatically selected.
- You can use the Pull Up register to these 2 Pins.
The Connection is as shown Below
Download the code below, unzip it, Copy the fonts.h, test.h and ssd1306.h in the Inc folder of your project, and fonts.c, test.c and ssd1306.c in the Src folder and open the project now.
Include the following files in the main.c:-
#include "ssd1306.h" #include "fonts.h" #include "test.h"
Open the ssd1306.c and change the handler accordingly
extern I2C_HandleTypeDef hi2c1;
You can see the functions available in the functions tab under ssd1306.c or in the ssd1306.h file. I am going to initialize the display and print “Hello World !!”…
SSD1306_Init (); // initialize 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
You can scroll the display LEFT, RIGHT, DIAGONAL LEFT or DIAGONAL RIGHT also. The following are the functions available to do so.
void SSD1306_ScrollRight(uint8_t start_row, uint8_t end_row); void SSD1306_ScrollLeft(uint8_t start_row, uint8_t end_row); void SSD1306_Scrolldiagright(uint8_t start_row, uint8_t end_row); void SSD1306_Scrolldiagleft(uint8_t start_row, uint8_t end_row); void SSD1306_Stopscroll(void);
The colour of the display can be inverted using
void SSD1306_InvertDisplay (int i);
i=1 will invert the display
i=0 will normalise it.
Drawing bitmap is also supported and the following function is assigned to do so
/** * @brief Draws the Bitmap * @param X: X location to start the Drawing * @param Y: Y location to start the Drawing * @param *bitmap : Pointer to the bitmap * @param W : width of the image * @param H : Height of the image * @param color : 1-> white/blue, 0-> black */ void SSD1306_DrawBitmap(int16_t x, int16_t y, const unsigned char* bitmap, int16_t w, int16_t h, uint16_t color);
We can also display the animation. Check out the video below, to see, how to do it.