Lets blink a LED with ESP32

Udayanga Sampath
3 min readNov 24, 2021

Have you ever tried blink a LED using Arduino?

  • If so, good job.
  • If not, try now. You are never late.

If you have some experience with Arduino, you already know that blinking a LED is not a big deal. But do you remember the very first day you blinked the LED with the Arduino? Believe me, I know it was exhilarating. Even if you didn’t realise it, that simple step is the biggest step that you have taken in the microcontroller world. Well, this article is not about Arduino programming. In 2016, a big outbreak happened in the microcontroller world. I would say this is the biggest revelation to happen in microcontroller history ever. It is, in fact, releasing the ESP32.

Schematic diagram for Blink a LED

In the previous article, I discussed the features of ESP32, suitable programming languages, and IDEs that can help program ESP32. With ESP32, you have more opportunities to develop and execute advanced IoT programs. But, when you are new to this microcontroller board, you may end up with pretty exhausting errors and dead ends. My advice, do not give up. There are answers out there.

Sample Code

Here I have used the PlatformIO IDE with C++ to program the ESP32. But you can choose Arduino or Thonny instead of PlatformIO. Basically, Thonny IDE works with micro-python and Arduino and PlatformIO work with C++. You can start programming the ESP32 with your preferred language and IDE. Coding will differ with the preferred language.

If you are familiar with Arduino, you are already aware of how it works. In PlatformIO, we have a similar background and a similar coding system. Mainly, we have two major functions, Setup() and Loop(). Before you start working with these two functions, you need to define the pins that you are going to use in your project. Here I have used pin 2 as an output pin.

#define LED 2 // or int LED = 2;

After defining the pin, you need to setup the pin-mode inside the Setup() function as below.

pinMode(LED, OUTPUT);

As in Arduino, in the PlatforIO, the main piece of cord work is inside the Loop() function.

digitalWrite(LED, HIGH);

delay(500);

digitalWrite(LED, LOW);

delay(500);

By calling the digitalWrite function, you can pass the commands to blink the LED. To ensure visibility of LED blink, I have used the delay function for 500 milliseconds with each circle of blinking.

Below, I have listed a nice Arduino/ESP32 simulator link that you can try for free. Have some fun by blinking LEDs. In the meantime, I will meet you with a more advanced project soon.

Free Simulator: https://wokwi.com/arduino/new?template=esp32

--

--

Udayanga Sampath

As a researcher, I’m working on “The Detection & Tracking of Elephants Using low-powered Infrasonic Detectors” at the University of Colombo School of Computing.