ESP32 #3. How to use I2C in ESP32 || LCD1602 || Espressif IDE

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hello and welcome to controllers tech this is another video in the esb 32 series and today we will see another peripheral the i2c i2c or i squared c or iic stands for inter-integrated circuit and it is a bus interface connection protocol incorporated into devices for serial communication we have covered a lot of videos in stm 32 series where we used i-2-c to communicate with the sensor or to drive some display in order to show how the i2c works i am going to interface a very famous lcd 1602 display with the esp32 today if you have already seen my stm32 video on this display this one is going to be easier for you since we will be using the same library that we have used in sdm 32 otherwise if you are only watching the esp32 series don't worry i will release another video where i will explain how the library was written i couldn't cover everything in this video that is why we will go with two videos so today we will see how to port the i2c lcd library from the stm32 to the esp32 if you are only interested in esp32 just focus on the parts where we make use of the library like how to import it to the project or how to use the functions to display data on the lcd and in the next video i will explain the library so this is the library we will be using today it basically uses the i2 c to interface the lcd with the mcu this is the stm32 related function which writes the data using the i2c we will change it obviously as per the esp 32. the lcd is connected using the p-c-f-8574 which is basically an i-2-c expander here are the data and clock pins for the i-2-c connection if we check our e-s-b-32 it has g-p-i-o-22 for the i-2-c clock and 21 for the data we will be using these pins to connect to the p-c-f you can see the p-c-f is attached to the l-c-d the 2 pins from the p-c-f are connected to the e-s-p-32 they are the data pin which is the green wire connected between the s-d-a of the p-c-f and the d-21 of the e-s-b-32 and the yellow wire is connected between the clock pin of the p-c-f and the d-22 of the e-s-b-32 we need to give 5 volts to the p-c-f for which i will use an external supply since esp32 doesn't have a 5 volts output all right now we have established the connection so let's create a new project in esp-idf we will use an example template so go to peripheral i2c and let's use this i2c simple template let's rename this to i2 clcd so the project is generated successfully and here we have our main file before we proceed let's build it once make sure you select the correct mcu from here all right the build is successful without any errors and if the main file still shows error just click at some empty place and press enter since this is an example template it has some functions configured for the mpu6050 let's delete them all all right here we have the function to initialize the i2c in master mode we will edit this function a little here we have to provide which i2 c we are using since the board i am using has only one i2c so this must be the i2 c0 next we have to input the data and clock pins here you can see the data is pin 21 and clock is pin 22 next parameter sets the pull-up feature for these pins since we are not using any external pull-up resistors let's keep the internal pull-up enabled the last parameter is the clock speed 100 kilohertz is enough for the lcd to work you can also set high speed mode with 400 kilohertz this basically depends on the device specifications all right let's delete these defines from here in the master initialization function after defining the parameters it configures them using the function configure parameters and finally it will install the i2c driver this function takes the parameter as the i2c number which in our case is 0 the i2c mode which is master the length of the rx and tx buffers which is not needed since we are using the master mode and the buffers are only required in the slave mode the last is the interrupt flag which also we are not using today so the buffer lengths should be set to zero this will install the i-2-c driver and we are good to go with our l-c-d let's delete the m-p-u related functions in the main so inside the main function we are initializing the i2c and once it has been initialized we will log the success message now let's include the library files you can just drag and drop them in the main folder open the i2clcd.c file so that we can modify it remove the i2c handler definition the slave address for the p-c-f is zero cross four e the data will be written to the l-c-d in the similar manner but we just need to change the function to write data using e-s-b-32 this part will remain similar across whatever controller you use now let's see what functions are available in the esb 32 i2c library i2 c master right to device the parameters are the i2c number the device address the buffer we want to write the buffer length and the timeout for the function this function should work for us also note here that the device address is being shifted to left by 1 position this info will be needed later all right let's use this function to send the data to the lcd i am defining the error variable the first parameter is the i2c number which is zero in our case then we have the slave address which is defined here next is the buffer we want to send that will be the data t itself the buffer size is 4 bytes and let's keep the time out to be 1 second this function will return zero on success and i guess minus 1 if it fails anyway if the error is not zero we will log this on the console for this we must define the tag first as it's defined in the main file and now we will log using this tag that there is some problem in sending the command the same functions will be used to send the data also so that's all we need to modify in this library in the lcd header file remove the stm32 inclusion let's build it once we have errors regarding the use of i2c number this is because we haven't included the i2c driver yet so let's include this in the lcd library one last and very important thing in order to include a library in the project we must register it in the c make list file in this idf component register we will register a new source file i2 clcd dot c all right let's build it again since we have registered a new source it's going to build the entire project again and you can see the errors regarding the inclusions are gone now i forgot to change this delay function whole delay is used to provide delay in milliseconds so we need to use an equivalent function here in esp32 you sleep is used to put the mcu in sleep but this takes the argument in microseconds so change all the delays to u sleep and modify the parameters we also need to include the header file for this u sleep function so everything is fine now and our library is ready to be used with the esp32 if you are only using esp32 you can just take this library include it in the project and modify the c make list file as i said i will explain the library in the next video now we will use this library to print something on the lcd in the main file include the lcd header file now in the main function first we need to initialize the lcd remember the i2c must be initialized before we initialize the lcd lcd put cursor will put the cursor in the zeroth row and zeroth column which is in the beginning of the top row we will send the string hello world to this position then we will put the cursor at the first row and zeroth column that is in the beginning of the bottom row and we will print another string there that's it let's build the code now there are no errors so we will run it using the run configuration in the idf application tab we will also check the logs to see if there is any error make sure the serial port is correct here the log is showing errors in sending commands and data to the lcd remember we defined them in the i2c lcd source file all right the error is because of this address this address is 8 bits long which include the read or write bit also since the address here is shifted left by one place means the esp32 functions also take the 7-bit address just like arduino so we will shift this address to the right by one place making it a 7-bit address all right let's build and run the code again you can see in the console there are no errors this time the lcd is displaying our strings hello world in the top row and from esp32 in the bottom row so things have been working pretty good so far remember that the lcd can only print ascii characters so if you want to display numbers you need to change them to respective ascii characters first for example if i want to print any number first of all i need to define a buffer where i can store the converted ascii characters we will use s printf to change the number to the characters then simply put the cursor where you want to print the number and send the buffer to the display you can see the number is being displayed on the lcd similarly say if we are displaying the float value we need to change the formatting in the s print f and the rest will remain the same the l c d is displaying the float number as well basically it can display anything in the ascii format i hope you understood how to use the library to display the data on the lcd if you are only watching this channel for the esp32 and want to understand how the library was written check out the very next video this is it for this video you can download the code from the link in the description keep watching and have a nice day ahead [Music] you
Info
Channel: ControllersTech
Views: 18,315
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, lcd, 1602, 16x2, lcd16x2, i2c, display, pcf, 8574
Id: DPmMeOnrXdg
Channel Id: undefined
Length: 16min 17sec (977 seconds)
Published: Thu Jul 14 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.