0:09
hello and welcome to controllers Tech
0:13
this is another video in the es32 series
0:16
and today we will see another peripheral
0:19
i2c i2c or I squ C or I stands for inter
0:25
integrated circuit and it is a bus
0:28
interface connection protocol and
0:29
incorporated into devices for serial
0:33
Communication in order to show how the
0:35
i2c works I am going to interface a very
0:38
famous LCD 1602 display with the sb32
0:42
today I have already made a video
0:45
explaining how I wrote the lcd2c library
0:48
so if you are interested in knowing the
0:50
details please check out that video so
0:54
this is the library we will be using
0:57
today it basically uses the 2C to
1:00
interface the LCD with the
1:03
MCU this is the stdm 32 related function
1:07
which writes the data using the i2c we
1:10
will change it obviously as for the
1:13
esp32 the LCD is connected using the
1:16
pcf8574 which is basically an i2c
1:21
expander here are the data and clock
1:26
connection if we check our es32 it has
1:30
gp22 for the i2c clock and 21 for the
1:34
data we will be using these pins to
1:39
PCF you can see the PCF is attached to
1:43
LCD the two pins from the PCF are
1:48
32 they are the data pin which is the
1:51
green wire connected between the SDA of
1:53
the PCF and the D21 of the ESB
1:57
32 and the yellow wire is connected
2:00
between the clock pin of the PCF and the
2:05
32 we need to give 5 volts to the PCF
2:09
for which I will use an external Supply
2:11
since es32 doesn't have a 5 volts
2:15
output all right now we have established
2:17
the connection so let's create a new
2:22
ESF we will use an example template so
2:25
go to peripheral i2c and let's use this
2:37
template let's rename this to I2
2:47
LCD so the project is generated
2:49
successfully and here we have our main
2:52
file before we proceed let's build it
2:56
once make sure you select the correct
3:02
here all right the build is successful
3:04
without any errors and if the main file
3:07
still shows error just click at some
3:09
empty place and press
3:11
enter since this is an example template
3:14
it has some functions configured for the
3:24
all all right here we have the function
3:27
to initialize the i2c in master mode
3:30
we will edit this function a little here
3:33
we have to provide which i2c we are
3:36
using since the board I am using has
3:38
only one i2c so this must be the i2c Z
3:43
next we have to input the data and clock
3:46
pins here you can see the data is pin 21
3:52
22 next parameter sets the pull-up
3:54
feature for these pins since we are not
3:58
using any external pull-up resistors
4:00
let's keep the internal pull-up enabled
4:04
the last parameter is the clock
4:06
speed 100 khz is enough for the LCD to
4:10
work you can also set highspeed mode
4:14
khz this basically depends on the device
4:18
specifications all right let's delete
4:20
these defines from here in the master
4:23
initialization function after defining
4:26
the parameters it configures them using
4:28
the function configur
4:33
parameters and finally it will install
4:36
the i2c driver this function takes the
4:39
parameter as the i2c number which in our
4:42
case is zero the i2c mode which is
4:45
Master the length of the RX and TX
4:47
buffers which is not needed since we are
4:49
using the master mode and the buffers
4:51
are only required in the slave mode the
4:55
last is the interrupt flag which also we
5:00
so the buffer lengths should be set to
5:03
zero this will install the i2c driver
5:06
and we are good to go with our
5:08
LCD let's delete the mpu related
5:11
functions in the main so inside the main
5:15
function we are initializing the i2c and
5:18
once it has been initialized we will log
5:22
message now let's include the library
5:30
you can just drag and drop them in the
5:33
folder open the I2 CCD doc file so that
5:37
we can modify it remove the i2c Handler
5:42
definition the slave address for the PCF
5:45
is 0 cross4 e the data will be written
5:49
to the LCD in the similar manner but we
5:51
just need to change the function to
5:55
es32 this part will remain similar
5:58
across whatever controller you
6:00
use now let's see what functions are
6:03
available in the sb32 i2c
6:13
library I to see Master right to device
6:16
the parameters are the i2c number the
6:19
device address the buffer we want to
6:21
write the buffer length and the timeout
6:24
function this function should work for
6:27
us also note here that the device
6:30
address is being shifted to left by one
6:33
position this info will be needed later
6:37
all right let's use this function to
6:41
LCD I am defining the error
6:48
variable the first parameter is the i2c
6:51
number which is zero in our case
7:10
then we have the slave address which is
7:13
defined here next is the buffer we want
7:16
to send that will be the data te
7:19
itself the buffer size is four
7:22
bytes and let's keep the time out to be
7:26
second this function will return zero on
7:29
success and I guess minus one if it
7:34
fails anyway if the error is not zero we
7:38
console for this we must Define the tag
7:42
first as it's defined in the main
7:47
file and now we will log using this tag
7:50
that there is some problem in sending
7:52
the command the same functions will be
7:55
used to send the data also
8:09
so that's all we need to modify in this
8:12
library in the LCD header file remove
8:15
the stdm 32 inclusion let's build it
8:24
once we have errors regarding the use of
8:27
i2c number this is because we haven't
8:31
included the i2c driver yet so let's
8:33
include this in the LCD
8:38
Library one last and very important
8:41
thing in order to include a library in
8:44
the project we must register it in the
8:46
cake list file in this IDF component
8:50
register we will register a new source
8:52
file I2 CCD doc all right let's build it
9:02
since we have registered a new source
9:04
it's going to build the entire project
9:06
again and you can see the errors
9:09
regarding the inclusions are gone now I
9:12
forgot to change this delay function H
9:15
delay is used to provide delay in
9:18
milliseconds so we need to use an
9:20
equivalent function here in
9:22
es32 you sleep is used to put the MCU in
9:26
sleep but this takes the argument in
9:33
so change all the delays to you sleep
9:53
parameters we also need to include the
9:55
header file for this you sleep function
10:08
so everything is fine now and our
10:10
library is ready to be used with the ESP
10:13
32 if you are only using
10:16
esp32 you can just take this Library
10:19
include it in the project and modify the
10:21
cmake list file as I said I will explain
10:25
the library in the next video now we
10:29
will will use this library to print
10:32
LCD in the main file include the LCD
10:35
header file now in the main function
10:39
first we need to initialize the
10:41
LCD remember the i2c must be initialized
10:45
before we initialize the LCD LCD put
10:49
cursor will put the cursor in the zeroth
10:51
row and zeroth column which is in the
10:53
beginning of the top
10:57
row we will send the string hello world
11:05
position then we will put the cursor at
11:07
the first row and zeroth column that is
11:10
in the beginning of the bottom row and
11:12
we will print another string there
11:15
that's it let's build the code
11:26
now there are no errors so we will run
11:30
it using the Run configuration in the
11:32
IDF application tab we will also check
11:35
the logs to see if there is any error
11:38
make sure the serial Port is correct
11:44
here the log is showing errors in
11:47
sending commands and data to the
11:51
LCD remember we defined them in the I2
11:54
clcd source file or right the error is
11:58
because of this this address this
12:00
address is 8 Bits long which include the
12:03
read or write bit also since the address
12:06
here is shifted left by one place means
12:09
the esp32 functions also take the 7bit
12:14
Arduino so we will shift this address to
12:16
the right by one place making it a seven
12:19
bit address all right let's build and
12:39
you can see in the console there are no
12:41
errors this time the LCD is displaying
12:45
our strings hello world in the top row
12:48
and from esp32 in the bottom row so
12:51
things have been working pretty good so
12:54
far remember that the LCD can only print
12:57
asy characters so if you want to display
12:59
numbers you need to change them to
13:01
respective asy characters first for
13:05
example if I want to print any number
13:08
first of all I need to define a buffer
13:10
where I can store the converted asy
13:17
characters we will use S print F to
13:20
change the number to the
13:25
characters then simply put the cursor
13:28
where you want to print the number and
13:30
send the buffer to the
13:41
display you can see the number is being
13:45
LCD similarly say if we are displaying
13:48
the float value we need to change the
13:50
formatting in the S print F and the rest
13:53
will remain the same
14:18
the LCD is displaying the float number
14:21
as well basically it can display
14:26
format I hope you understood how to use
14:29
the library to display the data on the
14:32
LCD this is it for this
14:35
video you can download the code from the
14:39
description keep watching and have a