ESP32 - How to create your First ESP IDF project (From Scratch)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi there today we are going to learn how to create our first esp-idf project from scratch we will learn how to set up and create a simple example application for reading and writing inputs and outputs let's get started [Music] hi my name is yurijar and this is my third video of my esp32 series if you haven't seen my previous ones yet make sure to check them out because we are going to use the same idf on visual studio code setup today we are going to go through setting up our project and learning how to use the menu config a very useful tool to configure the main settings for our idev project if you are new to the channel you may be asking why use this thing called idf at all what's the reason basically idf is a framework created by expressive that allows us to have total control of everything in our firmware which normally is very difficult to do using different approaches such as arduino ides or even python don't get me wrong those are usually better for quick and simple applications but let's say you want to have many tasks running at the same time reading multiple sensors communicating constantly through wifi while handling multiple interactions everything real time the best way to do this is straight through idf to get things started let's open visual studio code then go to file open folder and create a new folder for our new project just remember to keep it simple without spaces or special characters to avoid build problems let's open it now open a new idf term now and type idf dot pi create dash project dash p and dot and the name of our new project for example i will use gpio underline example this command will create an issue structure for our project in the current directory generating all the basic files for us keep in mind that you cannot use some reserved words alone such as project string install and some others you can find a list of reserve words in description now let's go through the project files and have a look at how an initial project looks like here in the main folder we basically have a c file with our project name which is where our main function goes and also we'll see a file called cmakelist.txt this file is important because here we need to tell the compiler where our source code files are located if you create a new c file for example we need to specify it here you'll notice that there is another cmake list file in the root folder this one contains the name of our project and you can rename it by changing the text here if you want these files are used by cmake which is a build system used by idf that uses scripts called simic lists to generate build files you can find more about it in the idf documentation the next step is configuring our project using a tool called menu config menu configs are visual to use to set up our build options and customize our project through this menu we can add and remove features in our firmware if you are familiar with linux you may have used make menu config before it's the same idea to access the menu we just need to open a new terminal and type start idf.pi menuconfig in order to make my life easier i prefer adding a button using the extension action buttons press f1 and type settings.json and add the following command in the action button section name menu config command start idf.pi menu config save the file and then click on the refresh button down here on this menu now let's use the button to open the menu config this is how the menu config looks like we can navigate using the up and down keys and pressing enter to go inside the sub menu to go back just press ask and to enable disable features just press the space bar also pressing the question mark key displays more information and a brief description of how a particular feature works there are many interesting features on idaf such as friatus file system support lwp and many others these are really helpful too so now let's use the menu config to set up the basics of our example project first i will show you how we can change the clock speed of the esp32 core go to component config and press enter or right arrow to enter this menu now go to esp32 specific and then to cpu frequency and now we can change it from 106 megahertz to megahertz another important thing is to change our flash memory size let's press ask to return to the previous menu and then go to serial flasher config and select flash size if you remember my debugging video we have seen that by default the memory is configured to use 2 megabytes only and here we can change it to whatever our development kit has for example i'm using the esp32 devkit v1 which has 4 megabytes of flash some boards and modules come with different memory sizes so check the one that you have and select the right option in case you select the wrong number i definitely telling the logs that the memory size appears to be wrong this way you can confirm that you are using the right size let's now save the chains by pressing ask and selecting yes after that we can see that the menu config generates some files for us sdk config and sdk config.load which contain our new and previous settings as a backup ok now let's click on the build button and let's run it as you can see when the esp32 initializes we can see the change that we just did the flash memory size now is 4 megabytes and the cpu clock is 240 megahertz here we also can see the idf version that we are running and a version of our firmware by the way this dirty in the idap version just means that some files in the idap folder have been modified it shows for me here because i have previously modified a few example project files but no need to worry about it and this number in the firmware version is that just because we haven't properly defined a version yet we can just create a file named version.txt and type in our desired version for example 1.0.1 then just type on the terminal idf.pi full clean to clean the build folder and then build and flash the chip again now we have a proper version number that can keep track of let's take a quick look at the build folder now here we can see that a lot of files have been created for now the most important ones here are bootloader.bing which is the bootloader binary file that's going to be loaded when flashing the chip partitiontable.bin this file contains the partition address for our firmware i am going to cover it in more details in future videos especially.bing which is the filmer binary file of our project which we will load in our my controller notice that the name of this file will change according with the name of our project esp project.elf this file is used to debug the microcontroller with an external debugger check my debugging video for more details now you are ready to start developing your application so let's make a simple example using gpios to learn the basics of idf the first thing that you need to do is find out how to read an input and activate an output here instead of giving you the functions in the command straight away i will show you where we can find this type of information so we can find anything in the future the best way to find out about functions and how to use idev is naturally by going to espressif's idf documentation let's open the browser and open the idf documentation page open api reference and select the peripheral we want to know about in our case we want a gpio here we can see a lot of information about the ports in our esp32 and how to configure and use them first in order to choose the functions of ggpio we need to add the driver's head as you can see here in the documentation drivers are the piece of code that provides you the ability to change configure and transmit data to the hardware level think of it as the middlemen between the software application and the hardware we can build our own drivers but usually the manufacturer of the mic controller provides a very well implemented driver for us in this case we are going to add this precise gpio driver to our code by typing include driver slash gpio.h now let's solve these red squiggles like we did in my first video we need to have the cc plus extension installed then click on this light bulb and select it did include path settings this will create a ccplusplus properties.json file where you need to add the idf path inside of this include path let's save the file and the red squiggles are gone next we need to configure the gpio that you want to use as an input as you can see here the function to configure a gpio is gpio set direction so let's try using it we can notice that this method expects a few parameters and here's a good tip we can go to the methods definition by pressing f12 and then taking a closer look at these parameters interesting the first parameter expects a variable of the gpio num t type we can check what that is by pressing f12 here as well nice now we know that a variable type gpio non-t is just a simple numeric index for the input numbers so in my case i have a button connect to my gpo22 so i need to use gpion32 here now let's do the same with the other parameter of the gpio set direction function here we can see that if i want to use my gpio as an input i need to use gpio mode input as the second parameter so let's add that now in order to have a stable signal on the pin for us to read we need to add a pull-up resistor to it to fix the level at 2.3 volts luckily the sp-32 has internal resistors and you can configure them as a pull up or put down so we don't need to add anything externally if we look for pull up in the idf gpio page we can find the function that we have to use which is gpao set pull mode well we know the first parameter gpio922 but how about the second one let's look at the definition so to enable pull up we have to use gpio pull up only as the second parameter let's try configuring an output now let's set the direction of our gpio to output in my case pin 26 now we need to read the input level in order to do something with it in the idf documentation we can see that the function that you're looking for is gpio get level we can see that it returns an integer value 1 if the input level is high and 0 if the input level is low so let's make it do something if the input is high otherwise if the input is low we do something else let's try making it turn an led on when the button is pressed in order to activate it the idf documentation says that we can use gpio set level to change the output level now here we need to add a small delay to this loop the esp32 runs a small operating system called freeartos in the background in order to not block its execution we need to use a delay function from friartos called v task delay this will add a delay and also give time to the processor to attend to the background task as well in order to use this function we need to add the three arthouse headers to our code freearthus slash freeattus.h and friartos slash task.h friartus is a big topic and i'll make a separate video series about it in the future so if you are not subscribed yet consider doing it so you don't miss it okay let's build and run it again now let's try the button cool we can see that the led is turned on and off based on the input pretty simple right as always don't forget to like and subscribe so you don't miss my future videos thanks for watching and see in the next one you
Info
Channel: Yuri R
Views: 63,035
Rating: undefined out of 5
Keywords: Firmware, STM32, adafruit, arduino, c++, embedded, embedded c, embedded c++, embedded systems, esp-idf, esp32, esp32 datasheet, esp32 mesh, esp32 mesh network, esp32 project, esp32 tutorial, esp32 visual studio code, espressif, espressif esp32, espressif esp32 tutorial, espressif idf, firmware, freertos, getting started, how-to, mesh network, programming, sparkfun, ttgo, tutorial, visual studio, visual studio code, vs code, windows, yuri r, esp idf, electronics, iot, what is firmware
Id: oHHOCdmLiII
Channel Id: undefined
Length: 13min 10sec (790 seconds)
Published: Mon May 16 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.