GPIO pins of ESP32 – LED Blinking example

How to use GPIO pins of ESP32 devkit with LED blinking example. Whenever any beginner starts learning about any microcontroller based development board, Experts always recommends beginners to start with LED blinking example which is also known as a light emitting diode. LED blinking examples to use general purpose input output pins to turn on and turn off the LED. By learning how to control GPIO pins, you will be able to use GPIO pins of ESP32 board for other applications like LCD interfacing, keypad interfacing, and other embedded system projects. So let’s start with the basic introduction of general purpose input output pins of ESP32 Dev kit. You can use any ESP32 development board you want until it has ESP-WROOM-32 chip on it. Also, the concepts used in this article will remain applicable to other types of ESP32 boards.  For more features of this board, you can go through this tutorial:

Prerequisites

We will be using Arduino IDE to program ESP32. So if you don’t know how to install a library of this IOT board you can check this tutorial:

ESP32 Devkit supports thirty  GPIO pins and but not all the pins can be used as digital output pins.  From a total of 30 pins, only 24 pins can be used both as digital input and output. But six pins like GPIO34, GPIO35, GPIO36, GPIO37, GPIO38, GPIO39 can be used as digital input pins only. But We have enough 24 pins which can be used as digital output pins. So we can use any out of thirty pins to drive a LED, relay, seven segment displays, liquid crystal display and other actuators.  Do you want to learn the difference between sensor and actuator? Check here. For more information about the GPIO pins of the ESP32 development board, check this article:

Now that I believed you have already installed ESP32 in Arduino IDE and You have also gone through the article on ESP32 pinout. Now let’s see how to blink an LED using ESP32 and Arduino IDE.

LED Blinking Using ESP32

As I mentioned earlier in ESP32 , we have can use 30 pins as a digital output pin. Now lets select one pin and used to turn on and turn off LED with some delay. ESP32 pin mapping

As you can in the above picture we have a total of 36 GPIO pins, but we will be using GPIO23 as a digital output pin. On different board, these pins can be located at different locations. So you need to check its data sheet before using pin number in programming which I will explain in the programming part of this article. Now check the schematic and make it on your breadboard:LED blinking ESP32

Now make the circuit connection according to the above circuit diagram on the bread board.

  • Connect GPIO22 with the anode or positive pin of the LED. Positive pin is usually round edge.
  • Connected other end of the LED with ground through 330 ohm resistor. Resistor is used as a current limiting resistor.
  • Each pin can provide a maximum of 10mA current. So current limiting resistor is used to limit current not crossing this limit.
  • If you want to use GPIO pins to drive any other device which has a higher current requirement, you should connect a transistor in between or current driver integrated circuit like ULN2003 relay driver circuit IC.

Program for LED blinking using ESP32

To write code for LED blinking using ESP32, first, you need to understand three main functions used in Arduino IDE to control general purpose input output pins. If you have already used Arduino IDE for Arduino or esp8266 programming, you will be already familiar with these functions :

  • pinMode() : This function is used to select input output pin mode either as a input or output. First argument to this function is pin number to which you want to declare either as a input or output. Second argument to this function is in which direction you want to use a digital pin. For example, if we write pinMode(22, OUPUT) , it will declare pin number 22 as a digital output pin.
  • digitalWrite(pin_number, LOGIC_HIGH_LOW) : This function is used to control digital output pin. First argument is a pin number and second value to this function is logic state ‘HIGH’ or ‘LOW’. For example, if you write digitalWrite(22, HIGH), it will make pin number 22 logic HIGH or 3.3 volt and if you write digitalWrite(22, LOW), it will make digital output pin 22 LOW or you will get zero volt at the output pin.
  • delay(value) : This function is used to generate delay in mili seconds. If you want to add a delay of one second between turning on and turning off a LED, you will use this state delay(1000).
  • setup( ) : In Arduino IDE, setup function is used for declaration or initialization. A pinMode function is used for declaration of digital pins. So it will be defined inside setup function. It will become more clear when you see the example code at the end of this article.
  • Loop( ) : In Arduino IDE, loop function is like a main function in c programming.  In c programming whenever, you want to perform certain tasks again and again, we use those c statements inside the loop. Similarly, we use loop function in Arduino IDE to perform certain tasks again and again.

This code is used to blink LED connected with pin number 22 with a delay of one second.

void setup()

 {
pinMode(22, OUTPUT);
}

void loop() 

{
digitalWrite(22, HIGH); 
delay(1000); 
digitalWrite(22 LOW); 
delay(1000); 
}
  • Now to run this example of LED blinking using ESP32, simply copy this code to Arduino IDE and compile the code.
  • After compiling code, click on upload button to upload code to ESP32 devkit.
  • Before uploading code, make sure you have selected board and COM pin to which your ESP32 board is connected.installing ESP32 in Arduino IDE 10
  • If you do not see this board in toots>port option , you need to install the driver for this board, you can download and install drivers from this link.
  • Go to tools>Boards and select ESP32 installing ESP32 in Arduino IDE 09
  • Go to tools>Upload speed and choose 115200
  • Also, select Flash Frequency of 80Mhz from tools options.
  • Now click on the upload button.installing ESP32 in Arduino IDE 11
  • After that, you will see LED will be blinking with a delay of one second.

It is a getting started tutorial with ESP32. To explore more tutorials and projects follow these guides:

1 thought on “GPIO pins of ESP32 – LED Blinking example”

  1. your notes on the Arduino /esp32 functions are clear. Since you introducing coding how about including a paragraph on the use and positioning of curly brackets?

Leave a Comment

shares