How to use I2C in ESP32 || Interface LCD 1602 || Espressif IDE
1 views
Jul 14, 2022
WATCH THE UPDATED VIDEO :::: https://youtu.be/xCVAuSobOxU
View Video Transcript
0:01
[Music]
0:09
hello and welcome to controllers tech
0:13
this is another video in the esb 32
0:16
series and today we will see another
0:18
peripheral the i2c
0:20
i2c or i squared c or iic stands for
0:25
inter-integrated circuit and it is a bus
0:28
interface connection protocol
0:29
incorporated into devices for serial
0:32
communication
0:34
we have covered a lot of videos in stm
0:36
32 series where we used i-2-c to
0:39
communicate with the sensor or to drive
0:41
some display
0:43
in order to show how the i2c works i am
0:46
going to interface a very famous lcd
0:49
1602 display with the esp32 today
0:53
if you have already seen my stm32 video
0:56
on this display this one is going to be
0:58
easier for you since we will be using
1:01
the same library that we have used in
1:03
sdm 32
1:05
otherwise if you are only watching the
1:07
esp32 series don't worry i will release
1:11
another video where i will explain how
1:13
the library was written
1:16
i couldn't cover everything in this
1:17
video that is why we will go with two
1:20
videos
1:22
so today we will see how to port the i2c
1:24
lcd library from the stm32 to the esp32
1:30
if you are only interested in esp32
1:33
just focus on the parts where we make
1:36
use of the library like how to import it
1:38
to the project or how to use the
1:40
functions to display data on the lcd
1:43
and in the next video i will explain the
1:46
library
1:48
so this is the library we will be using
1:51
today
1:52
it basically uses the i2 c to interface
1:55
the lcd with the mcu
1:58
this is the stm32 related function which
2:02
writes the data using the i2c we will
2:04
change it obviously as per the esp 32.
2:08
the lcd is connected using the
2:10
p-c-f-8574
2:12
which is basically an i-2-c expander
2:16
here are the data and clock pins for the
2:19
i-2-c connection
2:21
if we check our e-s-b-32 it has
2:24
g-p-i-o-22 for the i-2-c clock and 21
2:28
for the data
2:29
we will be using these pins to connect
2:32
to the p-c-f
2:34
you can see the p-c-f is attached to the
2:36
l-c-d
2:38
the 2 pins from the p-c-f are connected
2:40
to the e-s-p-32
2:43
they are the data pin which is the green
2:45
wire connected between the s-d-a of the
2:48
p-c-f and the d-21 of the e-s-b-32
2:52
and the yellow wire is connected between
2:54
the clock pin of the p-c-f and the d-22
2:57
of the e-s-b-32
3:00
we need to give 5 volts to the p-c-f for
3:03
which i will use an external supply
3:05
since esp32 doesn't have a 5 volts
3:08
output
3:10
all right now we have established the
3:12
connection so let's create a new project
3:14
in esp-idf
3:17
we will use an example template so go to
3:20
peripheral i2c and let's use this i2c
3:23
simple template
3:32
let's rename this to i2 clcd
3:42
so the project is generated successfully
3:45
and here we have our main file
3:48
before we proceed let's build it once
3:51
make sure you select the correct mcu
3:54
from here
4:05
all right the build is successful
4:07
without any errors and if the main file
4:09
still shows error just click at some
4:11
empty place and press enter
4:14
since this is an example template it has
4:17
some functions configured for the
4:19
mpu6050
4:22
let's delete them all
4:27
all right here we have the function to
4:29
initialize the i2c in master mode
4:32
we will edit this function a little
4:35
here we have to provide which i2 c we
4:38
are using
4:39
since the board i am using has only one
4:41
i2c so this must be the i2 c0
4:45
next we have to input the data and clock
4:48
pins
4:49
here you can see the data is pin 21 and
4:53
clock is pin 22
4:55
next parameter sets the pull-up feature
4:57
for these pins
4:59
since we are not using any external
5:01
pull-up resistors let's keep the
5:04
internal pull-up enabled
5:06
the last parameter is the clock speed
5:09
100 kilohertz is enough for the lcd to
5:12
work you can also set high speed mode
5:14
with 400 kilohertz
5:17
this basically depends on the device
5:19
specifications
5:22
all right let's delete these defines
5:24
from here
5:25
in the master initialization function
5:28
after defining the parameters it
5:30
configures them using the function
5:31
configure parameters
5:36
and finally it will install the i2c
5:39
driver
5:40
this function takes the parameter as the
5:42
i2c number which in our case is 0 the
5:46
i2c mode which is master the length of
5:48
the rx and tx buffers which is not
5:51
needed since we are using the master
5:53
mode and the buffers are only required
5:55
in the slave mode
5:57
the last is the interrupt flag which
5:59
also we are not using today
6:02
so the buffer lengths should be set to
6:04
zero
6:06
this will install the i-2-c driver and
6:09
we are good to go with our l-c-d
6:12
let's delete the m-p-u related functions
6:14
in the main
6:16
so inside the main function we are
6:18
initializing the i2c and once it has
6:21
been initialized we will log the success
6:24
message
6:25
now let's include the library files
6:32
you can just drag and drop them in the
6:34
main folder
6:36
open the
6:38
i2clcd.c file so that we can modify it
6:42
remove the i2c handler definition
6:45
the slave address for the p-c-f is zero
6:48
cross four e
6:50
the data will be written to the l-c-d in
6:52
the similar manner but we just need to
6:54
change the function to write data using
6:56
e-s-b-32
6:59
this part will remain similar across
7:01
whatever controller you use
7:04
now let's see what functions are
7:06
available in the esb 32 i2c library
7:16
i2 c master right to device the
7:19
parameters are the i2c number the device
7:21
address the buffer we want to write the
7:24
buffer length and the timeout for the
7:26
function
7:28
this function should work for us
7:31
also note here that the device address
7:33
is being shifted to left by 1 position
7:36
this info will be needed later
7:39
all right let's use this function to
7:42
send the data to the lcd
7:44
i am defining the error variable
7:51
the first parameter is the i2c number
7:54
which is zero in our case
8:13
then we have the slave address which is
8:15
defined here
8:17
next is the buffer we want to send that
8:19
will be the data t itself
8:22
the buffer size is 4 bytes
8:25
and let's keep the time out to be 1
8:27
second
8:29
this function will return zero on
8:31
success and i guess minus 1 if it fails
8:37
anyway if the error is not zero we will
8:39
log this on the console
8:42
for this we must define the tag first as
8:45
it's defined in the main file
8:50
and now we will log using this tag that
8:53
there is some problem in sending the
8:55
command
8:56
the same functions will be used to send
8:59
the data also
9:12
so that's all we need to modify in this
9:14
library
9:15
in the lcd header file remove the stm32
9:19
inclusion
9:21
let's build it once
9:28
we have errors regarding the use of i2c
9:30
number
9:32
this is because we haven't included the
9:34
i2c driver yet so let's include this in
9:37
the lcd library
9:41
one last and very important thing
9:44
in order to include a library in the
9:46
project we must register it in the c
9:49
make list file
9:51
in this idf component register we will
9:53
register a new source file i2 clcd dot c
9:58
all right let's build it again
10:04
since we have registered a new source
10:06
it's going to build the entire project
10:08
again
10:10
and you can see the errors regarding the
10:12
inclusions are gone now
10:14
i forgot to change this delay function
10:18
whole delay is used to provide delay in
10:20
milliseconds so we need to use an
10:22
equivalent function here in esp32
10:26
you sleep is used to put the mcu in
10:28
sleep but this takes the argument in
10:30
microseconds
10:36
so change all the delays to u sleep and
10:38
modify the parameters
10:56
we also need to include the header file
10:58
for this u sleep function
11:10
so everything is fine now and our
11:12
library is ready to be used with the
11:14
esp32
11:17
if you are only using esp32 you can just
11:20
take this library include it in the
11:22
project and modify the c make list file
11:26
as i said i will explain the library in
11:29
the next video
11:31
now we will use this library to print
11:33
something on the lcd
11:35
in the main file include the lcd header
11:38
file
11:40
now in the main function first we need
11:42
to initialize the lcd
11:45
remember the i2c must be initialized
11:47
before we initialize the lcd
11:50
lcd put cursor will put the cursor in
11:53
the zeroth row and zeroth column which
11:55
is in the beginning of the top row
12:01
we will send the string hello world to
12:04
this position
12:08
then we will put the cursor at the first
12:10
row and zeroth column that is in the
12:12
beginning of the bottom row
12:14
and we will print another string there
12:17
that's it let's build the code now
12:30
there are no errors so we will run it
12:32
using the run configuration in the idf
12:35
application tab
12:36
we will also check the logs to see if
12:39
there is any error
12:40
make sure the serial port is correct
12:43
here
12:48
the log is showing errors in sending
12:50
commands and data to the lcd
12:55
remember we defined them in the i2c lcd
12:58
source file
12:59
all right the error is because of this
13:01
address
13:03
this address is 8 bits long which
13:05
include the read or write bit also
13:08
since the address here is shifted left
13:10
by one place means the esp32 functions
13:13
also take the 7-bit address just like
13:15
arduino
13:17
so we will shift this address to the
13:19
right by one place making it a 7-bit
13:22
address
13:23
all right let's build and run the code
13:25
again
13:41
you can see in the console there are no
13:44
errors this time
13:45
the lcd is displaying our strings hello
13:49
world in the top row and from esp32 in
13:52
the bottom row
13:54
so things have been working pretty good
13:56
so far
13:57
remember that the lcd can only print
13:59
ascii characters so if you want to
14:01
display numbers you need to change them
14:04
to respective ascii characters first
14:07
for example if i want to print any
14:09
number first of all i need to define a
14:12
buffer where i can store the converted
14:14
ascii characters
14:20
we will use s printf to change the
14:22
number to the characters
14:28
then simply put the cursor where you
14:30
want to print the number and send the
14:33
buffer to the display
14:44
you can see the number is being
14:46
displayed on the lcd
14:48
similarly say if we are displaying the
14:50
float value we need to change the
14:53
formatting in the s print f and the rest
14:55
will remain the same
15:21
the l c d is displaying the float number
15:23
as well
15:25
basically it can display anything in the
15:27
ascii format
15:29
i hope you understood how to use the
15:31
library to display the data on the lcd
15:35
if you are only watching this channel
15:37
for the esp32
15:39
and want to understand how the library
15:41
was written check out the very next
15:43
video
15:45
this is it for this video
15:47
you can download the code from the link
15:49
in the description
15:52
keep watching and have a nice day ahead
15:56
[Music]
16:16
you