Blink the LED with ESP32

zhil
5 min readFeb 2, 2022

In THIS ARTICLE, I will show you how I used the ESP32 microcontroller to blink both an internal and external LED. This little project is part of a course in embedded systems guided by the excellent Dr. Kusprasapta Mutijarsa, S.T, M.T. of the Bandung Institute of Technology (ITB).

Prerequisites

To complete this project, I used this set of hardware and software components. All of these can be acquired with relative ease either online or at the Jaya Plaza electronic market in Jl. Ahmad Yani, Bandung.

Hardware components of the project
  1. An ESP32 microcontroller whose vertebrae (“legs”) have been soldered.
  2. A Micro-USB cord
  3. A 3mm LED lamp
  4. 2 male-to-male jumper cable
  5. A 330Ω resistor
  6. A breadboard
  7. An Arduino IDE on your laptop or similar personal device. This article includes a step-by-step on how to install it.

Step 1: Installing the Arduino IDE for ESP32 use

In this step, I followed the excellent guide from RandomNerdTutorial’s “Getting Started with ESP32” ( https://randomnerdtutorials.com/installing-the-esp32-board-in-arduino-ide-windows-instructions/ ) as instructed by the course.

  1. Download and install the Arduino IDE — I used the Windows version as my OS is Windows 10. The installer is available in the official Arduino site. After the download is complete, I opened the program and follow the instructions.

2. Open the IDE. The result was this Window.

As you might suspect, this IDE is still set up for use of the Arduino microcontroller. I need to change it to ESP32.

3. Open File → Preferences, then add this link to the Additional Managers URLs: https://dl.espressif.com/dl/package_esp32_index.json

4. Install ESP32 in the board manager by opening Tools → Board → Boards Manager, then search for ESP32. The below screenshots is after I already installed ESP32; before that, the Board option should specify an Arduino microcontroller as a default.

5. Select the ESP32 board of use. I used the ESP32 Dev Module.

6. Select the port. I used COM3.

The ESP32 Board should now be installed in my IDE, and it’s time to test it.

Step 2: Set up the ESP32 and blink the internal LED

While most programs use the classic “print ‘Hello World’ to the console” as a sanity test, internal LEDs are often used instead in ESP32. All ESP32 are equipped with an internal LED.

  1. Place the ESP32 on the breadboard with the micro-USB port facing outside for ease of use. Press it in, gently and evenly, until the legs cannot be seen — be careful as it is quite sensitive.

2. Connect it to the device with a micro-USB cord. If successful, an internal LED should glow bright red. (This is not the LED we want).

3. Code the program to blink in the IDE. I used the template code accessible from File → Examples → 01.Basics → Blink.

However, this code is geared towards Arduino, not ESP32, which apparently does not have the constant LED_BUILTIN as a hard-coded pin. This resulted in a variable scope error when I tried to compile it. I must thus declare the value first — and for the internal LED, it is pin 2. The end code looks like this.

#define LED_BUILTIN 2// the setup function runs once when you press reset or power the board
void setup() {
// initialize digital pin LED_BUILTIN as an output.
pinMode(LED_BUILTIN, OUTPUT);
}
// the loop function runs over and over again forever
void loop() {
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}

Two actions happened in this code. First, Pin 2 is initialized as an output in the setup function. Then, its output is oscillated from high to low in the loop output, 2000ms per cycle.

4. Check against error by clicking the check button. If no problems were found, compile and upload the code into the microcontroller by pressing the arrow button.

The blue internal LED should blink every two seconds.

Step 3: Setting up and blink the external LED

In this last requirement, I used pin D23 as the output outlet and a red LED as the external LED. It is prudent to understand the structure of a breadboard before, so we can use it to construct our circuit:

  1. Place the LED on the breadboard.

2. Connect D23 of the ESP32 board with the positive leg of the LED. By convention, I used a red wire.

3. Connect the negative leg with a 330Ω resistor.

4. Connect the other end of the resistor to the GND (ground) of the ESP32 board. This will be the negative side; by convention, I used a black wire.

5. Code the blink program by changing the output pin of the previous code to D23. Connect the ESP32 board to the device using the micro-USB cord, then compile and export the code.

Through these steps, I successfully made the external LED blinked with an interval of 2 seconds per cycle.

That is all for my first ESP32 project. Stay tuned for the next one, bye-onara!

--

--