Arduino Nano ESP32 - LED Strip
In this tutorial, we are going to learn how to program Arduino Nano ESP32 to control a LED strip to emit the light.
A relay or a MOSFET module can sit between the Nano ESP32 and the strip. With a relay, the strip can only be lit or dark. With a MOSFET module, the strip can be lit or dark too, and you can also dim it to any brightness with PWM, without any click. This tutorial covers both.
Hardware Preparation
Or you can buy the following kits:
| 1 | × | DIYables Sensor Kit (18 sensors/displays) |
Additionally, some of these links are for products from our own brand, DIYables .
Overview of LED Strip
A LED strip, also known as LED tape or LED ribbon, is a flexible circuit board with surface-mounted LEDs that emit light. These strips are versatile and commonly used for decorative lighting in various applications. LED strips come in a range of colors and are often used to provide ambient lighting, accent lighting, or decorative lighting effects.
LED strips come in two main types:
- Addressable LED Strips: In this type, the color and brightness of each individual LED on the strip can be independently controlled. This capability is due to the fact that each LED is assigned a specific address.
- Non-Addressable LED Strips: In contrast, non-addressable LED strips allow control over the color and brightness, but this control applies uniformly to all LEDs on the strip.
This tutorial will focus on the Non-Addressable LED Strips. For Addressable LED Strips, please refer to the following tutorials:
Non-Addressable LED Strip Pinout
Non-Addressable LED Strip has two main types:
- Non-Addressable 1-color LED strip: Only one color defined by manufacturer.
- Non-Addressable RGB LED strip: any colors
A Non-Addressable 1-color LED Strip usually has two pins:
- 12V/24V pin: needs to be connected to the positive pin of 12V or 24V DC power supply
- GND pin: needs to be connected to the negative pin of 12V or 24V DC power supply
A Non-Addressable RGB LED Strip usually has four pins:
- 12V/24V pin: needs to be connected to the positive pin of 12V or 24V DC power supply
- R pin: This pin is used to control the red color. Connecting this pin to the negative pin of the power supply enables the red color
- G pin: This pin is used to control the green color. Connecting this pin to the negative pin of the power supply enables the green color
- B pin: This pin is used to control the blue color. Connecting this pin to the negative pin of the power supply enables the blue color

We will learn how to control the both types by Arduino Nano ESP32 one-by-one.
LED Strip with Relay or MOSFET Module
Besides the relay, a MOSFET module can switch a non-addressable strip. To learn this module (pins, how it works, code), see the Arduino Nano ESP32 - MOSFET Module tutorial.
| Relay | MOSFET Module | |
|---|---|---|
| Light control | lit or dark | lit, dark, and dimming to any brightness (PWM) |
| Noise when switching | click | silent |
| Switching speed | slow, milliseconds | very fast, fast enough for PWM |
| Lifetime | contacts get worn | long, no moving parts |
| Load | AC or DC | DC only |
| Strip supply | any, within the relay rating | at least about 6V (12V and 24V strips are fine) |
| Board isolation | yes (coil vs contacts) | yes (PC817 optocoupler) |
| Pick it for | simple ON/OFF, AC lights | dimming, effects, fast and frequent switching |
For a 12V or 24V strip, the MOSFET module is the nicer option, because you can fade the light in and out. An RGB strip has three color channels (R, G, B), so it needs three MOSFET modules, one per channel, in the same way as it needs three relays.
How to Control a Non-Addressable 1-color LED strip.
If 12V LED strip is powered by 12V power supply, it emits light. To control a 12V LED strip, we need to use a relay in between Arduino Nano ESP32 and 12V LED strip. Arduino Nano ESP32 can control the 12V LED strip via the relay. Unfamiliar with relay, including their pinouts, functionality, and programming? learn about relay in the Arduino Nano ESP32 - Relay tutorial
Wiring Diagram.
Wiring Diagram between Arduino Nano ESP32 and Non-Addressable 1-color LED strip.

This image is created using Fritzing. Click to enlarge image
Wiring Diagram between Arduino Nano ESP32 and Non-Addressable RGB LED strip.

This image is created using Fritzing. Click to enlarge image
Arduino Nano ESP32 Code
Arduino Nano ESP32 Code for controlling Non-Addressable 1-color LED strip.
The below code repeatedly turns the LED strip ON in 5 seconds and OFF in 5 seconds,
Arduino Nano ESP32 Code for controlling Non-Addressable RGB LED strip.
The below code repeatedly control the color of the RGB LED strip (red, green, blue, yellow, magenta, cyan and white)
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.
- Connect Arduino Nano ESP32 to PC via USB cable
- Open Arduino IDE, select the right board and port
- Copy the above code and open with Arduino IDE
- Click Upload button on Arduino IDE to upload code to Arduino Nano ESP32
- Check out the LED strip's state
Code Explanation
Read the line-by-line explanation in comment lines of code!
Please note that, to control the brightness an other colors of non-addressable LED strip, we need to use the L298N driver instead of relay
Arduino Nano ESP32 Code - Dim LED Strip with MOSFET Module
This part uses a MOSFET module in place of the relay to change the brightness of a 1-color LED strip.
Wiring Diagram between Arduino Nano ESP32, MOSFET Module and 1-color LED strip

This image is created using Fritzing. Click to enlarge image
- Nano ESP32 D9 to the PWM pin of the module
- Nano ESP32 GND to the GND pin of the module
- 12V adapter + to the + terminal
- LED strip 12V pin to the + terminal as well
- LED strip GND pin to the LOAD terminal
- 12V adapter − to the − terminal
An LED strip has no coil, so you do not need a flyback diode. For a 24V strip, just use a 24V adapter. All three modules can take 24V.
Code
It is the same code as in the MOSFET module tutorial. The strip shines at full brightness for 2 seconds, goes dark for 2 seconds, then gets brighter from 10% to 100% and dimmer back to 0%.
Detailed Instructions
- Do the IDE setup from the relay Quick Steps above, if you have not done it yet.
- Make the wiring shown in the diagram, but leave the 12V adapter unplugged.
- Upload the code to the Arduino Nano ESP32.
- Plug in the 12V adapter.
- Open the Serial Monitor, with the baud rate set to 9600.
- See the strip fade in and fade out.
Tip: the MOSFET module handles DC only, and the strip supply should be at least about 6V. For AC lamps, use the relay instead.