In this guide, you will learn how to connect a 16×2 character LCD to an STM32 microcontroller. We cover the wiring, the example code and how the code works. As a result, you can show text on the LCD and build a simple user interface for your embedded project.

⏱ 5 min read · 🔧 Includes: diagrams & photos, source code · 💬 Stuck? Ask us on WhatsApp

Advertisements

Working with electricity involves serious risk. Ensure you have the necessary skills and take proper safety precautions before attempting any electrical projects. Proceed at your own risk — the author assumes no responsibility for any damage, injury, or issues resulting from the use or misuse of the information provided.

All content on this website is original and protected by copyright. Please do not copy or reproduce content without permission. While most of the resources shared here are open-source and freely accessible for your learning and benefit, your respect for our intellectual effort is appreciated.

If you find our tutorials helpful, consider supporting us by purchasing related materials or sharing our work — it helps keep the content flowing.

Need help or have questions? Leave a comment below — the author is always happy to assist!

About 16×2 LCD

A 16×2 LCD gets its name from its layout: 16 columns and 2 rows of characters. Other sizes exist, such as 8×1, 8×2, 10×2 and 16×1. However, the 16×2 is by far the most popular, so we use it here.

All of these HD44780-compatible displays have 16 pins, and you program them the same way. Therefore, the same code works for any of them. Below are the pinout and pin functions of the 16×2 LCD module:

How to interface a 16x2 LCD with an STM32 – About 16x2 LCD

Interfacing 16×2 LCD with STM32

Connecting a 16×2 LCD to an STM32 is a common embedded task. First, you wire the LCD to the STM32. Then, you write code to control it. Follow these steps:

  1. Wire the LCD to the STM32 as follows:
  • Connect the RS pin of the LCD to a GPIO pin of the STM32.
  • Connect the EN pin of the LCD to another GPIO pin.
  • Connect the D4–D7 data pins of the LCD to four more GPIO pins.
  • Connect VSS and RW to ground. With RW grounded, the LCD only receives data and never drives the STM32 pins.
  • Connect VDD to +5 V, V0 (contrast) to the wiper of a 10 kΩ potentiometer, the backlight A pin to +5 V through a resistor (unless your module already has one) and K to ground.
  1. Set up the GPIO pins: in the code, configure the pins connected to the LCD as outputs.
  2. Initialise the LCD: send the start-up commands. These select 4-bit mode, set the display mode and clear the screen.
  3. Display data: finally, send commands to place the cursor, then send the characters you want to show.
How to interface a 16x2 LCD with an STM32

Example code

Here is example code that drives a 16×2 LCD from an STM32:

#include "stm32f4xx.h"

#define RS_Pin GPIO_PIN_12
#define RS_GPIO_Port GPIOB
#define EN_Pin GPIO_PIN_13
#define EN_GPIO_Port GPIOB
#define D4_Pin GPIO_PIN_14
#define D4_GPIO_Port GPIOB
#define D5_Pin GPIO_PIN_15
#define D5_GPIO_Port GPIOB
#define D6_Pin GPIO_PIN_10
#define D6_GPIO_Port GPIOB
#define D7_Pin GPIO_PIN_11
#define D7_GPIO_Port GPIOB

void LCD_SendCommand(uint8_t command);
void LCD_SendData(uint8_t data);
void LCD_Clear(void);
void LCD_WriteString(char* str);

void LCD_Init(void) {
  LCD_SendCommand(0x02);
  LCD_SendCommand(0x28);
  LCD_SendCommand(0x0C);
  LCD_SendCommand(0x06);
}

void LCD_SendCommand(uint8_t command) {
  HAL_GPIO_WritePin(RS_GPIO_Port, RS_Pin, GPIO_PIN_RESET);

  HAL_GPIO_WritePin(D4_GPIO_Port, D4_Pin, (command >> 4) & 1);
  HAL_GPIO_WritePin(D5_GPIO_Port, D5_Pin, (command >> 5) & 1);
  HAL_GPIO_WritePin(D6_GPIO_Port, D6_Pin, (command >> 6) & 1);
  HAL_GPIO_WritePin(D7_GPIO_Port, D7_Pin, (command >> 7) & 1);

  HAL_GPIO_WritePin(EN_GPIO_Port, EN_Pin, GPIO_PIN_SET);
  HAL_Delay(1);
  HAL_GPIO_WritePin(EN_GPIO_Port, EN_Pin, GPIO_PIN_RESET);
  HAL_Delay(1);

  HAL_GPIO_WritePin(D4_GPIO_Port, D4_Pin, command & 1);
  HAL_GPIO_WritePin(D5_GPIO_Port, D5_Pin, (command >> 1) & 1);
  HAL_GPIO_WritePin(D6_GPIO_Port, D6_Pin, (command >> 2) & 1);
  HAL_GPIO_WritePin(D7_GPIO_Port, D7_Pin, (command >> 3) & 1);

  HAL_GPIO_WritePin(EN_GPIO_Port, EN_Pin, GPIO_PIN_SET);
  HAL_Delay(1);
  HAL_GPIO_WritePin(EN_GPIO_Port, EN_Pin, GPIO_PIN_RESET);
  HAL_Delay(1);
}

void LCD_SendData(uint8_t data) {
  HAL_GPIO_WritePin(RS_GPIO_Port, RS_Pin, GPIO_PIN_SET);

  HAL_GPIO_WritePin(D4_GPIO_Port, D4_Pin, (data >> 4) & 1);
  HAL_GPIO_WritePin(D5_GPIO_Port, D5_Pin, (data >> 5) & 1);
  HAL_GPIO_WritePin(D6_GPIO_Port, D6_Pin, (data >> 6) & 1);
  HAL_GPIO_WritePin(D7_GPIO_Port, D7_Pin, (data >> 7) & 1);

  HAL_GPIO_WritePin(EN_GPIO_Port, EN_Pin, GPIO_PIN_SET);
  HAL_Delay(1);
  HAL_GPIO_WritePin(EN_GPIO_Port, EN_Pin, GPIO_PIN_RESET);
  HAL_Delay(1);

  HAL_GPIO_WritePin(D4_GPIO_Port, D4_Pin, data & 1);
  HAL_GPIO_WritePin(D5_GPIO_Port, D5_Pin, (data >> 1) & 1);
  HAL_GPIO_WritePin(D6_GPIO_Port, D6_Pin, (data >> 2) & 1);
  HAL_GPIO_WritePin(D7_GPIO_Port, D7_Pin, (data >> & 1);
HAL_GPIO_WritePin(EN_GPIO_Port, EN_Pin, GPIO_PIN_SET);
HAL_Delay(1);
HAL_GPIO_WritePin(EN_GPIO_Port, EN_Pin, GPIO_PIN_RESET);
HAL_Delay(1);
}

void LCD_Clear(void) {
LCD_SendCommand(0x01);
HAL_Delay(2);
}

void LCD_WriteString(char* str) {
while(*str) {
LCD_SendData(*str++);
HAL_Delay(1);
}
}

int main(void) {
HAL_Init();

RCC->AHB1ENR |= RCC_AHB1ENR_GPIOBEN;

GPIO_InitTypeDef GPIO_InitStruct = {0};
GPIO_InitStruct.Pin = RS_Pin | EN_Pin | D4_Pin | D5_Pin | D6_Pin | D7_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

LCD_Init();

LCD_Clear();
LCD_WriteString("Hello World!");

while (1) {
}
}

In this example, the LCD connects to the STM32 as follows:

  • RS → PB12
  • EN → PB13
  • D4 → PB14
  • D5 → PB15
  • D6 → PB10
  • D7 → PB11

How the code works

First, the code sets up the GPIO pins. Next, it defines functions that send commands and data to the LCD. For example, LCD_Init() prepares the display, LCD_Clear() clears it, and LCD_WriteString() prints a text string.

In main(), the program initialises the GPIO pins and the LCD. Then it writes “Hello World!” to the screen. Finally, the microcontroller stays in an endless loop.

The code uses the 4-bit interface, so it sends each byte as two 4-bit nibbles. It sets the data pins with HAL_GPIO_WritePin(). Since RW is tied to ground, the LCD is write-only. For this reason, HAL_Delay() adds short waits so the LCD has time to finish each command.

Conclusion

In short, connecting a 16×2 LCD to an STM32 needs only a few wires and some simple code. Follow the steps and the example above, and you can show text on the screen. This gives you a basic user interface for many embedded projects.

Read more on:

Liked this article? Subscribe to our newsletter:

Loading

or,

Visit LabProjectsBD.com for more inspiring projects and tutorials.

Thank you!

Have a project in mind? LabProjectsBD designs power electronics, embedded firmware, IoT devices and production-ready PCBs for clients. Message us on WhatsApp, use the contact form, or see our services and portfolio.


MKDas

Mithun K. Das. B.Sc. in Electrical and Electronic Engineering (EEE) from KUET. Senior Embedded Systems Designer at a leading international company. Welcome to my personal blog! I share articles on various electronics topics, breaking them down into simple and easy-to-understand explanations, especially for beginners. My goal is to make learning electronics accessible and enjoyable for everyone. If you have any questions or need further assistance, feel free to reach out through the Contact Us page. Thank you for visiting, and happy learning!

5 Comments

Rafiu Ajape · 27/03/2023 at 4:47 pm

Good day sir. Thank you for the good work. Pls what compiler do you for this STM microcontroller boards in case of simulation purposes.

    MKDas · 28/03/2023 at 11:45 am

    no simulation, direct debug. STM32CubeIDE

Asimiyu · 04/04/2023 at 10:04 pm

Thank you sir

DINESH KUMAR · 24/11/2023 at 10:03 am

what simulation is suitable for STM32 family other than proteus simulation ?????

    MKDas · 29/11/2023 at 8:29 pm

    hardware simulation(!). Debug in real hardware. Simulation is for kids.

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *