How to use UART in ESP32 || Espressif IDE
2 views
Jun 13, 2022
WATCH THE UPDATED VIDEO :::: https://youtu.be/qa7-2lVAghc
View Video Transcript
0:01
[Music]
0:09
hello and welcome to controllers tech
0:13
few months ago i made a video on getting
0:16
started with esp32 mcu and the espressif
0:19
id
0:20
i have decided to continue the esp32
0:23
series so this is basically the second
0:26
video in the esb 32 series
0:29
today we will see how to use uart in
0:32
esp32 and we will transmit and receive
0:35
data from the computer using the uart
0:37
peripheral
0:38
we will use a built-in example which we
0:41
will modify as per our need i will also
0:43
explain the code as we progress through
0:45
the video
0:47
i have downloaded this esp32 data sheet
0:50
and let's check the uart section in this
0:58
as mentioned here the esp32 has three
1:01
uarts uart 0 1 and 2.
1:05
the communication speed can be up to 5
1:08
megabits per second
1:10
we have the hardware flow control and
1:12
dma is also available to manage the data
1:15
transfer
1:17
today's tutorial will cover the simplest
1:19
way to use uart which is using it in the
1:22
blocking mode where we wait for the data
1:24
to be received in a particular interval
1:26
of time
1:27
the more complex methods like d-m-a and
1:30
interrupt will be covered later
1:33
you can read about the u-art in detail
1:35
in this section
1:37
here you will find the uart features the
1:40
registers the interrupts available etc
1:43
but since we are only going to use the
1:45
simple way we will see this part in some
1:48
other video
2:00
let's start the expressive id
2:03
in the meantime let me show you the
2:05
connections
2:08
am going to use these pins rx2 and tx2
2:12
these are basically the rx and tx pins
2:14
for the uart2
2:16
as you can see the pins are x2 and tx2
2:20
are connected to the ftdi the usb to
2:23
serial device
2:25
there must be a cross connection
2:27
connecting the rx pin from the esp to tx
2:30
of the fddi and the tx pin from the esb
2:33
should be connected to the rx pin of the
2:35
f d d i
2:37
the uart is always connected in this way
2:39
no matter which m c u you are using
2:45
all right let's create a new expressive
2:48
idf project
2:50
give some name to the project and click
2:53
next
2:54
as i mentioned we will use an example
2:56
template so check this box here
3:00
now navigate to the peripherals
3:02
uart we will use this particular example
3:06
as it has separate tasks for sending and
3:08
receiving data
3:10
the name of the project will be set
3:12
according to the example so you need to
3:15
modify it again
3:17
click finish to generate the project
3:20
here we have our main file
3:23
we will understand this code in detail
3:25
but first we need to build it once
3:28
make sure the target selected is the
3:30
right one and also make sure that the
3:33
com port is correct
3:35
now click build to build the project
3:39
the first build takes a lot of time so i
3:41
will fast forward this particular part
3:50
the build is successful without any
3:52
errors
3:54
if the code still shows errors like it's
3:56
showing here just click anywhere random
3:59
and press enter
4:01
here is the size of the rx buffer
4:04
1024 bytes
4:06
we have to define the rx and tx pins
4:11
as i mentioned i am going to use the ur2
4:14
and as shown in the picture the tx pin
4:16
is gpio17
4:19
and rx pin is gpio16
4:33
next is the initialization function
4:36
here we have the u art configuration
4:39
first
4:40
we are using the board rate of 11 5 200
4:43
with eight data bits no parity check and
4:46
one stop bit
4:48
the hardware flow control is disabled
4:51
and the source clock for the uart is the
4:53
apb clock
4:56
now inside the initialization function
4:59
we will install the uart driver first
5:02
the first parameter is the uart instance
5:05
we are using
5:06
as i mentioned that i am going to use
5:08
the uart 2 for this tutorial i am
5:11
defining it globally
5:18
and now we can simply replace it
5:20
everywhere in the code
5:31
so if you are using any other uart you
5:34
just simply need to change here
5:37
coming back to the uart driver install
5:39
function the first parameter is the uart
5:42
instance then we have the rx buffer size
5:45
which you can set according to your
5:46
requirement
5:48
we are not using the tx buffer so it is
5:51
set to zero
5:53
we are not using any q so the event q
5:56
and u art q are set to zero
5:59
the last parameter is the interrupt flag
6:01
which we are not using for now so again
6:04
this is set to zero
6:06
after installing the driver we will
6:08
configure the uart using the
6:10
configuration we set earlier
6:13
finally we need to configure the uart
6:15
pins
6:17
this function also takes the uart
6:19
instance as the first parameter then we
6:21
have the number of the tx pin which we
6:23
have defined as 17 the rx pin is defined
6:27
as 16.
6:29
we are not using the hardware flow
6:31
control so the cts and rts pins must not
6:35
be defined
6:36
we set them to uart pin no change which
6:39
is actually minus 1.
6:42
so in order to initialize the uart we
6:44
need to install the driver first then
6:47
configure the parameters and finally set
6:49
the pins
6:51
now let's take a look at the tx task
6:54
i am not planning to log the transmitted
6:57
data so let's delete these logging
6:59
functions from here
7:06
instead of sending the data using a
7:07
separate function we will call the u
7:10
outright bytes inside the tx task itself
7:17
here the parameters of the uart instance
7:19
the data and the length of the data
7:31
the t-x task will send data every 2
7:33
seconds
7:40
next is the r-x task
7:43
we will log the received data into the
7:46
terminal so first we need to define the
7:48
tag
7:52
then set the esp log level to info
7:55
there are other log levels also you can
7:58
check them here
8:03
then we will create a buffer to store
8:05
the received data
8:11
to receive the uart data we will call
8:14
the function uart read bytes
8:17
the parameters are the uart instance the
8:20
destination buffer the length of the
8:22
data to be received and the time out for
8:24
the data to arrive
8:26
this function waits for the specified
8:28
time for the data and if the required
8:30
amount of data is received or if the
8:33
timeout occurs the data will be stored
8:35
in the rx buffer
8:38
let me change the timeout to 500
8:40
milliseconds
8:42
the function returns the number of bytes
8:44
received via the uart which will be
8:46
stored in the variable rx bytes
8:49
if we have indeed received some data we
8:52
will first set the very next position in
8:54
the buffer as the terminating character
8:57
this will help the log function to
8:58
identify the end of the string
9:01
then we will log the data to the
9:03
terminal using the function e s b log i
9:06
here i indicate the info
9:08
we are displaying the number of bytes
9:10
received and the data itself
9:13
we don't need to hex dump the data so
9:15
delete this function
9:22
finally we come to the main function
9:25
here we will first initialize the uart
9:28
then we will create 2 tasks the rx task
9:32
and the tx task
9:34
the stack size for both is set to 2
9:36
kilobytes and the priority for the rx
9:38
task is set to highest and the tx task
9:41
is set to 1 lower than that
9:44
once the tasks are created the rx task
9:47
will start waiting for the data to be
9:49
received while the tx task will start
9:51
transmitting every 2 seconds
9:54
build the project now
9:59
we need to choose a run configuration so
10:02
choose the one under e s b i d f
10:04
application
10:09
i am using the real term serial monitor
10:13
the baud rate should be eleven five two
10:15
hundred
10:17
the f-t-d-i is connected to the com port
10:20
7.
10:23
the data is transmitting but i need to
10:25
make a little modification
10:28
i am adding the carriage return and the
10:30
data size is 13 now
10:34
let's test it again
10:49
all right the data is transmitting well
10:51
and now we will open the terminal
10:58
choose the serial monitor here
11:00
choose the project name and the correct
11:02
com port where the esb is connected
11:11
let's try to send some data to the e s p
11:23
you can see the received data has been
11:25
logged into the terminal
11:27
it's due to this log function we have
11:30
used
11:36
you can see we are able to transmit data
11:38
every 2 seconds and the esp is also able
11:42
to receive data accurately
11:44
now let's modify the code so that we
11:47
would be able to send a modified data
11:49
each time
11:55
i am defining an integer which we will
11:57
increment later in the code
12:00
inside the tx task we will first create
12:03
a new buffer to store the data to be
12:05
transmitted
12:09
now we will store the data in the buffer
12:12
here i am incrementing the value of the
12:14
num variable so that a new value is sent
12:17
every time the task is executed
12:24
let's keep this outside and we will free
12:27
the buffer in the end
12:38
there is some error about the unsigned
12:40
int so change it to character pointer
12:52
we also need to modify the data length
12:56
all right let's build the code and run
12:58
again
13:09
the e s b is sending data and you can
13:12
see the index value is updating
13:15
so the data is transmitting fine and now
13:18
we will send the data from the computer
13:20
to the esp
13:22
the data we sent is received
13:25
successfully
13:28
so we were able to send and receive data
13:31
with the esp 32 and computer using the
13:34
uart
13:35
i hope you understood the process and
13:37
working
13:39
i begun the series with uart because i
13:41
might try to interface this esp32 with
13:44
the sdm 32 and uart is the easiest way
13:47
to do it
13:49
you can download the code from the link
13:51
in the description
13:53
leave comments in case of any doubt
13:56
keep watching and have a nice day ahead
14:07
do
14:21
you
#Development Tools
#Programming