ESP8266 - LED Strip

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

The LED strip can be switched by a relay or by a MOSFET module. With the relay, the strip is either fully lit or dark. With the MOSFET module, it can be fully lit, dark, or dimmed to any brightness by PWM, and it stays silent. We will build the relay version first, then the MOSFET version.

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
ESP8266 NodeMCU non-addressable led strip Pinout

Relay vs MOSFET Module for LED Strip

Before you wire anything, choose the part that switches the strip. If the MOSFET module is new to you, read the ESP8266 - MOSFET Module tutorial first.

Relay MOSFET Module
Brightness ON/OFF, no dimming ON/OFF plus dimming (PWM)
Switching sound audible click none
Switching time milliseconds (slow) very short
Lifetime contacts wear with use long, no moving parts
Supply type AC or DC DC only
Strip voltage as the relay allows 6V minimum (12V and 24V strips are fine)
Isolation yes (coil and contacts) yes (PC817 optocoupler)
Best use simple ON/OFF, AC lights dimming DC strips, fast or frequent switching

If you want to dim a 12V or 24V strip, take the MOSFET module. For an RGB strip, each color pin (R, G and B) needs its own module, so you need 3 MOSFET modules, in the same way as you need 3 relays.

We will learn how to control the both types by ESP8266 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 ESP8266 and 12V LED strip. ESP8266 can control the 12V LED strip via the relay. If you do not know about relay (pinout, how it works, how to program ...), learn about relay in the ESP8266 - Relay tutorial

Wiring Diagram.

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

The wiring diagram between ESP8266 NodeMCU and 12V LED strip

This image is created using Fritzing. Click to enlarge image

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

The wiring diagram between ESP8266 NodeMCU and 12V LED strip

This image is created using Fritzing. Click to enlarge image

ESP8266 Code

ESP8266 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 ESP8266 NodeMCU code was developed by newbiely.com * * This ESP8266 NodeMCU code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp8266/esp8266-led-strip */ #define LED_STRIP_PIN D5 // The ESP8266 pin controls to the LED strip via relay void setup() { Serial.begin(9600); // Configure ESP8266 pins as digital output pins pinMode(LED_STRIP_PIN, OUTPUT); } // The loop function repeats indefinitely 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); }

ESP8266 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 ESP8266 NodeMCU code was developed by newbiely.com * * This ESP8266 NodeMCU code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp8266/esp8266-led-strip */ #define BLUE_PIN D5 // The ESP8266 pin connects to the blue pin of LED strip via relay 1 #define RED_PIN D6 // The ESP8266 pin connects to the red pin of LED strip via relay 2 #define GREEN_PIN D7 // The ESP8266 pin connects to the green pin of LED strip via relay 3 void setup() { Serial.begin(9600); // Configure ESP8266 pins as digital output pins pinMode(BLUE_PIN, OUTPUT); pinMode(RED_PIN, OUTPUT); pinMode(GREEN_PIN, OUTPUT); } // The loop function repeats indefinitely 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); }

Detailed Instructions

To get started with ESP8266 on Arduino IDE, follow these steps:

  • Check out the how to setup environment for ESP8266 on Arduino IDE tutorial if this is your first time using ESP8266.
  • Wire the components as shown in the diagram.
  • Connect the ESP8266 board to your computer using a USB cable.
  • Open Arduino IDE on your computer.
  • Choose the correct ESP8266 board, such as (e.g. NodeMCU 1.0 (ESP-12E Module)), and its respective COM port.
  • Connect ESP8266 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 ESP8266
  • 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

How to Dim a 1-color LED Strip with MOSFET Module

A MOSFET module is another easy way to dim a 1-color strip. The relay is removed, and pin D5 of the NodeMCU now sends PWM to the module, which sets the strip brightness.

Wiring Diagram between ESP8266, MOSFET Module and 1-color LED Strip

The wiring diagram between ESP8266 NodeMCU and NodeMCU LED strip MOSFET module

This image is created using Fritzing. Click to enlarge image

See more in ESP8266's pinout and how to supply power to the ESP8266 and other components.

ESP8266 NodeMCU / power side MOSFET Module
D5 (GPIO14) PWM
G (GND) GND
12V adapter + +
LED strip 12V (+) pin + (shared with the adapter +)
LED strip GND (−) pin LOAD
12V adapter − −

An LED strip is not a coil, so you do not need a flyback diode here.

ESP8266 Code for Dimming the LED Strip

/* * This ESP8266 NodeMCU code was developed by newbiely.com * * This ESP8266 NodeMCU code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp8266/esp8266-led-strip */ const int MOSFET_PIN = D5; // ESP8266 pin D5 connected to the PWM pin of the MOSFET module void setup() { Serial.begin(9600); pinMode(MOSFET_PIN, OUTPUT); analogWrite(MOSFET_PIN, 0); // start with the load OFF } void setLoad(int percent) { int duty = map(percent, 0, 100, 0, 255); // convert 0-100% to 0-255 analogWrite(MOSFET_PIN, duty); Serial.print("Load power: "); Serial.print(percent); Serial.println("%"); } void loop() { // turn the load fully ON, then fully OFF Serial.println("Load: ON"); setLoad(100); delay(2000); Serial.println("Load: OFF"); setLoad(0); delay(2000); // increase the power step by step (PWM) for (int percent = 10; percent <= 100; percent += 10) { setLoad(percent); delay(500); } // decrease the power step by step (PWM) for (int percent = 90; percent >= 0; percent -= 10) { setLoad(percent); delay(500); } delay(1000); }

The strip shines at full brightness for 2 seconds and goes dark for 2 seconds. Then it gets brighter in 10% steps up to 100%, and dimmer in 10% steps down to 0%.

Detailed Instructions

  • Use the same Arduino IDE and board setup as in the Quick Steps above.
  • Wire the MOSFET module and the strip as in the diagram, but do not plug in the adapter yet.
  • Upload the code above to the ESP8266.
  • Plug in the adapter and watch the strip fade.
  • Open the Serial Monitor at 9600 baud:
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
Nodemcu 1.0 (ESP-12E Module)
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Nodemcu 1.0 (ESP-12E Module)' on 'COM15')
New Line
9600 baud
Load: ON Load power: 100% Load: OFF Load power: 0% Load power: 10% Load power: 20% Load power: 30% Load power: 40% Load power: 50% Load power: 60% Load power: 70% Load power: 80% Load power: 90% Load power: 100% Load power: 90% Load power: 80% Load power: 70% Load power: 60% Load power: 50% Load power: 40% Load power: 30% Load power: 20% Load power: 10% Load power: 0%
Ln 11, Col 1
Nodemcu 1.0 (ESP-12E Module) on COM15
2

Remember: the module is for DC strips only, with a supply of about 6V or more. A light on AC mains still needs a relay.

Video Tutorial

※ 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!