Arduino

How to Program ESP32 Using Arduino IDE to Blink an LED

Like every beginner in the Arduino environment the first code we will run is to blink an external LED. Today we will not program LED to blink using Arduino board, we will take an IOT based microcontroller board named ESP32. Process of programming an ESP board is much like Arduino. Let’s see how to configure an LED using the ESP32 board through Arduino IDE.

How to Blink LED Using ESP32

Before we begin writing our first code. Connect ESP32 board to PC using a micro-USB cable. Once you connected ESP32 board setup it by following the below steps:

Step 1: First select the type of board you are using. Normally the ESP32 model is written on the back side of the board. Here we are using ESP32 DEVKIT V1.

Go to: Tools>Boards>esp32>Board Type:

Step 2: Last step before we code ESP32 is to select the COM port at which it is connected. You can check the com port by going to Device Manager under COM & LPT section.

Select COM port. Go to: Tools>Port>COMX:

Now we have selected the ESP32 board, it’s time to upload our first code.

Circuit

Draw the following circuit on a breadboard using a led and resistor. Connect positive end of led at digital pin 5 of ESP32 and connect negative end of led at GND of ESP32. In between ESP board and led connect a resistor to maintain safe value of current.

Schematics

Following diagram illustrates schematics of external Led with ESP32. LED is connected at digital pin 5 of ESP32:

Code

Write the following code in the Arduino programming environment. Upload code using the upload button at the top left corner of the screen.

In the mentioned code first, we initialized a LED_PIN variable and set it to pin 5. After that in the setup part, we declared pin 5 as an output for the ESP32 board.

In the loop section of code, using digitalWrite() function LED_PIN is set HIGH for 1 sec and set LOW for 1 sec alternatively. This loop section code will keep on running resulting in blinking LED.

const int LED_PIN = 5; //LED pin is defined
void setup() {
  pinMode (LED_PIN, OUTPUT);  // Pin 5 is set as OUTPUT
}
void loop() {
  digitalWrite (LED_PIN, HIGH); // LED on
  delay(1000)// delay of 1 sec
  digitalWrite (LED_PIN, LOW)// LED off
  delay(1000)// delay of 1 sec
}

Output

Below images shows LED circuit output where an external led will start blinking with a delay of 1 sec. Led will remain on for 1 sec then remain in LOW state for 1 sec. This pattern will repeat until a new code is uploaded to the ESP32 board.

Conclusion

ESP32 is a development board having Bluetooth and WiFi features pre-installed. As a beginner to get familiar with ESP32 programming and digitals pins, an example is run. Here we blink a led using ESP32 board at digital pin 5. This guide will help beginners to get basic ideas related to ESP32 and its working.

About the author

Kashif

I am an Electrical Engineer. I love to write about electronics. I am passionate about writing and sharing new ideas related to emerging technologies in the field of electronics.