This is a comprehensive guide for STM32 I2C LCD Interfacing (I2C LCD 16×2, 20×4, and Multiple I2C LCDs). You’ll learn how to use our STM32 I2C LCD Library and create some example projects to practice what we’ll be learning in this tutorial. We’ll implement the following examples in this tutorial:
STM32 I2C LCD 16×2 Text DisplaySTM32 I2C LCD 16×2 Scrolling Text DisplaySTM32 I2C LCD 16×2 Custom Characters DisplayMultiple I2C LCD Displays Interfacing With STM32STM32 I2C LCD 20×4 Display InterfacingWe’ll start off by explaining how the I2C LCD module works, its pinout, and how to connect it to your STM32 board. We’ll display some text on the I2C LCD screen, make the text scroll on the LCD, and also create some custom characters and emojis. Without further ado, let’s get right into it!
Table of ContentsSTM32 I2C LCDSTM32 I2C LCD InterfacingSTM32 I2C LCD Library (Driver)STM32 I2C LCD Library IntegrationSTM32 I2C LCD 16×2 ExampleSTM32 I2C LCD 16×2 Example Code [Project]STM32 Multiple I2C LCD Example Code [Project]STM32 I2C LCD Common Issues TroubleshootingWrap UpSTM32 I2C LCDLCD (Liquid Crystal Display) is typically used in embedded systems to display text and numbers for the end user as an output device. The 16×2 alphanumeric display is based on the Hitachi HD44780 driver IC. Which is the small black circular chip on the back of the LCD module itself. This is the controller that controls the LCD unit and we communicate with using an STM32 microcontroller to send commands and text messages.
Alphanumeric LCD 16×2The LCD module consists of 16×2 character cells (2 rows x 16 columns), each cell of which is 5×8 dots. Controlling all of these individual dots is a tedious task for our STM32 microcontroller. However, it doesn’t have to do so. As there is a specific function controller on the LCD itself controlling the display while reading the user’s commands & data (the Hitachi HD44780 controller).
The tutorial linked below is highly recommended to learn more about STM32 LCD interfacing with GPIO-based Library, it’s bare metal and very informative.
STM32 LCD 16×2 Interfacing TutorialThis article will provide you with more in-depth information about STM32 LCD 16×2 interfacing, how LCDs work, how to use the GPIO-based LCD16X2 library and integrate it into your projects, and more. It’s highly recommended!
I2C LCD Module (PCF8574 IO Expander)The I2C IO expander IC (PCF8574) is commonly used as a cheap solution to implement an I2C LCD interface that replaces the classic parallel connection to the LCDs (at least 6 pins) with an easy-to-use I2C bus (only 2 pins). Using it saves a lot of the STM32 microcontroller’s IO pins that would have been consumed to create a generic LCD parallel communication.
Moreover, it’s shared with all I2C devices on the bus, so you can still have so many other modules/sensors connected on the same bus. In this previous article, We’ve demonstrated everything about this IC using its datasheet and build a driver library (in Embedded-C) for this IC. You can check it out if you’re interested in learning more about it.
In this tutorial, we’ll only discuss the important things that you need to know in order to get started with I2C LCD interfacing with STM32.
I2C LCD Address (Default)You need to refer to the PCF8574 chip manufacturer’s datasheet to make sure what’s the I2C device address for the chip you’ve got around. If you’ve got an I2C LCD with Ti (Texas Instruments) PCF8574 chip, the default I2C address is 0x27 which is the 7-Bit device address with the three least significant bits (A0 – A1 – A2) are pulled up to HIGH.
If you’ve got an I2C LCD with an NXP PCF8574 chip, the default I2C address is therefore 0x3F. The question is what if we’d like to add multiple I2C LCDs to the I2C bus and control them with an STM32 microcontroller, how is that possible?
First of all, any I2C device on the bus must have a unique address. Therefore, we need to change the I2C address of the PCF8574 module if we’d like to add multiple units of it on the same I2C bus. Let’s next see how to do it!
Change I2C LCD AddressThe I2C LCD interface (PCF8574) has 3 solder pads on the module’s board which control the value of the last 3 digits in the 7-Bit address of the device (A0 – A1 – A2). The pins are by default (internally) pulled up to HIGH (1), but if we short the solder pads together, this will drive the corresponding address bit pin to LOW (0). And that’s how we can change the device address.
You can use the interactive tool below to check the I2C LCD device address after soldering any of the solder pads (A0, A1, or A2). There are 8 different combinations, which means we can connect up to 8 different I2C LCDs on the same bus with a single STM32 board and control all of them at the same time.
I2C LCD Address (Texas Instruments’ PCF8574)
0100A2A1A0111I2C LCD Address (NXP’s PCF8574)
0111A2A1A0111❕ NoteThe I2C LCD module has a default I2C device address of either 0x27 or 0x3F depending on the hardware manufacturer. If you’re not quite sure about the device address, you can use this STM32 I2C Scanner application to detect the exact device address.
Using the solder pads on the PCD8574 module will enable you to set the low 3 address bits (A0-A1-A2). This means we can have up to 8 different address combinations and consequently be able to connect up to 8 I2C LCD units on the same I2C bus and control them with only one STM32 microcontroller.
STM32 I2C LCD InterfacingNow, let’s move to interfacing the I2C LCD 16×2 display with STM32. Let’s check the pinout, wiring diagram, LCD contrast control, and the I2C LCD device address.
I2C LCD PinoutThe I2C LCD Display has only four pins. The pinout is shown below:
GND is the ground pin.
Vcc is the LCD’s power supply input pin (connects to +5v).
SDA is the serial data line for the I2C LCD interface.
SCL is the serial clock line for the I2C LCD interface.
Wiring I2C LCD With STM32 Blue PillHere is the wiring diagram for the I2C LCD display with STM32 Blue Pill board that we’ll be using in the examples hereafter in this tutorial.
Contrast Adjustment For I2C LCDAfter connecting the I2C LCD module, you’ll be able to control the LCD contrast by using the PCF8574 module’s on-board potentiometer. Turn it to the right and to the left until you feel satisfied with the current contrast level.
Get The I2C LCD AddressIf you’re not quite sure about the I2C LCD’s device address, you can use the code example below and run it on your STM32 board after connecting the I2C LCD display to your board. It’ll automatically detect and print out the I2C device address for the LCD over the serial monitor.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657/** File: I2C_Scanner* Author: Khaled Magdy* -------------------------------------------* For More Information, Tutorials, etc.* Visit Website: www.DeepBlueMbedded.com**/#include "main.h"#include "stdio.h"I2C_HandleTypeDef hi2c1;UART_HandleTypeDef huart1;uint8_t Buffer[25] = {0};uint8_t Space[] = " - ";uint8_t StartMSG[] = "Starting I2C Scanning: \r\n";uint8_t EndMSG[] = "Done! \r\n\r\n";void SystemClock_Config(void);static void MX_GPIO_Init(void);static void MX_I2C1_Init(void);static void MX_USART1_UART_Init(void);int main(void){ uint8_t i = 0, ret; HAL_Init(); SystemClock_Config(); MX_GPIO_Init(); MX_I2C1_Init(); MX_USART1_UART_Init(); HAL_Delay(1000); /*-[ I2C Bus Scanning ]-*/ HAL_UART_Transmit(&huart1, StartMSG, sizeof(StartMSG), 10000); for(i=1; i