STM32 + LCD1602 with I²C Made Easy | PCF8574 Full Setup & Code with STM32CubeIDE
115 views
Dec 21, 2019
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 Tutorials: How the I²C-LCD Library Was Written (Explained): [https://youtu.be/WAGLQOIHEio] Interface LCD1602 in Parallel Mode : [https://youtu.be/ITTBWSQTi3c] #STM32 #I2C #LCD1602 #PCF8574 #STM32CubeIDE #HAL #EmbeddedSystems #Microcontroller #STM32Tutorial #I2CLCD
View Video Transcript
0:07
hello everyone welcome to controllers
0:11
tech today in this video we will
0:15
interface the LCD 16 by 2 using AI to
0:17
see in stm32 I have PCF 8 5 7 4 attached
0:23
to the back of the LCD making it easy to
0:26
use in case you don't have the one which
0:30
is attached at the back you need to
0:32
connect it to the LCD separately this is
0:35
how the pins are arranged in the PCF 8 5
0:38
7 4 you need to connect the pin 1 to the
0:41
pin 1 of the LCD and pin 16 to that of
0:44
the LCD this is how finally the
0:48
connection will look like
0:56
[Music]
1:00
these are the i2c pins and we will
1:03
discuss them in a while after connecting
1:06
the PCF 8 5 7 4 to the LCD this is how
1:10
they are internally connected RS 2 p 0
1:14
RW 2 P 1 and enable 2 P to the higher
1:18
data pins are connected from p4 to p7
1:23
[Music]
1:25
let's start the cube IDE now and create
1:29
the new project
1:43
[Music]
1:54
I am using stm32 f103 controller give
1:59
the name to the project and click finish
2:03
[Music]
2:13
first of all I am selecting the clock
2:17
from the external crystal you have to
2:20
select serial wire debug or else the SD
2:23
link will not work after the first flash
2:25
and to make it work again you have to
2:28
erase it
2:29
select the i2c and the respective pins
2:32
will be enabled note that pin p b6 is
2:36
i2c clock pin and it should be connected
2:39
to PCF eight five seven four clock pin
2:42
as you can see in this picture the same
2:46
goes for the data pins
2:48
[Music]
2:53
let's go to the clock setup now here I
2:57
am using the external crystal to
2:59
generate the clock of 72 megahertz click
3:03
Save to generate the project this is the
3:18
main dot C file here first we need to
3:22
include the library files copy the i2c
3:26
LCDC file in the source folder and i2c
3:29
LCD dot H file in the include folder
3:37
let's take a look at the i2c LCDC file
3:42
you have to change the i2c handler
3:45
according to your setup the default I to
3:48
C address is defined as 0 cross 4e this
3:51
is by default set for PCF 8 5 7 4 let's
3:56
take a look at the data sheet of the PCF
3:58
8 5 7 4 as mentioned here the addressing
4:02
of this completely depends on a0 a1 and
4:05
a2 pins by default these pins are high
4:09
and therefore the address is 0 cross 4e
4:12
I will advise that you take a look at
4:15
the data sheet of the i2c extender that
4:18
you are using these 4 bits are fixed and
4:25
the rest three are changeable along with
4:28
these 7 bits the read and write bit must
4:31
also be sent so stm32 uses different
4:36
addressing pattern than arduino does
4:39
basically in stm32 you have to send all
4:43
8 bits of the address at once the 8th
4:46
bit will be 0 in case of write and 1 in
4:49
case of read LCD send command is used to
4:53
send the command to the display as we
4:57
are using LCD in 4-bit mode we have to
4:59
send our 8 bit command in two halves
5:02
you
5:07
as you can see in the connection that
5:11
the upper four bits are connected to the
5:13
PCF that means we have to send the
5:16
higher nibble first and then the lower
5:18
one lcd sends data sends the data to the
5:30
display in the same manner this is the
5:33
function to clear the display basically
5:36
I am just writing blank everywhere on
5:39
the display LCD put cursor is used to
5:47
put the cursor specific location on the
5:49
display the parameter row can either be
5:57
0 or 1 and column can vary between oh
6:00
215 LCD in it initializes the display
6:09
the pattern of initialization is given
6:12
in the data sheet and as I said we are
6:14
using it in the 4-bit mode so that must
6:17
be initialized and at last LCD sent
6:21
string is used to send the entire string
6:24
to the LCD you must put the cursor at
6:27
some specific location before sending
6:30
the string let's start the coding now
6:34
first of all we must include the header
6:37
file in our project
6:40
[Music]
6:43
initialize the LCD send some string to
6:47
it wait for one second clear the display
6:57
put the cursor at first row and zero
7:01
column send another string remove this
7:12
clear part let's build the code so no
7:16
errors that's good
7:19
start the debugging
7:24
[Music]
7:34
you
7:42
this is our debugging perspective let's
7:46
run the project now as you can see both
7:59
the strings are bring printed at their
8:01
respective locations the second one
8:04
prints after one second has written in
8:07
the program let's make it little more
8:10
interesting I am defining the variables
8:21
row and column first
8:24
[Music]
8:31
I am going to print all the ASCII
8:51
characters from 20 to 128 the column is
9:00
going to increment after each print this
9:03
is not necessary as the column will
9:05
increment automatically anyway but we
9:08
need this for the next step and that's
9:10
why I am writing this if column is more
9:13
than 15 the row will increment and
9:16
column becomes 0 if the row is more than
9:19
one row becomes 0 and I am giving some
9:24
delay between each printing
9:27
[Music]
9:37
[Music]
9:45
[Music]
9:58
you
10:10
[Music]
10:18
[Music]
10:19
it is printing alright but not as I
10:22
expected let's make some changes in the
10:30
code
10:37
I am going to add some delay here and
10:42
clear this data from the screen and I
10:49
wanted to print from 0 to 128
10:58
[Music]
11:10
[Music]
11:11
as you can see now it's much better to
11:16
display any number on the LCD you need
11:18
to change it to the respective ASCII
11:20
character first you can do that by
11:24
either using S print F or break the
11:27
number down in ones tens and hundreds
11:30
form and then display individually by
11:32
adding 48 to the numbers this is it guys
11:36
I hope you understood the video you can
11:41
download the code from the link in the
11:43
description the library files will be in
11:47
the source and include folders leave
11:50
comments in case of any doubts but
11:53
before doing that make sure that you
11:55
have checked the datasheet of the i2c
11:57
extender properly because the i2c
12:00
addresses change for other variants of
12:02
the PCF family do not use the i2c
12:05
address that you found from Arduino have
12:09
a nice day keep watching
12:15
[Music]
#Computers & Electronics
#Electronic Components
#Electronics & Electrical