LED with ESP32 | ESP32 Hello World

LED with ESP32 | ESP32 Hello World
LED with ESP32 | ESP32 Hello World

This tutorial will help you interface LED with ESP32 and this tutorial can be considered as the ESP32 Hello World project because this is one of the project for beginners to get started with ESP32. ESP32 is capable of all kinds of IoT projects and IoT being the trending field makes ESP32 a perfect development board to learn. In this tutorial, we will learn how to interface LED with ESP32 using Arduino IDE and Micropython as programming languages and then we will learn how to blink an LED with ESP32 using the same Arduino IDE and Micropython.

LED with ESP32 Circuit

Components required

ComponentAmazon (US)Aliexpress
ESP32LED with ESP32 | ESP32 Hello WorldLED with ESP32 | ESP32 Hello World
LEDLED with ESP32 | ESP32 Hello WorldLED with ESP32 | ESP32 Hello World
ResistorLED with ESP32 | ESP32 Hello WorldLED with ESP32 | ESP32 Hello World
BreadboardLED with ESP32 | ESP32 Hello WorldLED with ESP32 | ESP32 Hello World
Jumper wiresLED with ESP32 | ESP32 Hello WorldLED with ESP32 | ESP32 Hello World
Micro USB cableLED with ESP32 | ESP32 Hello WorldLED with ESP32 | ESP32 Hello World
LED with ESP32 Components required

Disclosure: Some of these links are affiliate links. We mayy earn commission on you purchase at no extra cost to you. Support us by buying from here .

LED Pinout and working

Before connecting the LED with ESP32, first lets look at how the pinout of LED.

LED Pinout
LED Pinout
Credits: tweaking4all

LED is a Light emitting diode therefore, the first symbol of LED looks very similar to diode. We can see in the above figure, that LED has two pins Anode ( + ) and Cathode ( – ).

Cathode (-) will be connected to Ground (0V).

Anode (+) will be connected to any digital pin of ESP32 and it will be used to control the state of LED. If digital pin will be HIGH, LED will be on. Otherwise if the digital pin is LOW (0V), LED will stay off.

Wiring Diagram

LED with ESP32 Circuit diagram
LED with ESP32 Circuit diagram

From the circuit digram, you can see that we have connected the Anode of LED with a resistor and resistor is connected to D25 of ESP32. The cathode of the LED is connected to the ground.

Resistor with LED

Resistor help us to protect the LED from burning. The value of resistor depends on the specification of LED and you can connect resistor on any pin of the LED, either with cathode (-) or Anode (+). If your LED comes with builtin resistor for protection, you could skip the resistor.

LED with ESP32 Code

We can program ESP32 in different programming languages and in different platforms such as Arduino IDE, Micropython, circuit python, ESP-IDF, Rust and scratch. For this tutorial, we are going to program LED with ESP32 in Arduino IDE, and LED with ESP32 in Micropython.

LED with ESP32 Arduino IDE

To program ESP32 with Arduino IDE, make sure your Ardunio IDE is setup. All you need to do is install the board support for ESP32 in Arduino ide.

After you have setup the Arduino IDE to program ESP32, we can write code. First, we will define the Pin number of LED.

#define LED_PIN 25

As we know that we have connected our LED with digital pin 25 of ESP32 and mostly it is marked as D25 in ESP32, Or if you want to use the builtin LED of ESP32, the above line can be written as,

#define LED_PIN LED_BUILTIN

After defining the pin, we will write a setup function, which will be executed once and is basically for defining and setting up the pins or other parameters.

void setup() {
  // put your setup code here, to run once:
  pinMode(LED_PIN, OUTPUT);
}

The above snippet of code defines the our LED which is connected on pin D25 is output and we can control the state of LED using the following function.

digitalWrite(LED_PIN,HIGH);

The digitalWrite(pinNum, state) function is used to control all your digital outputs, In the pin number we have to give the pin number and in the state we can say “High” for 3.3V and “LOW” for 0V. Also we need the the delay function which will help us to make LED ON for sometime and OFF for sometime , so we can see LED blinking. The delay function can be used as following,

delay(500);

We pass the time in milliseconds in the delay function. The value 500 in the function means to wait for 0.5 seconds (1 second = 1000 milliseconds).

LED blink with ESP32 in Arduino

#define LED_PIN 25

void setup() {
  pinMode(LED_PIN, OUTPUT);
}

void loop() {
  digitalWrite(LED_PIN,HIGH);
  delay(500);
  digitalWrite(LED_PIN,LOW);
  delay(500);
}

The above code will help us to blink the LED and the frequncy of blink is 0.5 second. You can play with the values of delay to change the frequency and you can change the pin number to any digital pin or use builtin LED with ESP32. Congratualtions you have written your first ESP32 program and has said esp32 hello world.

LED with ESP32 Micropython

First thing you need to setup Micropython support in ESP32.

After setting up the support, the first thing we do is import two libraries to access the delay function (time library) and GPIO pins (machine library).

import time
from machine import Pin

In the above snippet, we are importing all the functions of time library and from the machine library we are just importing the pin function.

Then, we define the LED pin and define it as output.

LED_PIN = Pin(25,Pin.OUT)

The pin function take two arguments, first one is the pin number and the second one, we define it as output.

Then we will create a forever loop using the following snippet,

while(True):

and we can set the state of LED to HIGH and LOW using the following snippet. 1 defines HIGH, and 0 defines LOW.

LED_PIN.value(1)

and for the delay and to make LED stay ON or OFF for a certain period, we will use the following function that we imported from the time library.

time.sleep(0.5)

and here we define time in seconds in the sleep function unlike the delay function in Arduino, where we were providing time in milliseconds.

LED blink with ESP32 in Micropython

import time
from machine import Pin
LED_PIN = Pin(25,Pin.OUT)
 
while True:
    LED_PIN.value(1)
    time.sleep(0.5)
    LED_PIN.value(0)
    time.sleep(0.5)

The above code will help you blink LED with ESP32 in micropython, The frequency of delay is same which is 0.5 seconds and you can play with the sleep function to increase or decrease the frequency.

Identation in Python

Python’s idea of indentation is crucial since improper indentation will result in an IndentationError and prevent the code from compiling. The identation you see under the while loop is necessary.

FAQ

Leave a Comment

Your email address will not be published. Required fields are marked *