Independent control of two LEDs with Arduino Nano ESP32: a practical introduction


Let’s introduce a new board from Arduino, the Arduino Nano ESP32 , released a few months ago, with this article (which is practically a “getting started” dedicated to this board) with a simple project that makes two LEDs flash independently. In particular, one of the two will flash ON/OFF while the other will gradually turn on using the PWM technique. Learning to control LEDs is a fundamental step for anyone taking their first steps in the world of electronics and programming with Arduino. In this article, I will guide you step by step to discover how to control two LEDs independently with the Arduino Nano ESP32, a powerful microcontroller development board that boasts integrated Wi-Fi and Bluetooth.

The article is aimed at a wide and diverse audience, from hobbyists and makers eager to learn the basics of LED control with Arduino to beginners who want to approach using the Arduino Nano ESP32 for the first time. Electronics and engineering students, as well as professionals looking for a compact and versatile solution for controlling LEDs, will find interesting and useful ideas in this article.

The goal is to provide readers with the knowledge and skills needed to independently build LED control projects with the Arduino Nano ESP32. To this end, the article uses a clear and concise structure, placing emphasis on practical learning.

As usual, for the development of the project, we will use the excellent IDE PlatformIO.

Arduino Nano ESP32: A Powerful and Versatile Microcontroller


The Arduino Nano ESP32 is a microcontroller development board based on the ESP32-S2FH4 chip. It offers a wide range of features in a compact and convenient form factor, making it ideal for a variety of IoT and embedded projects.

Main features:

  • Microcontroller ESP32-S2FH4: CPU dual-core Tensilica Xtensa LX106 a 240 MHz, 4 MB di Flash SPI, 200 KB di SRAM, 802.11 b/g/n Wi-Fi e Bluetooth 4.2 BLE.
  • Integrated connectivity: Wi-Fi and Bluetooth 4.2 BLE for wireless communication with other devices and the Internet.
  • Wide range of I/O: 14 GPIO pins, 1 12-bit ADC, 1 8-bit DAC, SPI, I2C, UART and JTAG.
  • Support for expansion boards: Compatible with Arduino Nano expansion boards.
  • Power supply: Power supply via USB or external 5V.
  • Compact size: 45 mm x 18 mm x 7 mm.


  • Powerful and versatile: The ESP32-S2FH4 offers high performance and a wide range of features.
  • Integrated connectivity: Wi-Fi and Bluetooth 4.2 BLE for wireless communication.
  • Wide range of I/O: Suitable for a variety of sensors, actuators and other devices.
  • Support for expansion boards: Expand functionality with Arduino Nano expansion boards.
  • Easy to use: Compatible with the Arduino IDE and the Arduino programming language.


  • IoT: Home automation, wearable tech, environmental and industrial monitoring projects.
  • Robotics: Control of robots and drones.
  • Wearable electronics: Smart watches, fitness trackers and other wearable devices.
  • Rapid prototyping: Quickly create prototypes of electronic devices.

The Arduino Nano ESP32 is a powerful and versatile microcontroller development board that offers a wide range of functionality in a compact and affordable form factor. It is an excellent choice for a variety of IoT and embedded projects.

The Arduino Nano ESP32: Reasons and Advantages

The Arduino Nano ESP32 was created to meet several emerging needs in the world of IoT and embedded electronics:

1. Wi-Fi and Bluetooth integration:

  • The previous generation of Nano cards did not include wireless connectivity. The ESP32 integrates Wi-Fi and Bluetooth 4.2 BLE, opening up new possibilities for wireless communication with other devices and the Internet.

2. More processing power:

  • The ESP32-S2FH4 offers superior dual-core CPU performance to AVR microcontrollers used in previous Nano boards. This allows you to manage more complex and demanding applications.

3. Size reduction:

  • The ESP32-S2FH4 has a smaller footprint than AVR chips, allowing for more compact and lightweight ESP32 Nano boards.

4. Compatibility with expansion boards:

  • The Arduino Nano ESP32 maintains compatibility with Arduino Nano expansion boards, allowing you to expand the board’s functionality with sensors, actuators and other devices.

5.Ease of use:

  • The Arduino Nano ESP32 is compatible with the Arduino IDE and the Arduino programming language, making it easy to use even for beginners.

In summary, the Arduino Nano ESP32 delivers:

  • Greater power and flexibility: It allows you to create more complex and high-performance IoT and embedded projects.
  • Integrated connectivity: Wi-Fi and Bluetooth 4.2 BLE for wireless communication.
  • Compact size:Ideal for wearable and miniaturized devices.
  • Ease of use: Accessible to hobbyists, makers and engineers of all experience levels.

Example of applications:

  • Home automation: Control lights, thermostats, smart locks and other home devices.
  • Wearable tech: Smart watches, fitness trackers and other wearable devices.
  • Environmental monitoring: Detection of temperature, humidity, air quality and other environmental parameters.
  • Robotics: Control of robots and drones.

The Arduino Nano ESP32 represents a significant advancement for the Arduino platform, offering a unique combination of power, flexibility, and ease of use that makes it ideal for a variety of IoT and embedded projects.

Pulse Width Modulation (PWM): A Technical View

Pulse Width Modulation (PWM) is a technique widely used in electronics for modulating the pulse width of a signal. This technique is based on varying the pulse duration of the signal, keeping the frequency constant. Here are some key points that illustrate the nature and usefulness of PWM:

  1. PWM Signal Generation:
    • PWM can be generated using timers and counters found in many microcontrollers and microprocessors.
    • The frequency of the PWM signal is determined by the frequency of the timer, while the ratio between the time the signal is “high” (high logic level) and the total period defines the duty cycle.
  2. Work Cycle and Duty Cycle:
    • The duty cycle represents the percentage of time the signal is high compared to the total period.
    • The duty cycle is the numerical value of the duty cycle and is expressed as a percentage. For example, a 50% duty cycle means the signal is high for half the period.
  3. PWM applications:
    • Motor Control: PWM is widely used in the control of electric motors to regulate the speed. By modulating the work cycle, it is possible to vary the power supplied to the engine.
    • Brightness Control: In LEDs and other light sources, PWM adjusts brightness by varying the time the light is on.
    • Audio: In the audio field, PWM can be used to generate simulated analog signals, contributing to sound synthesis.
  4. Advantages of PWM:
    • Energy Efficiency: Because PWM regulates the power supplied to a device, it contributes to energy efficiency by reducing heat dissipation.
    • Precise Control: PWM allows precise control of various devices, allowing for fine adjustments and quick response.
  5. Sound and Visual Effect:
    • In the audio domain, a PWM signal can create unique sound effects and modulations.
    • In lighting applications, PWM can create visual effects, such as gradually adjusting brightness.

What components do we need?

The list of components is not particularly long:

Project implementation

The electrical diagram

Before creating the actual circuit let’s take a look at the pinout of the board:

Pinout of the Arduino Nano ESP32 board
Pinout of the Arduino Nano ESP32 board

We will use GPIOs D2 and D5 to connect the LEDs.

At this point you can proceed with the creation of the circuit by following the connection diagram below.

The LEDs are connected to the Arduino Nano ESP32 via 100Ω resistors to limit the current that passes through them and avoid burning them (and burning the digital outputs to which they are connected).

The LED has two terminals (called anode and cathode) and, like all diodes, it is a component that has its own polarity: it passes the current when it is forward polarized (i.e. the voltage at the anode is greater than that at the cathode) and it blocks current when it is reverse polarized (i.e. the anode voltage is lower than the cathode voltage). The voltage between the anode and cathode, which we will indicate with Vd, varies according to the color of the light emitted. In particular we have that:

  • Vd = 1.8 V for red LEDs
  • Vd = 1.9 V for yellow LEDs
  • Vd = 2 V for green LEDs
  • Vd = 2 V for orange LEDs
  • Vd = 3 V for blue LEDs
  • Vd = 3 V for white LEDs

How do we identify the anode and cathode of the LED? We do this by looking at its terminals. The longest corresponds to the anode. Also, the LED body has a flattening at one point on the edge indicating that the nearby terminal is the cathode.

So if an LED doesn’t light up it’s possible that it’s wired upside down. In this case, to make it work, simply reverse the connections.

Below is the assembly diagram created with Fritzing:

Electrical diagram
Electrical diagram

How do you calculate the resistance to connect to the LED?

Please note: this paragraph deals with the calculation of the limiting resistance in a theoretical way and requires a minimum knowledge of the basics of Electrotechnics. Therefore it is not essential for understanding the rest of the project and can be skipped by the reader not interested in such theoretical aspects.

As we have already said, the resistor between the generic GPIO and the LED serves to limit the current flowing through the LED. But how can we calculate its resistance value? Ohm’s Law comes to our aid which says that the potential difference across a resistor (i.e. the voltage measured at the ends of the resistor) is proportional to the current I flowing through it and the constant of proportionality is precisely the resistance value of the resistor R:

V2 - V1 = RI

Please note: for the sake of precision it must be pointed out that while the resistor is the physical component (the actual object), the resistance is its value. So it is improper (even if it happens frequently) to call the resistor with the term resistance.

We can see Ohm’s Law on a simple circuit consisting of a voltage source (the circle on the left) and a resistor:

Representation of Ohm's Law
Representation of Ohm’s Law

The voltage (or potential difference) V2 – V1 impressed by the voltage source on the resistor is equal to the product of R by I.

Now let’s see a slightly more complex scheme where the usual voltage generator, the resistor and a red LED are present:

Circuit for calculating the current limiting resistor on the LED
Circuit for calculating the current limiting resistor on the LED

In our case the Vg represents the voltage present at the digital output of the Arduino Nano ESP32 when it is HIGH and is therefore equal to 3.3V.

Vd is the voltage across the diode (between anode and cathode) when it is forward biased (ie when it is carrying current). Having chosen a red LED, we know from the previous table that Vd = 1.8V.

We need to determine the R-value of the resistor. We still have one unknown: the value of the current I which must flow in the circuit when the pin is in the HIGH state.

Please note: when the digital pin is in the LOW state its voltage (ie Vg) is zero, it follows that also the current I in the circuit is zero.

LEDs generally cannot withstand currents greater than 20mA, so we impose a maximum current of 15mA to be on the safe side.

By Kirchhoff’s voltage law we have that:

Vg - Vr - Vd = 0

From which we derive that:

Vr = Vg - Vd 

Passing to the real values, we have that:

Vr = 3.3V - 1.8V

It follows that:

Vr = 1.5V

But, by Ohm’s Law, we have that:

Vr = RI

from which:

R = Vr / I

Substituting the real values:

R = 1.5V / 0.015A

The result is a value of R equal to 100Ω.

The sketch

Let’s create the PlatformIO project

We have already seen the procedure for creating a PlatformIO project in the article How to create a project for NodeMCU ESP8266 with PlatformIO. You can follow the guide but when choosing the platform you will have to choose Arduino Nano ESP32 in the Board field of the wizard, as in the image below:

The Project Wizard compiled for Arduino Nano ESP32
The Project Wizard compiled for Arduino Nano ESP32

Do not install the libraries indicated in the guide as we do not need them.

Now edit the platformio.ini file so it looks like this:

platform = espressif32
board = arduino_nano_esp32
framework = arduino
monitor_speed = 115200
upload_speed = 921600

Obviously you can download the project from the following link:

Replace the main.cpp file of the project you created with the one present in the zip file.

Now let’s see how the sketch works.

Initially the necessary libraries are included:

#include <Arduino.h>

Then the GPIOs to which we will connect the LED are defined:

const int led1Pin = D2;  // LED 1 connected to pin D2
const int led2Pin = D5;  // LED 2 connected to pin D5

The variables that deal with the timing of the LED switching on are then defined:

const unsigned long period1 = 1000;  // LED 1 flashing period (1 second)
const unsigned long period2 = 1000;  // LED 2 flashing period (1 second)

unsigned long previousMillis1 = 0;  // Previous time for LED 1
unsigned long previousMillis2 = 0;  // Previous time for LED 2

In the setup function the GPIOs are set as OUTPUT:

// Set LED pins as output
pinMode(led1Pin, OUTPUT);
pinMode(led2Pin, OUTPUT);

and the serial port initialized, followed by printing a welcome message on the Serial Monitor:

// Serial initialization
Serial.println("Hello world from Arduino Nano ESP32!");

In the loop function we encounter two blocks:

  unsigned long currentMillis = millis();

  // LED 1 control
  if (currentMillis - previousMillis1 >= period1) {
    digitalWrite(led1Pin, !digitalRead(led1Pin));
    previousMillis1 = currentMillis;

  // LED 2 control
  if (currentMillis - previousMillis2 >= period2) {
      for(int brightness = 0; brightness < 255; brightness++) {
        analogWrite(led2Pin, brightness);
    previousMillis2 = currentMillis;

The first activates every period1 ms and turns the first LED on and off. The second block is activated every period2 ms and starts a for that increases the value of the brightness variable from 0 to 254 which will regulate the PWM signal to gradually turn on the second LED, acting via the analogWrite(led2Pin, brightness) function;

As you can see for yourself, it is a very simple project, suitable for starting to become familiar with this new board recently created by Arduino.

Once you have compiled the project, transfer it to the board and, if everything went well, you will see the two LEDs light up.

IMPORTANT NOTICE: it can happen, at least as it happened to me for the first upload, to receive an error of this type:

dfu-util: Cannot open DFU device 2341:0364 found on devnum 10 (LIBUSB_ERROR_ACCESS) 

dfu-util: No DFU capable USB device available

I use a Linux distribution (specifically a Fedora) and therefore I had to look for a solution for my system.

If you are using Linux and the aforementioned error appears, you need to create a file called 99-platformio-udev.rules in the /etc/udev/rules.d/ folder by giving the command:

sudo touch /etc/udev/rules.d/99-platformio-udev.rules

and then, still in the terminal, give the command:

curl -fsSL | sudo tee /etc/udev/rules.d/99-platformio-udev.rules

Once this is done you need to restart udev (or restart your PC) with one of these two alternative methods:

sudo service udev restart


sudo udevadm control --reload-rules
sudo udevadm trigger

For further information I invite you to consult the page

If the problem occurs on Windows, you need to install the appropriate drivers as shown in this link,is%20not%20in%20bootloader%20mode. in the USB Drivers (Windows Only) paragraph.

Video of the operation

The following video shows how our first project with Arduino Nano ESP32 works:


If you want to be informed about the release of new articles, subscribe to the newsletter. Before subscribing to the newsletter read the page Privacy Policy (UE)

If you want to unsubscribe from the newsletter, click on the link that you will find in the newsletter email.

Enter your name
Enter your email
0 0 votes
Article Rating
Inline Feedbacks
View all comments
Would love your thoughts, please comment.x
Scroll to Top