ESP32 GPIO Tutorial Using ESP-IDF - LED Blinking Example
ESP32 GPIO Tutorial Using ESP-IDF - LED Blinking Example
2024 15:59 ESP32 GPIO Tutorial Using ESP-IDF | LED Blinking Example
Menu
Home → Tutorials → Wireless → ESP32 → IDF → ESP32 GPIO Tutorial Using ESP-IDF – LED
Blinking and Push Button Example
→
Table of Contents
https://embetronicx.com/tutorials/wireless/esp32/idf/esp32-gpio-tutorial-using-esp-idf/ 1/28
31.10.2024 15:59 ESP32 GPIO Tutorial Using ESP-IDF | LED Blinking Example
This article is the continuation of the Series on the ESP32 Tutorials using ESP-IDF and carries the
discussion on the ESP32 and its peripherals. The aim of this series is to provide easy and practical
examples that anyone can understand. In our previous post, we have seen how to setup the Visual
→
Studio Code for ESP-IDF and executed the hello world program. In this post, we will see ESP32
Table of Contents
Please read the GPIO article which explains how the GPIO works in all microcontroller and types of
the GPIO.
Table of Contents
1. Hardware Required
2. Introduction
3. ESP32 30 Pin Development board Pinout
4. ESP32 38 Pin Development board Pinout
5. ESP32 Pins
6. ESP32 GPIO Registers
7. ESP32 GPIO Driver APIs
8. LED Interfacing
8.1. Connection
8.2. Source Code – ESP32 GPIO Example (Blinky)
8.3. Demo – ESP32 GPIO Example (Blinky)
9. Push Button Interfacing – Without Interrupt
9.1. Connection
9.2. Source Code – ESP32 GPIO Input
9.3. Demo – ESP32 GPIO Input
https://embetronicx.com/tutorials/wireless/esp32/idf/esp32-gpio-tutorial-using-esp-idf/ 2/28
31.10.2024 15:59 ESP32 GPIO Tutorial Using ESP-IDF | LED Blinking Example
Hardware Required
PC
ESP32 Board
LED
Resistor (To limit the current through the LED)
Connecting wires
Bread Board
Introduction
The ESP32 is a versatile microcontroller that is widely used in the field of embedded system and IoT
→
(Internet of Things). ESP32 has 48 pins which can do multiple functions. But not all the pins are
Table of Contents
exposed in the development board. Few pins cannot be used also. There are multiple ESP32
In this series, we are going to use two ESP32 development boards which are 30 pin board and 38
pin board.
Before looking into the GPIO, we will see the pinout of the ESP32.
https://embetronicx.com/tutorials/wireless/esp32/idf/esp32-gpio-tutorial-using-esp-idf/ 3/28
31.10.2024 15:59 ESP32 GPIO Tutorial Using ESP-IDF | LED Blinking Example
154 80 TL
The below image illustrates the ESP-WROOM-32 chip pinout of 30 Pin ESP32 Development board.
→
Table of Contents
The below image illustrates the ESP-WROOM-32 chip pinout of 38 Pin ESP32 Development board.
https://embetronicx.com/tutorials/wireless/esp32/idf/esp32-gpio-tutorial-using-esp-idf/ 4/28
31.10.2024 15:59 ESP32 GPIO Tutorial Using ESP-IDF | LED Blinking Example
ESP32 Pins
ESP32 pins are used for multiple functions. Those pin supports the below functionalities.
→
Table of Contents
2 I2C interfaces
16 PWM output channels
2 Digital-to-Analog Converters (DAC)
2 I2S interfaces
In this tutorial, we are going to see on the GPIO functionality. We will see other functionalities later
part of this ESP32 tutorial series. The ESP32 chip features 34 physical GPIO pins (GPIO0 ~
GPIO19, GPIO21 ~ GPIO23, GPIO25 ~ GPIO27, and GPIO32 ~ GPIO39). Each pin can be used as
a general-purpose I/O, or be connected to an internal peripheral signal.
GPIO34 to GPIO39 are only input pins and it won’t support output. These pins don’t have internal
https://embetronicx.com/tutorials/wireless/esp32/idf/esp32-gpio-tutorial-using-esp-idf/ 5/28
31.10.2024 15:59 ESP32 GPIO Tutorial Using ESP-IDF | LED Blinking Example
16,50 ₺
KEŞFET
The table below provides more information on pin usage, and please note the comments in the table
for GPIOs with restrictions.
→
GPIO1 TXD
Table of Contents
GPIO3 RXD
GPIO6 SPI0/1
GPIO7 SPI0/1
GPIO8 SPI0/1
GPIO9 SPI0/1
GPIO10 SPI0/1
GPIO11 SPI0/1
https://embetronicx.com/tutorials/wireless/esp32/idf/esp32-gpio-tutorial-using-esp-idf/ 6/28
31.10.2024 15:59 ESP32 GPIO Tutorial Using ESP-IDF | LED Blinking Example
GPIO16 SPI0/1
GPIO17 SPI0/1
GPIO18
GPIO19
GPIO21
→
GPIO22
Table of Contents
GPIO23
https://embetronicx.com/tutorials/wireless/esp32/idf/esp32-gpio-tutorial-using-esp-idf/ 7/28
31.10.2024 15:59 ESP32 GPIO Tutorial Using ESP-IDF | LED Blinking Example
Note: While booting we need to keep the below GPIOs in respective states. These pins are
GPIO Analog Function RTC GPIO Comments
called as strapping pins.
GPIO38 ADC1_CH2 RTC_GPIO2 GPI
GPIO 0
GPIO39 ADC1_CH3 RTC_GPIO3 GPI
GPIO 2
GPIO 4
GPIO 5 (must be HIGH during boot)
GPIO 12 (must be LOW during boot)
GPIO 15 (must be HIGH during boot)
These are used to put the ESP32 into bootloader or flashing mode. On most development boards
with built-in USB/Serial, you don’t need to worry about the state of these pins. The board puts the
However, if you have peripherals connected to those pins, you may have trouble trying to upload
new code, flashing the ESP32 with new firmware or resetting the board. If you have some
→
peripherals connected to the strapping pins, and you are getting trouble uploading code or flashing
Table of Contents
the ESP32, it may be because those peripherals are preventing the ESP32 to enter the right mode.
After resetting, flashing, or booting, those pins work as expected.
In ESP32, GPIO registers are memory-mapped registers that control the behavior of each GPIO pin.
But when we use the ESP-IDF, we no need to bother about the registers. Because, it has GPIO
driver. So, we can use that GPIO driver APIs in our code.
https://embetronicx.com/tutorials/wireless/esp32/idf/esp32-gpio-tutorial-using-esp-idf/ 8/28
31.10.2024 15:59 ESP32 GPIO Tutorial Using ESP-IDF | LED Blinking Example
ESP-IDF GPIO driver has few APIs to control the GPIO functionalities. We will see few important
APIs.
API Details
gpio_set_level(gpio_num_t gpio_num, uint32_t This function set the GPIO state to high
level) or Low.
https://embetronicx.com/tutorials/wireless/esp32/idf/esp32-gpio-tutorial-using-esp-idf/ 9/28
31.10.2024 15:59 ESP32 GPIO Tutorial Using ESP-IDF | LED Blinking Example
LED Interfacing
LED interfacing with ESP32 involves connecting an LED to one of the GPIO pins of the ESP32 and
controlling its state (on/off) through code. In this demo, we are going to use two GPIOs (GPIO2
which is connected to on board LED and GPIO25).
Connection
Connect the anode (+) as positive to the LED to a GPIO 2 pin on the ESP32.
Connect the cathode (-) as negative to the starting point of the resistor.
https://embetronicx.com/tutorials/wireless/esp32/idf/esp32-gpio-tutorial-using-esp-idf/ 10/28
31.10.2024 15:59 ESP32 GPIO Tutorial Using ESP-IDF | LED Blinking Example
Create the empty project and copy paste the below code to the main.c.
1. #include <stdio.h>
2. #include "freertos/FreeRTOS.h"
3. #include "freertos/task.h"
4. #include "driver/gpio.h"
5. #include "esp_log.h"
6. #include "sdkconfig.h"
7.
8. static const char *TAG = "ETX_BLINKY";
9.
10. #define BLINK_GPIO_1 2
11. #define BLINK_GPIO_2 25
12.
13. void app_main(void)
14. {
15. /* Reset the pin */
16. gpio_reset_pin(BLINK_GPIO_1);
17. gpio_reset_pin(BLINK_GPIO_2);
18.
19. /* Set the GPIOs to Output mode */
20. gpio_set_direction(BLINK_GPIO_1, GPIO_MODE_OUTPUT);
21. gpio_set_direction(BLINK_GPIO_2, GPIO_MODE_OUTPUT);
22.
23. while (1)
https://embetronicx.com/tutorials/wireless/esp32/idf/esp32-gpio-tutorial-using-esp-idf/ 11/28
31.10.2024 15:59 ESP32 GPIO Tutorial Using ESP-IDF | LED Blinking Example
24. {
25. gpio_set_level(BLINK_GPIO_1, 1);
26. gpio_set_level(BLINK_GPIO_2, 1);
27. vTaskDelay(1000 / portTICK_PERIOD_MS);
28. ESP_LOGI(TAG, "Turning the LED %s!","ON");
29.
30. gpio_set_level(BLINK_GPIO_1, 0);
31. gpio_set_level(BLINK_GPIO_2, 0);
32. vTaskDelay(1000 / portTICK_PERIOD_MS);
33. ESP_LOGI(TAG, "Turning the LED %s!","OFF");
34. }
35. }
Overall, this code initializes a GPIO pin on the ESP32, configures it as an output, and then enters an
infinite loop, continuously turning an LED connected to GPIO pin ON and OFF with a 1000-mili
second delay. Log messages are printed to indicate the state of the LED.
→
Table of Contents
Ad Ad
https://embetronicx.com/tutorials/wireless/esp32/idf/esp32-gpio-tutorial-using-esp-idf/ 12/28
31.10.2024 15:59 ESP32 GPIO Tutorial Using ESP-IDF | LED Blinking Example
In this example we are going to set one GPIO (GPIO25) as a input and another GPIO (GPIO2) as a
output. When we press the button, the LED will be ON and when we release the button, the LED will
be OFF.
Connection
Connect the one end of the push button to GPIO25 and another end of the push button to
GND.
Connect the LED to GPIO2.
Create the empty project and copy paste the below code to the main.c.
1. #include <stdio.h>
2. #include "freertos/FreeRTOS.h"
3. #include "freertos/task.h"
4. #include "driver/gpio.h"
5. #include "esp_log.h"
6. #include "sdkconfig.h"
https://embetronicx.com/tutorials/wireless/esp32/idf/esp32-gpio-tutorial-using-esp-idf/ 13/28
31.10.2024 15:59 ESP32 GPIO Tutorial Using ESP-IDF | LED Blinking Example
7.
8. static const char *TAG = "ETX_PUSH_BUTTON";
9.
10. #define BLINK_GPIO_1 2
11. #define BLINK_GPIO_2 25
12.
13. void app_main(void)
14. {
15. /* Reset the pin */
16. gpio_reset_pin(BLINK_GPIO_1);
17. gpio_reset_pin(BLINK_GPIO_2);
18.
19. /* Set the GPIOs to Output mode */
20. gpio_set_direction(BLINK_GPIO_1, GPIO_MODE_OUTPUT);
21. gpio_set_direction(BLINK_GPIO_2, GPIO_MODE_INPUT);
22.
23. /* Enable Pullup for Input Pin */
24. gpio_pullup_en(BLINK_GPIO_2);
25.
26. while (1)
27. {
28. if( gpio_get_level(BLINK_GPIO_2) == 0 )
29. {
30. /* Button is pressed. Turn on the LED */
31. gpio_set_level(BLINK_GPIO_1, 1);
32. ESP_LOGI(TAG, "Turning the LED %s!","ON");
33. }
→ 34. else
Table of Contents
35. {
36. /* Button is released. Turn off the LED */
37. gpio_set_level(BLINK_GPIO_1, 0);
38. }
39. }
40. }
Ad Ad
https://embetronicx.com/tutorials/wireless/esp32/idf/esp32-gpio-tutorial-using-esp-idf/ 14/28
31.10.2024 15:59 ESP32 GPIO Tutorial Using ESP-IDF | LED Blinking Example
Connect the one end of the push button to GPIO25 and another end of the push button to
GND.
Connect the LED to GPIO2.
Create the empty project and copy paste the below code to the main.c. The below code toggles the
LED with the each button press.
1. #include <stdio.h>
2. #include "freertos/FreeRTOS.h"
3. #include "freertos/task.h"
4. #include "driver/gpio.h"
5. #include "esp_log.h"
6. #include "sdkconfig.h"
7. #include <inttypes.h>
8.
9. #define BLINK_GPIO_1 2
10. #define BLINK_GPIO_2 25
11.
https://embetronicx.com/tutorials/wireless/esp32/idf/esp32-gpio-tutorial-using-esp-idf/ 15/28
31.10.2024 15:59 ESP32 GPIO Tutorial Using ESP-IDF | LED Blinking Example
12. bool gpio_value = false;
13.
14. static void IRAM_ATTR gpio_isr_handler(void* arg)
15. {
16. /*
17. TODO - Implement the debouncing algorithm
18. to eliminate the Spurious interrupt trigger.
19. */
20.
21. /* Disable the Interrupt */
22. gpio_intr_disable(BLINK_GPIO_2);
23. gpio_isr_handler_remove(BLINK_GPIO_2);
24.
25. /* Button is pressed. Toggle the LED */
26. gpio_set_level(BLINK_GPIO_1, gpio_value);
27. gpio_value = !gpio_value;
28.
29. /* Re-Enable the Interrupt */
30. gpio_isr_handler_add(BLINK_GPIO_2, gpio_isr_handler, NULL);
31. gpio_intr_enable(BLINK_GPIO_2);
32. }
33.
34. void app_main(void)
35. {
36. /* Reset the pin */
37. gpio_reset_pin(BLINK_GPIO_1);
38. gpio_reset_pin(BLINK_GPIO_2);
→ 39.
Table of Contents
→
Table of Contents
Ad Ad
https://embetronicx.com/tutorials/wireless/esp32/idf/esp32-gpio-tutorial-using-esp-idf/ 17/28
31.10.2024 15:59 ESP32 GPIO Tutorial Using ESP-IDF | LED Blinking Example
→
STM32F103 Bootloader Tutorials RT-Thread RTOS Tutorials
Table of Contents
VHDL Tutorials
Admin
Embedded Software | Firmware | Linux Devic Deriver | RTOS
Hi, I am a tech blogger and an Embedded Engineer. I am always eager to learn and
explore tech-related concepts. And also, I wanted to share my knowledge with
everyone in a more straightforward way with easy practical examples. I strongly
believe that learning by doing is more powerful than just learning by reading. I love to do experiments. If
you want to help or support me on my journey, consider sharing my articles, or Buy me a Coffee! Thank
you for reading my blog! Happy learning!
https://embetronicx.com/tutorials/wireless/esp32/idf/esp32-gpio-tutorial-using-esp-idf/ 18/28
31.10.2024 15:59 ESP32 GPIO Tutorial Using ESP-IDF | LED Blinking Example
IDF
→
Leave a Comment
Table of Contents
Name *
Email *
Website
https://embetronicx.com/tutorials/wireless/esp32/idf/esp32-gpio-tutorial-using-esp-idf/ 19/28
31.10.2024 15:59 ESP32 GPIO Tutorial Using ESP-IDF | LED Blinking Example
Post Comment
This site uses Akismet to reduce spam. Learn how your comment data is processed.
→
Table of Contents
Browse by Tags
8051 (33) ADC (7) Automotive (9) Bootloader (10) Bottom Half (12) c (49)
Character Device Driver (50) Cortex-M0+ (16) Cortex-M3 (19) cortex-m4 (38) Cortex-M7 (32)
CubeIDE (7) DC Motor (8) Device Driver (53) Diagnostics (6) esp32 (12) FreeRTOS (15)
GPIO (26) GSM (7) HAL (7) I2C (14) Interrupts (18) keypad (6) LCD Interfacing (51)
LED Interfacing (29) Linux (53) LPC2148 (41) NuttX RTOS (7) PIC16F877A (26) Raspberry PI (15)
RT-Thread RTOS (9) RTOS (34) Sensor (24) SPI (9) ST Link (8) stm32 (38) sysfs (10)
Tasklet (6) Timer/Counter (8) Tutorials (262) UART (37) UDS Protocol (6) USB (6)
https://embetronicx.com/tutorials/wireless/esp32/idf/esp32-gpio-tutorial-using-esp-idf/ 20/28
31.10.2024 15:59 ESP32 GPIO Tutorial Using ESP-IDF | LED Blinking Example
Recent Posts
How to Select the Right Electrical Contactor for Your Needs
Email Address
Subscribe
Should Consider:
https://embetronicx.com/tutorials/wireless/esp32/idf/esp32-gpio-tutorial-using-esp-idf/ 21/28
31.10.2024 15:59 ESP32 GPIO Tutorial Using ESP-IDF | LED Blinking Example
Advertise Here
→
Table of Contents
https://embetronicx.com/tutorials/wireless/esp32/idf/esp32-gpio-tutorial-using-esp-idf/ 22/28
31.10.2024 15:59 ESP32 GPIO Tutorial Using ESP-IDF | LED Blinking Example
Advertise Here
→
Table of Contents
Diagnostics and Communication Management Function Group – UDS Protocol Tutorial Part 2
(242,316)
SPI Device Driver Tutorial – Linux Device Driver Tutorial Part 47 (145,697)
UDS Protocol Introduction (Unified Diagnostic Services) – UDS Protocol Tutorial Part 1
(140,917)
https://embetronicx.com/tutorials/wireless/esp32/idf/esp32-gpio-tutorial-using-esp-idf/ 23/28
31.10.2024 15:59 ESP32 GPIO Tutorial Using ESP-IDF | LED Blinking Example
First Linux Device Driver – Linux Device Driver Tutorial Part 2 (140,097)
FreeRTOS Tutorials
https://embetronicx.com/tutorials/wireless/esp32/idf/esp32-gpio-tutorial-using-esp-idf/ 24/28
31.10.2024 15:59 ESP32 GPIO Tutorial Using ESP-IDF | LED Blinking Example
ESP32 LED Blinky Example using NuttX (own app and driver)
STM32 Tutorials
https://embetronicx.com/tutorials/wireless/esp32/idf/esp32-gpio-tutorial-using-esp-idf/ 25/28
31.10.2024 15:59 ESP32 GPIO Tutorial Using ESP-IDF | LED Blinking Example
STM32 USB Device MSC using RAM – USB Device Tutorial Part 1
STM32 USB Device MSC using Internal Flash – USB Device Tutorial Part 2
→
Getting Started STM32 with RT-Thread Tutorial – Part 1
Table of Contents
Automotive Tutorials
UDS Protocol Introduction (Unified Diagnostic Services) – UDS Protocol Tutorial Part 1
https://embetronicx.com/tutorials/wireless/esp32/idf/esp32-gpio-tutorial-using-esp-idf/ 26/28
31.10.2024 15:59 ESP32 GPIO Tutorial Using ESP-IDF | LED Blinking Example
AUTOSAR Tutorials
Reviews
→
HT208D Inrush Clamp Meter (6000 Counts) – Review
Table of Contents
https://embetronicx.com/tutorials/wireless/esp32/idf/esp32-gpio-tutorial-using-esp-idf/ 27/28
31.10.2024 15:59 ESP32 GPIO Tutorial Using ESP-IDF | LED Blinking Example
→
Table of Contents
TESA 77774 –
00002 –
00 çıkarılabilir…
4.1 194
170 80 TL
Contact Us | Advertise with Us | Disclaimer | FAQ | Trademark | Privacy Policy | Guest Post
https://embetronicx.com/tutorials/wireless/esp32/idf/esp32-gpio-tutorial-using-esp-idf/ 28/28