Skip to playerSkip to main content
Unlock the simplicity of connecting the LCD1602 display to your STM32 microcontroller via I²C using the PCF8574 I/O expander. Perfect for embedded developers, this guide walks you through configuration using STM32CubeIDE and HAL, covering wiring, initialization, and sending text data—all in easy-to-follow steps.

📥 Download Project Files:
- Get the full project code to replicate and experiment with: https://controllerstech.com/i2c-lcd-in-stm32/

📺 Related Videos:
- How the I²C-LCD Library Was Written (Explained) : [https://youtu.be/WAGLQOIHEio]
- Interface LCD1602 in Parallel Mode : [https://youtu.be/ITTBWSQTi3c]
- STM32 Custom Characters on LCD : [https://www.youtube.com/watch?v=diwjZPmFUKo]

✨ If you found this helpful, don’t forget to LIKE 👍, SUBSCRIBE 🔔, and SHARE!

STM32 Playlist :::: https://www.youtube.com/playlist?list=PLfIJKC1ud8gga7xeUUJ-bRUbeChfTOOBd

#STM32 #I2C #LCD1602 #PCF8574 #STM32CubeIDE #HAL #EmbeddedSystems #Microcontroller #STM32Tutorial #I2CLCD

________________________________________________________________________________________


Facebook Page : https://www.facebook.com/controllerstech

Telegram Group : https://t.me/controllerstechdiscuss

Instagram : https://www.instagram.com/controllerstech/

For more info, visit https://controllerstech.com

Category

🤖
Tech
Transcript
00:00Hello everyone. Welcome to Controllers Tech. Today in this video, we will interface the
00:15LCD 16x2 using I2C in SDM32. I have PCF8574 attached to the back of the LCD, making it
00:26easy to use. In case you don't have the one which is attached at the back, you need to connect it to
00:32the LCD separately. This is how the pins are arranged in the PCF8574. You need to connect
00:40the pin 1 to the pin 1 of the LCD, and pin 16 to that of the LCD. This is how finally the connection
00:48will look like. These are the I2C pins, and we will discuss them in a while. After connecting
01:06the PCF8574 to the LCD, this is how they are internally connected. RS to P0, RW to P1, and
01:16enable to P2. The higher data pins are connected from P4 to P7.
01:26Let's start the cube IDE now, and create the new project.
01:54I am using SDM32F103 controller. Give the name to the project and click Finish.
02:15First of all, I am selecting the clock from the external crystal. You have to select Serial
02:21wire debug or else the SD link will not work after the first flash, and to make it work
02:27again, you have to erase it. Select the I2C, and the respective pins will be enabled.
02:35Note that pin PB6 is I2C clock pin, and it should be connected to PCF8574 clock pin. As you can
02:43see in this picture, the same goes for the data pins.
02:54Let's go to the clock setup now. Here I am using the external crystal to generate the clock
03:00of 72 MHz. Click Save to generate the project.
03:18This is the main .c file. Here first we need to include the library files. Copy the I2C LCD
03:26dot C file in the source folder, and I2C LCD dot H file in the include folder.
03:33Let's take a look at the I2C LCD dot C file. You have to change the I2C handler according
03:45to your setup. The default I2C address is defined as 0 cross 4E, this is by default set for PCF8574.
03:54Let's take a look at the datasheet of the PCF8574. As mentioned here, the addressing of this completely
04:03depends on A0, A1, and A2 pins. By default these pins are high, and therefore the address
04:11is 0 cross 4E. I will advise that you take a look at the datasheet of the I2C extender
04:18that you are using. These 4 bits are fixed, and the rest 3 are changeable. Along with these
04:297 bits, the read and write bit must also be sent. So, STM32 uses different addressing pattern
04:37than Arduino does. Basically, in STM32 you have to send all 8 bits of the address at once.
04:46The 8th bit will be 0 in case of write, and 1 in case of read.
04:51LCD send command is used, to send the command to the display. As we are using LCD in 4-bit mode,
04:59we have to send our 8-bit command in two halves. As you can see in the connection that the upper
05:124 bits are connected to the PCF, that means, we have to send the higher nibble first, and
05:18the lower one. LCD send data sends the data to the display in the same manner. This is the
05:33function to clear the display. Basically, I am just writing blank everywhere on the display.
05:44LCD put cursor is used to put the cursor at specific location on the display.
05:55The parameter row, can either be 0 or 1, and column can vary between 0 to 15.
06:07LCD init initializes the display. The pattern of initialization is given in the datasheet, and
06:13as I said, we are using it in the 4-bit mode, so that must be initialized.
06:20And at last, LCD send string is used to send the entire string to the LCD.
06:26You must put the cursor at some specific location before sending the string.
06:32Let's start the coding now. First of all, we must include the header file in our project.
06:43Initialize the LCD.
06:46Send some string to it.
06:54Wait for one second.
06:57Clear the display. Put the cursor at first row and 0 column.
07:04And another string.
07:12Remove this clear part. Let's build the code.
07:16So no errors, that's good.
07:20This is our debugging perspective. Let's run the project now.
07:43This is our debugging perspective. Let's run the project now.
07:58As you can see, both the strings are bring printed at their respective locations.
08:04The second one prints after one second, as written in the program.
08:09Let's make it little more interesting.
08:19I am defining the variables row and column first.
08:24I am going to print all the ASCII characters from 20 to 128.
08:49I am going to print all the ASCII characters from 20 to 128.
08:59The column is going to increment after each print.
09:03This is not necessary, as the column will increment automatically anyway, but we need this for the
09:08next step, and that's why I am writing this.
09:13If column is more than 15, the row will increment and column becomes 0.
09:18If the row is more than 1, row becomes 0.
09:23And I am giving some delay between each printing.
09:30I am giving some delay between each printing.
09:38The row is more than 15, the row will increasing the waste of time.
09:44The row is now in the row, where the row becomes 0.
09:47The row is now in the row of the half.
09:49This row is going to begin with a row of two.
09:52The row is now in place.
09:56The row is next.
09:59The row is already in the row, where the row is next.
10:02The row is next to the row of the row.
10:05It is printing alright but not as I expected.
10:28Let's make some changes in the code.
10:40I am going to add some delay here, and clear this data from the screen.
10:48And I wanted to print from 0 to 128.
11:13As you can see now, it's much better.
11:16To display any number on the LCD, you need to change it to the respective ASCII character
11:21first.
11:23You can do that by either using s-print-f.
11:27Or break the number down in ones, tens and hundreds form, and then display individually
11:32by adding 48 to the numbers.
11:36This is it guys.
11:38I hope you understood the video.
11:41You can download the code from the link in the description.
11:45The library files will be in the source and include folders.
11:50Leave comments in case of any doubts.
11:53But before doing that, make sure that you have checked the datasheet of the i2c extender properly,
11:59because the i2c addresses change for other variants of the PCF family.
12:04Do not use the i2c address, that you found from Arduino.
12:09Have a nice day.
12:11Keep watching.
Be the first to comment
Add your comment

Recommended