Interfacing I2C LCD With STM32 BllackPill Using STM32CubeIDE

by tanushree4587be23 in Circuits > Microcontrollers

21 Views, 1 Favorites, 0 Comments

Interfacing I2C LCD With STM32 BllackPill Using STM32CubeIDE

stm32f411-blackpill-development-board-dfrobot-dfr0864-41091705733315_800x.jpg

For any microcontroller project, interfacing a display unit with it would make the project a lot easier and appealing for the user to interact with. The most commonly used display unit for microcontrollers is the 16×2 Alpha numeric displays. These types of displays are not only useful to display vital information to the user but can also act as a debugging tool during the initial developmental stage of the project. So, in this tutorial we will learn how we can interface a 16×2 LCD display with the STM32F401CEUX STM32 Development board and program it using the STM32CubeIDE. For people who are familiar with STM32CubeIDE this tutorial will just be a cake walk since they both are very similar.

Brief Intro to 16×2 Dot matrix LCD display

As told earlier IDE provides a beautiful library which makes the interfacing a piece of cake and hence it’s not mandatory to know anything about the display module. But, would didn’t it be interesting to show what we are using!! 

The name 16×2 implies that the display has 16 Columns and 2 Rows, which together (16*2) forms 32 boxes. One single box would look something like this in the picture below

A single box has 40 pixels (dots) with a matrix order of 5 Rows and 8 columns, these 40 pixels together forms one character. Similarly, 32 characters can be displayed using all the boxes. Now lets take a look at the pinouts.

The LCD has a total of 16 Pins, as shown above, they can be categorized into four groups like as follows

Source Pins (1, 2 and 3): These pins source the power and contrast level for the display

Control Pins (4, 5 and 6): These pins sets/controls the registers in the LCD interfacing IC (more this can be found in link below)

Data/Command Pins (7 to 14): These pins provide the data of what information should be displayed on the LCD.

LED pins (15 and 16): These pins are used to glow the backlight of LCD if needed (optional).

Out of all these 16 pins, only 10 pins are to be used mandatory for the proper working of the LCD.


Supplies

  1. STM32 Black Pill
  2. LCD with I2C
  3. Female to Female Jumpers
  4. STMCubeIDE
  5. STMCubeProgrammer
  6. USB C Cable

Downloading

The liquidcrystal_i2c.h library emerges as a crucial tool in facilitating this communication, particularly when interfacing I2C LCDs with microcontrollers such as the STM32 Black Pill. By abstracting the intricacies of low-level I2C protocol implementation, this library simplifies the development process, enabling developers to concentrate on higher-level functionality and user experience. Its integration streamlines code maintenance and encourages code reusability, rendering it an indispensable asset for projects demanding dependable and efficient LCD control. With liquidcrystal_i2c.h, developers can seamlessly incorporate I2C LCDs into their projects, empowering them to craft sophisticated user interfaces and elevate overall system functionality.

download the file from the folder given below.


Configure Your STM Cube IDE

F73C1QALUPJA1A1.png
FA8NV4VLUPJA1B0.png

We will use STM32Cube IDE to program our STM32 board. Open the IDE and move over to a new project.

choose the STM32 BlackPill board. Then, select any column. click the 'Next' button.

PIN Mode Selection

FN3BZ65LTR8Q0IL.png

Select Pin B6 and B7 for SCL and SDA respectively

I2C Configuration

FUMTAL5LTMYDVQ4.png

Configure the I2C mode to I2C under Connections Menu

Configuring the Clock

FI9U5UVLTMYDVSV.png

Adding the Code

#include "main.h"

#include "liquidcrystal_i2c.h"


I2C_HandleTypeDef hi2c1;


void SystemClock_Config(void);

static void MX_GPIO_Init(void);

static void MX_I2C1_Init(void);


int main(void)

{

HAL_Init();

SystemClock_Config();

MX_GPIO_Init();

MX_I2C1_Init();


HD44780_Init(2);

HD44780_Clear();

HD44780_SetCursor(0,0);

HD44780_PrintStr("TANUSHREE");

HD44780_SetCursor(0,1);

HD44780_PrintStr("GUPTA");

HAL_Delay(2000);

}

Debug and Building Binary File

FDKL9V8LTMYDVZ0.png

Click on debug (On top left corner) to generate binary file.

Open the File Created in Explorer

FZL3X5JLTR8Q0KD.png

click on the project and click on show in system explorer.

Open STMCubeProgrammer and Connect With STM Board Via USB

FIGEBK3LTR8Q0NU.png

Click the Open File and Paste the Copied Path

FLMI3M5LTR8Q0OV.png

Go to Download Options

F4FINE2LTR8Q0QR.png

Modify Download Options and Click on "Start Automatic Mode"

FTPKECBLTR8Q0U3.png

DONE !

WhatsApp Image 2024-03-03 at 20.51.38_29f38b11.jpg
WhatsApp Image 2024-03-03 at 20.51.51_9a0ce09e.jpg