ESP32 - LED - Blink

Hardware Required

1×ESP-WROOM-32 Dev Module
1×Micro USB Cable
1×LED
1×220 ohm resistor
1×Breadboard
2×Jumper Wires
Please note: These are affiliate links. If you buy the components through these links, We may get a commission at no extra cost to you. We appreciate it.

About LED

Pinout

LED includes two pins:

  • Cathode(-) pin needs to be connected to GND (0V)
  • Anode(+) pin is used to control LED's state
  • LED Pinout

How It Works

After connecting the cathode(-) to GND:

  • If connecting GND to the anode(+), LED is OFF.
  • If connecting VCC to the anode(+), LED is ON.
  • How LED Works

Besides, if generating a PWM signal to the anode(+), the brightness of LED is changed according to PWM value ( described in detail in this tutorial)

※ NOTE THAT:

  • For most of LED, we need to use a resistor. The resistor can be placed between the anode(+) and VCC or between the cathode(-) and GND. The value of the resistor depends on the specification of the LED.
  • Some kinds of LEDs have a built-in resistor. We may not need to use a resistor for those kinds of LEDs.

ESP32 - LED

When an ESP32's pin is configured as a digital output, the pin's voltage can be programmatically set to GND or VCC value.

By connecting the ESP32's pin to LED's anode(+) pin (via a resistor), we can programmatically control LED's state.

Wiring Diagram

ESP32 LED Wiring Diagram

Image is developed using Fritzing. Click to enlarge image

How To Program

  • Configure an ESP32's pin to the digital output mode by using pinMode() function. For example, pin GIOP18:
pinMode(18, OUTPUT);
  • Program the pin to GND to turn OFF led by using digitalWrite() function:
digitalWrite(18, LOW);
  • Program the pin to VCC to turn ON led by using digitalWrite() function:
digitalWrite(18, HIGH);

ESP32 Code

Quick Steps

  • If this is the first time you use ESP32, see how to setup environment for ESP32 on Arduino IDE
  • Connect the ESP32 board to your PC via a micro USB cable
  • Open Arduino IDE, select the right ESP32 board and COM port
  • Copy the below code and open with Arduino IDE
/* * Created by esp32io.com * * This example code is in the public domain * * Tutorial page: https://esp32io.com/tutorials/esp32-led-blink */ // the setup function runs once when you press reset or power the board void setup() { // initialize digital pin GIOP18 as an output. pinMode(18, OUTPUT); } // the loop function runs over and over again forever void loop() { digitalWrite(18, HIGH); // turn the LED on (HIGH is the voltage level) delay(1000); // wait for 1000 milliseconds digitalWrite(18, LOW); // turn the LED off by making the voltage LOW delay(1000); // wait for 1000 milliseconds }
  • Click Upload button on Arduino IDE to compile and upload code to ESP32 board
  • How to upload ESP32 code on Arduino IDE
  • See the result: The LED toggles between ON/OFF periodically every second.

Code Explanation

Read the line-by-line explanation in comment lines of code!

※ NOTE THAT:

The above code uses the delay(). This function blocks ESP32 from doing other tasks during the delay time. If your project requires to do some tasks, avoid blocking ESP32 by using the non-blocking method for ESP32.

Video Tutorial

We are considering to make the video tutorials. If you think the video tutorials are essential, please subscribe to our YouTube channel to give us motivation for making the videos.

Challenge Yourself

  • Connect LED to another pin of ESP32 and change the blink time.
  • Turn on/off LED by button. Hint: Refer to ESP32 - Button.

Additional Knowledge

Which pins on ESP32 UNO can be used as an output pin to control LED?
  • Pin 0 to pin 13
  • Pin A0 to pin A5

※ NOTE THAT:

At a time, one pin can take only one task. If you already used a pin for another task (e.g, digital input, analog input, PWM, UART...), you should NOT use it as digital output to control LED. For example, if we use Serial.println() function, we should NOT use pin 0 and 1 for any other purpose because these pins are used for Serial.

Extendability

This tutorial shows how to use the output pin of ESP32 to control an LED. We can apply this code to control ON/OFF any devices, even big machines.

※ NOTE THAT:

for devices/machines that use a high power supply ( > 5v) and/or high-current consumption, we need to use a relay between output pin and devices/machines - see ESP32 - Relay.

LED on Commercial Products

Small LEDs usually are used to indicate the status of devices. For examples:

Big LEDs usually are used for lighting. They can be combined into groups. For examples:

Function References

※ NOTE THAT:

Note that this tutorial is incomplete. We will post on our Facebook Page when the tutorial is complete. Like it to get updated.

※ NOTICES

  • We are AVAILABLE for HIRE. See how to hire us to do your project
  • If this tutorial is useful for you, please support us to make more tutorials.
  • We spent a lot of time and effort to create the content for this tutorial, please respect our work! Please do not copy the content to share on other websites. Howerver, please feel free to share the link of this tutorial anywhere