Getting Started with GPIO | Esp32 | C++ | PlatformIO | ESP-IDF | Visual Studio Code | ESP IDF C++

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi guys welcome to the second session of dsp  32 series in this video i will be explaining   how to use gpio or general purpose input  output pins in esp32 so before moving on   please subscribe to our channel so that you  don't miss out any of our upcoming videos let's get started with gpio for all those who  are already familiar with this concept of gpio   can skip this chapter now a microprocessor  or microcontroller needs to interact with the   outside world so this may include communicating  with other microprocessors or communicating with   other sensors it may also need to access  some of the external memory elements   this is all possible because  of gpio parts or ports one thing i've noticed is that people often  get confused with gpio pins ports and pads   gpio pin is a generic pin whose value  consists of one of the two voltages   either high or low whereas a gpio port is  a platform defined grouping of those pins   have you ever searched for an esp32 pin out and found so many things mentioned for example if we   see here the pin number 5 is defined as gpio36 and is also defined as adc and few other things   this is because they are called as io pads  normally a single io pad has many functionalities   in this video i will not be explaining about the  gpio matrix or the io matrix as its little out of   scope for this chapter but feel free to refer  the official document i have attached the link   in the description now if you go to  the official esp32 reference manual   we will find about the various functions each of this io pad can do so as shown in this table we   can see each io pad performs many functions now apart from this the io circuits also have level   shifters and esd esd means electrostatic discharge protection circuits esp32 chip has 34 gpio pads   and always keep in mind that this gpio pins  operate at 3.3 volts and not at 5 volts so now let's create an empty project so let me give the project let me close this so let   me get the name of the project as esp32 gpio basic and the board which we'll be using is node mcu 32s   and the framework would be esp idf now i will  not be uh using the default location instead i   have my own favorite location which is  the project all my esp32 series videos   will be in this location and by the way uh all  this is under version control which means that uh   all this is available in github so i have attached the link to the github repo in the description so the first thing is always changing  the file type from dot c to dot cpp   so as we'll be using c plus plus and not c now let's wrap this main method in an extern  block before that let me open the zen mode yeah   so let me wrap this with an extern block so  guys if you're not familiar with this extend   block so please refer my previous video where i have explained why do we need the extern block   in order to use and configure the gpio pins  first we need to include the gpio library   so #include driver gpio.h   now that we have included the library let  us learn how to configure the gpio pins so as we can see here there are  two ways to configure the gpio pins   we have a single pin configuration and we have multi-pin configuration or we basically multi-pin   configuration using templates so basically what happens in a single pin configuration is that   we define the pin configuration each time we  use a new pin so for example if there are four   pins with the same configurations okay so  like for example all those are output pins   so we need to define the same configurations  four times for different set of pins   uh it's little bit easier or our better way in  multi-pin configuration so if you're familiar   with the concept of dry or don't repeat yourself  so what we do is instead of repeating the code we   create a template so we create this template and this template will be used by multiple pins so   this avoids lot of repeated codes now let's begin with the hands-on on gpio single pin configuration now the first thing we need to do is we  need to call gpio pad select gpio method   uh so let's do a small recap before uh before explaining why do we use this method so in my   previous chapter uh i had explained that a single esp32 io pad can perform many functions now   the gpio pad select gpio method is called to  select the pad as a gpio function or in simple   words this method will switch the io pad to  gpio mode now this method takes an argument   which is nothing but the gpio number we want to uh work on so interestingly what happens is   esp idf comes with the enum class which has all the pins defined for us based on the board type   so in order to use that pin just type  gpio underscore number and you can find   all the gpio pins that are available so let me  select gpio number two which is the inboard led now let's do a small deep dive so let me click yeah so you guys can find here based on the   target board right we can see there are different gpio pins available so for s2 we have this and   let me scroll for s3 we have this many pins  defined and similarly for c3 and it goes on   like that okay now why is this uh important  and like why is uh different pins defined   now what basically happens is different target boards expose different number of pins so instead   of us worrying about which pins are exposed and which are not and to handle all those things   esp-idf uh like it will handle it so based on the board selected right with the board type we select   so uh this will handle it so for example  if you see my project configuration so in my project platform ini file in my project configuration you can see my board type is   32 yes so only those pins will be shown here so you can see here uh amongst all this right   so let me just scroll a bit here sorry here  yeah so if you can see here uh this one is   highlighted where this uh or not this is because these are the pins that are currently being used   so you can test it actually so what we'll do  is let me select okay so if you see here right   39 pin is available for uh s2 32 but it's not available for   uh c3 c3 has just 22 pins max of 22. so i'll  do one thing so what i will do is i will use   pin 39 and i'll go here and let me go  to pio and projects and configuration   yeah so now what i will do is i'll click on this  uh configure and let's change uh the board type   from uh so let me select any of the c3 version so let us select esp32 yeah esp32 c3 so let me save   this now once i save uh the platform i will start rebuilding this let's wait for it to complete yeah so as you can see once uh the  sync is complete right the rebuilding   so you can see it's throwing an error the error is this is unidentified this is because if we see   this has pins only till 22 so if we go to this  types now you can see here the c3 is highlighted   and earlier we could see that uh the esp32  was highlighted right now it's not that is   because we change the target so if i go back and if i change back this to esp uh node mcu nodemcu 32 yes so if i change this and save the project and if i go back here and let me select 39   so let's wait for the rebuilding to complete  now it's showing it as it's unidentified now   as soon as this rebuilding is finished  right we can see the error will go away yeah see now it's a valid gpio open so if you go back to the gpio types we can see now this is   highlighted and this is not so guys i hope you are clear with uh this gpio uh this enum class   so guys uh make sure you use this uh  enum class while defining the pins so uh it's basically it's better right so   if you don't use this you'll actually be getting a lot of mess so it's the best practice to use this   now before getting into the next step let me just change the pin number back to gpio number two now   the next step would be to set the mode whether we will be using the gpio pin as an input   or output or both as an input and output  for this we need to use the method gpio   set direction so it takes two arguments  one is gpio number and other is the mode   uh let me give the same gpio pin  which is gpio number two now before   uh setting the mode so let us understand what are the different modes that are available so as we see here we have gpio mode  disabled this is used to disable   the input and output next we have gpio  mode input it's for input purpose only   and gpio mode output this is for output only and the next we have gpio mode input output   so in this mode the pin works as both input as well as output next we have gpio mode output od   so od stands for open drain in this the pin  works as output in open drain mode next we have   gpio mode input output od in this the pin works as both input and output but in open drain mode so remember this in open drain mode the pin can only sink current in simple words open drain mode   lacks the pull up capability it  only has pull down capability   so we have to use pull up resistors  in order to perform any of the actions now let me just go back to the gpio yeah so now if we see here right so the gpio set direction method   returns a esp error_t so this is used to written error codes so i will be explaining all these   error codes in detail in the upcoming video but for now remember that if this action is successful   we get esp_ok so let me configure  this pin in output mode so gpio mode output   now there is a macro available which is used to check whether this action is successful or not   so that macro is esp sorry esp error check so let me just paste it here so this macro  will check if this action is successful or not   so we'll be explaining this all  these macros in the upcoming videos   now once this is done let's set the gpio level in order to do this we use the method gpio set level   so this again takes two arguments the first  one being gpio number and second is just the   level so the level can either be zero or one  one refers to high and zero refers to low so   let me use the same gpio gp i sorry gpio  num 2 and for this let me set this as high and this method again returns the same esp  error t so let me again check for error so esp error check oh okay my bad uh error yeah so esp error  check yeah now let us upload this code now suppose i want to connect an external led to  the pin 16 okay i need to rewrite this uh these   three steps all over again so first i need  to select the pad uh gpio pad 16 and then i   have to set the direction and then have to set  the level so for now let me just copy this and   paste it over here so i'll  replace this two with 16 copy paste yeah so now uh in such cases right  so the esp-idf comes up with an   option to create and template so  to create a template we need to use   gpio config t this data structure is used so let  me name this as template template gpio config so guys now what i will do is uh for time being  i will just comment out this piece of code   so that at a later point we can compare  this along with the template configuration   and see actually how useful  is this template configuration   so guys let us see what are the arguments  that are being passed to this data structure   so the first one is pin bit mask so this is  used to set the bit mask or in simple words   this is used to set the pin we want to use  then is the mode whether it's input output   input output both or with od or whatever it is  then we have pull up enable pull down enable   and interrupt type so i will not be covering  these three types in detail in this video   as this is part of the gpio advanced  uh topic so in the later video when i   will be covering the gpa advance i will be  explaining all this this three in detail   now let me go back to main.ccp yeah so now let  me first set the gpio so for this we need to   use gpio cell so this constant is being used so  if you see here here we had used gpo number and   here we are using gpu select so the reason is that  this argument right the pin bitmas pin bit mask so   this takes the argument as a bit mask but here  it was not that here here it was not like that   in this case we were using the number as it is 2  1 however it may be but here it actually expects   the argument to be in the type of bit mask so  that's the reason why we are using this constant   over here and this one over here so don't get  confused with gpa or select and gpio number and   guess also remember that here we can concatenate  the values we can by using a bit wide bitwise or   operator right we can pass multiple values  so let me set gpio cell 16 as well over here   now comes the mode so let me set the mode as gpio  mode output and then we have the interrupt uh then   we have sorry we have the pull up pull down and we  have the interrupt so let me disable the pull up   gpio pull up disable and let  me also disable the pull down and finally let me disable the interrupt as well yeah so now once this is done right   we need to apply this template by  passing this to and function gpio config so you can see the difference  right so let me just remove this so suppose we have a new pin number so let  me add a new pin number and call it as 22 let me replace 22 here and here so in the  single pin mode configuration every time   we need to configure a new pin we need  to define these three things every time   and here also i have not defined the pull up  pull down on all those things so we need to   even define those as well okay so instead of that  we can just define a template so here let me add   gpio cell 22 so that is the beauty of  this template okay so that's it with   the basics of gpo configuration now  let's write a simple blink program so guys in this section i will be explaining  the blink program in the most simplest way   so the ideal way to uh define a blink is  by using free autos tasks and delay but to   simplify the things i will be using a super  loop but surely in my upcoming videos i will   be rewriting this code using free autos just  for the explanation and better understanding   i will be rewriting this code using a super  loop so first let's define a method blink let us call it over here okay so now let us create before that  let us include the logging library esp log dot h and then let us define an attack okay so the first thing what we'll do is we'll  define an infinite loop which will run forever and then let us define a counter whose initial  value is zero we will increment this counter a at the end of the loop cycle okay now let us check if this counter  is less than 500 if so then let us   set the level of this gpio pin which is 2 to high else level will be zero okay let us lock this as well switching off the led and let me copy this switching on the led okay okay and uh what we'll do here is  we'll just check if this counter   is less than thousand if it becomes greater than  sorry not less than if it is greater than thousand   so if it is greater than thousand what  we'll do is we'll reset the counter to zero okay so let us check this once again so in the template we have defined the gpio2 as  an output with all the pull down and pull up   and interrupt disabled then we are applying  the template then we are calling the blink   where we have a loop where if the counter  is less than 500 then we are setting the   level to one high or else we are setting it  to zero switching or switching so i wrote   the opposite this will be on okay and if  the counter becomes greater than thousand   then we'll be resetting it okay this  seems all good so let me upload this okay so the first thing is let's connect the  gpio pins so if we see here uh the vcc of the   ldr sensor is connected to the 3.3 volts  ground is connected to the ground and we   have connected the d out to the gpio 16. now  uh what we'll do is let us remove this for now so let's create a one more  new function and call it as void ldr switch so again here we will have one infinite loop okay and let us create a log esp log d okay so now if you see how we  haven't configured the pin number 16   so what i will do is i'll copy this and let me rename this to template gp okay let me rename this to  gpio output config i will rename this to okay so this is gpio output  config and this would be gpio input config let me pass this over here so we have two templates created now this would be pin number 16 and  instead of output this would be input okay so now let us go back to our ldr  switch here let us do one thing if gpio   get level so basically this gpu get level is used  to know the state of a gpu pin like is it high or   low okay so it returns basically an integer either  one or zero one refers to high and zero refers to   low so let me go back to the main program okay  and here let us pass the gpio number which is 16 so if this equals to high   then what we will do is we will switch  on the onboard led so we'll do set level gpio number 2 to high else we'll switch off the led okay let me align this okay so if uh this  equal stick equals to high then we'll switch   on the led or else we'll switch off so let me  put a log as well so here i will do esp log d tag followed by object okay so let's call it as detected okay in this case let us not put any log okay now uh let me put a semicolon okay so all seems  good let us call the function ldr switch so let us just do a quick check so we are starting  the ldr switch and there is a while loop where   we are starting an infinite loop where if  the gpio level of pin number 16 is equal to 1   then we'll switch on the led or else we'll  switch off the led so let us run this so guys hope this video was helpful guys next week  uh we shall be introducing a new series called as   iot playground and it will be fully hands on  approach so in this series uh in basically   in this iot playground series we shall create  our own pcb and write our code using gsp idf   so like after every uh theory based video right  there will be its corresponding iot playground   video where we should use whatever we have  learnt to solve some real world scenario   so guys please support us by  subscribing to our channel
Info
Channel: AbIoTs Vidya
Views: 15,428
Rating: undefined out of 5
Keywords: esp32 platformio vscode, platformio esp32, esp32 platformio, esp32 c++ programming, esp32 c++ tutorial, esp32, esp32 tutorial, esp32 datasheet, esp-idf tutorial, esp-idf vscode, esp-idf, esp idf, esp idf tutorial, esp idf vscode, esp idf visual studio code, platformio vscode, esp32 espressif, esp32 esp-idf tutorial, esp32 esp-idf, esp32 c++, esp32 c++ example, esp c++, esp idf c++, Espressif esp32, platformio, c++, esp32 gpio, esp32 gpio example, esp-idf gpio, Esp idf gpio
Id: r3yziWNAfe0
Channel Id: undefined
Length: 32min 24sec (1944 seconds)
Published: Tue Mar 08 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.