Arduino Nano ESP32 - LED - Blink

This tutorial provides instructions on how to use Arduino Nano ESP32 to blink an LED. This is one of the first tutorials that beginers learn.

Hardware Preparation

1×Arduino Nano ESP32
1×USB Cable Type-C
1×LED
1×220 ohm resistor
1×Breadboard
1×Jumper Wires
1×(Optional) DC Power Jack
1×(Recommended) Screw Terminal Expansion Board for Arduino Nano
1×(Recommended) Breakout Expansion Board for Arduino Nano

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
Additionally, some of these links are for products from our own brand, DIYables.

Overview of LED

LED Pinout

LED includes two pins:

  • Cathode(-) pin: connect this pin to GND (0V)
  • Anode(+) pin: is used to control LED's state
LED Pinout

How LED Works

After connecting the cathode(-) to GND:

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

In addition, if a PWM signal is generated to the anode(+), the LED's brightness is changed in proportion to the PWM duty cycle. See more detailed in Arduino Nano ESP32 fade LED tutorial.

※ NOTE THAT:

  • Usually, A resistor is required to protect LED from burning. The resistor can be placed between the anode(+) and VCC or between the cathode(-) and GND. The resistance value depends on the LED's specification.
  • Some LEDs have a built-in resistor, so there is no need to use a resistor for them.

Arduino Nano ESP32 - LED

The ESP32's digital output pin's voltage can be programmed to VCC or GND. By connecting the digital output pin to LED, we can programmatically control the LED's state.

Wiring Diagram between LED and Arduino Nano ESP32

The wiring diagram between Arduino Nano ESP32 and LED

This image is created using Fritzing. Click to enlarge image

How To Program

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

Arduino Nano ESP32 Code

Detailed Instructions

To get started with Arduino Nano ESP32, follow these steps:

  • If you are new to Arduino Nano ESP32, refer to the tutorial on how to set up the environment for Arduino Nano ESP32 in the Arduino IDE.
  • Wire the components according to the provided diagram.
  • Connect the Arduino Nano ESP32 board to your computer using a USB cable.
  • Launch the Arduino IDE on your computer.
  • Select the Arduino Nano ESP32 board and its corresponding COM port.* Copy the below code and paste it to Arduino IDE.
/* * This Arduino Nano ESP32 code was developed by newbiely.com * * This Arduino Nano ESP32 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano-esp32/arduino-nano-esp32-led-blink */ // the setup function runs once when you press reset or power the board void setup() { // initialize digital pin D5 as an output. pinMode(D5, OUTPUT); } // the loop function runs over and over again forever void loop() { digitalWrite(D5, HIGH); // turn the LED on delay(500); // wait for 500 milliseconds digitalWrite(D5, LOW); // turn the LED off delay(500); // wait for 500 milliseconds }
  • Compile and upload code to Arduino Nano ESP32 board by clicking Upload button on Arduino IDE
How to upload Arduino Nano ESP32 code on Arduino IDE
  • See the result: The LED blinks one time per second.

Line-by-line Code Explanation

The above Arduino Nano ESP32 code contains line-by-line explanation. Please read the comments in the code!

※ NOTE THAT:

The above code uses the delay(). This function blocks Arduino Nano ESP32 from doing other tasks . To avoid blocking ESP32, see Arduino Nano ESP32 blink without delay

Video Tutorial

Language References

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!