ESP32 – IDF Programming Getting Started

Today we are going to see ESP32-IDF getting started.

ESP32-IDF getting started

To develop applications for ESP32 you need:

  • PC loaded with either Windows, Linux or Mac operating system
  • Toolchain to build the Application for ESP32
  • ESP-IDF that essentially contains API for ESP32 and scripts to operate the Toolchain
  • A text editor to write programs (Projects) in C, e.g. Eclipse
  • ESP32 board itself

For setting up the above things please refer Here.

Hello World

Esp-IDF useses FreeRTOS as an operating system. If we want to work in esp-idf, we should have knowledge on How to write a program using FreeRTOS’s API. For FreeRTOS, I will give some separate tutorial later. Whenever i’m using some FreeRTOS API that time itself i will explain that API. So don’t worry guys. If you want to learn basic concepts of RTOS Part 1 please click Here. For RTOS Part 2 please click Here.

Now we are going to see the first example “hello world”. Hello world is the first program for programming, who wants to learn any programming language. So we are also going to see that.

Before that, we have to look in ESP directory. So the directory contains below Sub directory.

+–esp-idf

            |

            + – – components

            |

            + – – docs

            |

            + – – examples

            |

            + – – make

            |

            + – – tools

The components directory holds all the ‘C’ code for the ESP32. It contains all the ‘components’ that make up the ESP32. It includes Drivers for numerous peripherals, the bootloader, BT (bluetooth), freeRTOS etc.

Include Section 

  • freertos/FreeRTOS.h : Inclusion of this sets configuration required to run freeRTOS on ESP32.
  • freertos/task.h: This file includes task related things such as multitasking, task creating, deleting.
  • esp_system.h: This inclusion configures the peripherals in the ESP system. Think of it as system initialization. It’s like setting up the vessels before cooking!

Main Function

So, In ESP-IDF program starts with app_main. It is like main fuction in other platforms. Printf will print the strings in serial console.

esp_restart():

This function restart the esp. So again app_main will prints. Its like while(1).

Full Code

Here we go. Go through the code.

h ESP32 - IDF Programming Getting Started

MULTITASK : LED BLINKING WITH HELLO WORLD 

It is a RTOS. So we can create multiple tasks. Using that method, i am going to create the two tasks.

  1. LED task (It will blink the LED)
  2. Hello world task (It prints in serial console)

Before that, FreeRTOS has many API. Here we are going to use API for Task creating.

xTaskCreate()  API:

            This FreeRTOS API is used to create a task. Using this API we can create more number of tasks.

portBASE_TYPE xTaskCreate  (      pdTASK_CODE pvTaskCode,

                                  const signed portCHAR * const pcName,

                                  unsigned portSHORT usStackDepth,

                                  void *pvParameters,

                                  unsigned portBASE_TYPE uxPriority,

                                  xTaskHandle *pxCreatedTask );

  • pvTaskCode: a pointer to the function where the task is implemented. (Address of the fuction)
  • pcName: given name to the task. This is useless to FreeRTOS but is intented to debugging purpose only.
  • usStackDepth: length of the stack for this task in words. The actual size of the stack depends on the micro controller.
  • pvParameters: a pointer to arguments given to the task.
  • uxPriority: priority given to the task, a number between 0 and MAX_PRIORITIES – 1.
  • pxCreatedTask: a pointer to an identifier that allows to handle the task. If the task does not have to be handled in the future, this can be leaved NULL.

VtaskDelay() API:

            We are using this API for delay purpose.

 

gpio_pad_select_gpio():

This API is used for select the GPIO to use. Here we are selecting GPIO13.

 

gpio_set_direction():

This function is used to set the GPIO to weather output or input.

 

gpio_set_level():

If i select that GPIO as a output, We can set the level of the GPIO using this function.

1   –   High

0   –   Low

CODE

Output

Here i haven’t connected LED. But if you connect LED it will blink.

hello-world ESP32 - IDF Programming Getting Started

Please let us know if you have any doubt by commenting below. Thank you 🙂 .

 

Download our new Android app. You can learn all Embedded Tutorials from your Android Phone easily.

Click Here to Download App!