Home Blog Blog Details

How to connect STM32 Blue Pill with 16x2 character LCD module using Arduino IDE?

December 30 2024
Ampheo 37

Inquiry

Global electronic component supplier AMPHEO PTY LTD: Rich inventory for one-stop shopping. Inquire easily, and receive fast, customized solutions and quotes.

QUICK RFQ
ADD TO RFQ LIST
The STM32 Blue Pill (STM32F103C8T6) can interface with a 16x2 LCD module using the I2C protocol (recommended) or direct GPIO connections.

The STM32 Blue Pill (STM32F103C8T6) can interface with a 16x2 LCD module using the I2C protocol (recommended) or direct GPIO connections. Below are steps for both methods.


🛠️ 1. Components Required

  • STM32 Blue Pill (STM32F103C8T6)
  • 16x2 LCD Module
  • I2C LCD Adapter Module (for I2C method)
  • Breadboard
  • Jumper Wires

📌 Method 1: Using I2C Interface (Recommended)

Pin Connections (I2C Adapter on LCD)

I2C LCD Pin STM32 Pin Description
VCC 3.3V Power Supply
GND GND Ground
SDA PB7 I2C Data Line
SCL PB6 I2C Clock Line

📦 Install Required Libraries

In Arduino IDE, go to:

  1. Sketch > Include Library > Manage Libraries
  2. Search and install:
    • LiquidCrystal_I2C

💻 I2C LCD Code Example

cpp
 
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Initialize the LCD with I2C address (default: 0x27 or 0x3F)
LiquidCrystal_I2C lcd(0x27, 16, 2);
 
void setup() {
lcd.init(); // Initialize the LCD
lcd.backlight(); // Turn on the backlight
lcd.setCursor(0, 0); // Set cursor to first row, first column
lcd.print("STM32 + LCD");
lcd.setCursor(0, 1); // Set cursor to second row
lcd.print("Hello World!");
}
 
void loop() {
// Nothing in the loop for now
}

🚀 Upload and Test

  1. Select Generic STM32F103C Series in Tools > Board.
  2. Ensure Upload Method: is set to STM32CubeProgrammer (SWD) or Serial.
  3. Upload the code.
  4. You should see the text "STM32 + LCD" and "Hello World!" displayed.

📌 Method 2: Direct GPIO Interface (Without I2C)

If you're not using an I2C module, connect the LCD directly to GPIO pins.

Pin Connections (Direct GPIO)

LCD Pin STM32 Pin Description
VSS GND Ground
VCC 3.3V Power
VEE Potentiometer (for contrast) Contrast Control
RS PA0 Register Select
RW GND Read/Write (GND for Write mode)
E PA1 Enable
D4 PA2 Data Bit 4
D5 PA3 Data Bit 5
D6 PA4 Data Bit 6
D7 PA5 Data Bit 7
A 3.3V LED Anode
K GND LED Cathode

📦 Install Required Library

  1. Go to Sketch > Include Library > Manage Libraries.
  2. Search and install:
    • LiquidCrystal

💻 GPIO LCD Code Example

cpp
 
#include <LiquidCrystal.h>
 
// Pin configuration (RS, E, D4, D5, D6, D7)
LiquidCrystal lcd(PA0, PA1, PA2, PA3, PA4, PA5);
 
void setup() {
lcd.begin(16, 2); // Initialize 16x2 LCD
lcd.setCursor(0, 0);
lcd.print("STM32 + LCD");
lcd.setCursor(0, 1);
lcd.print("Direct GPIO!");
}
 
void loop() {
// Nothing here for now
}

🚀 Upload and Test

  1. Select Generic STM32F103C Series in Tools > Board.
  2. Upload the code.
  3. Text "STM32 + LCD" and "Direct GPIO!" should display on the screen.

🛡️ Troubleshooting

  • I2C Issue: Check the I2C address (0x27 or 0x3F). Use an I2C Scanner sketch to verify.
  • Display Issue: Adjust the contrast potentiometer on the LCD.
  • Wiring: Double-check pin connections.
  • Ensure STM32 drivers are correctly installed in Arduino IDE.
Ampheo