ESP32 - LED Strip

In this tutorial, we are going to learn how to program ESP32 to control a LED strip to emit the light.

Hardware Used In This Tutorial

1×ESP-WROOM-32 Dev Module
1×USB Cable Type-C
1×Relay
1×12V 1-color LED Strip
1×12V RGB LED Strip
1×12V Power Adapter
1×DC Power Jack
1×Jumper Wires
1×(Alternative) 24V 1-color LED Strip
1×(Alternative) 24V RGB LED Strip
1×(Alternative) 24V Power Adapter
1×(Optional) 5V Power Adapter for ESP32
1×(Recommended) Screw Terminal Expansion Board for ESP32

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 these links are affiliate links. We may earn a commission on your purchase at no extra cost to you. We appreciate it.

Introduction to 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
ESP32 non-addressable led strip Pinout

We will learn how to control the both types by ESP32 one-by-one.

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 ESP32 and 12V LED strip. ESP32 can control the 12V LED strip via the relay. Unfamiliar with relay, including their pinouts, functionality, and programming? learn about relay in the ESP32 - Relay tutorial

Wiring Diagram.

Wiring Diagram between ESP32 and Non-Addressable 1-color LED strip.

ESP32 12V LED strip Wiring Diagram

This image is created using Fritzing. Click to enlarge image

Wiring Diagram between ESP32 and Non-Addressable RGB LED strip.

ESP32 12V LED strip Wiring Diagram

This image is created using Fritzing. Click to enlarge image

If you're unfamiliar with how to supply power to the ESP32 and other components, you can find guidance in the following tutorial: How to Power ESP32.

ESP32 Code

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,

/* * This ESP32 code is created by esp32io.com * * This ESP32 code is released in the public domain * * For more detail (instruction and wiring diagram), visit https://esp32io.com/tutorials/esp32-led-strip */ #define LED_STRIP_PIN 16 // The ESP32 pin GPIO16 controls to the LED strip via relay void setup() { Serial.begin(9600); // initialize ESP32 pins as digital output pins pinMode(LED_STRIP_PIN, OUTPUT); } // the loop function runs over and over again forever void loop() { Serial.println("The LED strip is turned on"); digitalWrite(LED_STRIP_PIN, HIGH); delay(5000); Serial.println("The LED strip is turned off"); digitalWrite(LED_STRIP_PIN, LOW); delay(5000); }

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)

/* * This ESP32 code is created by esp32io.com * * This ESP32 code is released in the public domain * * For more detail (instruction and wiring diagram), visit https://esp32io.com/tutorials/esp32-led-strip */ #define BLUE_PIN 23 // The ESP32 pin GPIO23 connects to the blue pin of LED strip via relay 1 #define RED_PIN 22 // The ESP32 pin GPIO22 connects to the red pin of LED strip via relay 2 #define GREEN_PIN 21 // The ESP32 pin GPIO21 connects to the green pin of LED strip via relay 3 void setup() { Serial.begin(9600); // initialize ESP32 pins as digital output pins pinMode(BLUE_PIN, OUTPUT); pinMode(RED_PIN, OUTPUT); pinMode(GREEN_PIN, OUTPUT); } // the loop function runs over and over again forever void loop() { Serial.println("The LED strip is turned red"); digitalWrite(BLUE_PIN, LOW); digitalWrite(RED_PIN, HIGH); digitalWrite(GREEN_PIN, LOW); delay(2000); Serial.println("The LED strip is turned green"); digitalWrite(BLUE_PIN, LOW); digitalWrite(RED_PIN, LOW); digitalWrite(GREEN_PIN, HIGH); delay(2000); Serial.println("The LED strip is turned blue"); digitalWrite(BLUE_PIN, HIGH); digitalWrite(RED_PIN, LOW); digitalWrite(GREEN_PIN, LOW); delay(2000); Serial.println("The LED strip is turned yellow"); digitalWrite(BLUE_PIN, LOW); digitalWrite(RED_PIN, HIGH); digitalWrite(GREEN_PIN, HIGH); delay(2000); Serial.println("The LED strip is turned magenta"); digitalWrite(BLUE_PIN, HIGH); digitalWrite(RED_PIN, HIGH); digitalWrite(GREEN_PIN, LOW); delay(2000); Serial.println("The LED strip is turned cyan"); digitalWrite(BLUE_PIN, HIGH); digitalWrite(RED_PIN, LOW); digitalWrite(GREEN_PIN, HIGH); delay(2000); Serial.println("The LED strip is turned white"); digitalWrite(BLUE_PIN, HIGH); digitalWrite(RED_PIN, HIGH); digitalWrite(GREEN_PIN, HIGH); delay(2000); }

Quick Instructions

  • If this is the first time you use ESP32, see how to setup environment for ESP32 on Arduino IDE.
  • Do the wiring as above image.
  • Connect the ESP32 board to your PC via a micro USB cable
  • Open Arduino IDE on your PC.
  • Select the right ESP32 board (e.g. ESP32 Dev Module) and COM port.
  • Connect 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 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

Video Tutorial

Making video is a time-consuming work. If the video tutorial is necessary for your learning, please let us know by subscribing to our YouTube channel , If the demand for video is high, we will make the video tutorial.

※ OUR MESSAGES