ESP32 #2. How to use UART in ESP32 || Espressif IDE

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hello and welcome to controllers tech few months ago i made a video on getting started with esp32 mcu and the espressif id i have decided to continue the esp32 series so this is basically the second video in the esb 32 series today we will see how to use uart in esp32 and we will transmit and receive data from the computer using the uart peripheral we will use a built-in example which we will modify as per our need i will also explain the code as we progress through the video i have downloaded this esp32 data sheet and let's check the uart section in this as mentioned here the esp32 has three uarts uart 0 1 and 2. the communication speed can be up to 5 megabits per second we have the hardware flow control and dma is also available to manage the data transfer today's tutorial will cover the simplest way to use uart which is using it in the blocking mode where we wait for the data to be received in a particular interval of time the more complex methods like d-m-a and interrupt will be covered later you can read about the u-art in detail in this section here you will find the uart features the registers the interrupts available etc but since we are only going to use the simple way we will see this part in some other video let's start the expressive id in the meantime let me show you the connections am going to use these pins rx2 and tx2 these are basically the rx and tx pins for the uart2 as you can see the pins are x2 and tx2 are connected to the ftdi the usb to serial device there must be a cross connection connecting the rx pin from the esp to tx of the fddi and the tx pin from the esb should be connected to the rx pin of the f d d i the uart is always connected in this way no matter which m c u you are using all right let's create a new expressive idf project give some name to the project and click next as i mentioned we will use an example template so check this box here now navigate to the peripherals uart we will use this particular example as it has separate tasks for sending and receiving data the name of the project will be set according to the example so you need to modify it again click finish to generate the project here we have our main file we will understand this code in detail but first we need to build it once make sure the target selected is the right one and also make sure that the com port is correct now click build to build the project the first build takes a lot of time so i will fast forward this particular part the build is successful without any errors if the code still shows errors like it's showing here just click anywhere random and press enter here is the size of the rx buffer 1024 bytes we have to define the rx and tx pins as i mentioned i am going to use the ur2 and as shown in the picture the tx pin is gpio17 and rx pin is gpio16 next is the initialization function here we have the u art configuration first we are using the board rate of 11 5 200 with eight data bits no parity check and one stop bit the hardware flow control is disabled and the source clock for the uart is the apb clock now inside the initialization function we will install the uart driver first the first parameter is the uart instance we are using as i mentioned that i am going to use the uart 2 for this tutorial i am defining it globally and now we can simply replace it everywhere in the code so if you are using any other uart you just simply need to change here coming back to the uart driver install function the first parameter is the uart instance then we have the rx buffer size which you can set according to your requirement we are not using the tx buffer so it is set to zero we are not using any q so the event q and u art q are set to zero the last parameter is the interrupt flag which we are not using for now so again this is set to zero after installing the driver we will configure the uart using the configuration we set earlier finally we need to configure the uart pins this function also takes the uart instance as the first parameter then we have the number of the tx pin which we have defined as 17 the rx pin is defined as 16. we are not using the hardware flow control so the cts and rts pins must not be defined we set them to uart pin no change which is actually minus 1. so in order to initialize the uart we need to install the driver first then configure the parameters and finally set the pins now let's take a look at the tx task i am not planning to log the transmitted data so let's delete these logging functions from here instead of sending the data using a separate function we will call the u outright bytes inside the tx task itself here the parameters of the uart instance the data and the length of the data the t-x task will send data every 2 seconds next is the r-x task we will log the received data into the terminal so first we need to define the tag then set the esp log level to info there are other log levels also you can check them here then we will create a buffer to store the received data to receive the uart data we will call the function uart read bytes the parameters are the uart instance the destination buffer the length of the data to be received and the time out for the data to arrive this function waits for the specified time for the data and if the required amount of data is received or if the timeout occurs the data will be stored in the rx buffer let me change the timeout to 500 milliseconds the function returns the number of bytes received via the uart which will be stored in the variable rx bytes if we have indeed received some data we will first set the very next position in the buffer as the terminating character this will help the log function to identify the end of the string then we will log the data to the terminal using the function e s b log i here i indicate the info we are displaying the number of bytes received and the data itself we don't need to hex dump the data so delete this function finally we come to the main function here we will first initialize the uart then we will create 2 tasks the rx task and the tx task the stack size for both is set to 2 kilobytes and the priority for the rx task is set to highest and the tx task is set to 1 lower than that once the tasks are created the rx task will start waiting for the data to be received while the tx task will start transmitting every 2 seconds build the project now we need to choose a run configuration so choose the one under e s b i d f application i am using the real term serial monitor the baud rate should be eleven five two hundred the f-t-d-i is connected to the com port 7. the data is transmitting but i need to make a little modification i am adding the carriage return and the data size is 13 now let's test it again all right the data is transmitting well and now we will open the terminal choose the serial monitor here choose the project name and the correct com port where the esb is connected let's try to send some data to the e s p you can see the received data has been logged into the terminal it's due to this log function we have used you can see we are able to transmit data every 2 seconds and the esp is also able to receive data accurately now let's modify the code so that we would be able to send a modified data each time i am defining an integer which we will increment later in the code inside the tx task we will first create a new buffer to store the data to be transmitted now we will store the data in the buffer here i am incrementing the value of the num variable so that a new value is sent every time the task is executed let's keep this outside and we will free the buffer in the end there is some error about the unsigned int so change it to character pointer we also need to modify the data length all right let's build the code and run again the e s b is sending data and you can see the index value is updating so the data is transmitting fine and now we will send the data from the computer to the esp the data we sent is received successfully so we were able to send and receive data with the esp 32 and computer using the uart i hope you understood the process and working i begun the series with uart because i might try to interface this esp32 with the sdm 32 and uart is the easiest way to do it you can download the code from the link in the description leave comments in case of any doubt keep watching and have a nice day ahead do you
Info
Channel: ControllersTech
Views: 29,095
Rating: undefined out of 5
Keywords: ESP32, wroom, getting, started, espressif, ide, idf, uart, tx, rx, tasks, async, example, tutorial, eclipse, ESP, 32, arduino, serial, data, computer, ftdi, terminal, monitor
Id: 15zXAhsVx0k
Channel Id: undefined
Length: 14min 22sec (862 seconds)
Published: Mon Jun 13 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.