ESP8266 - NeoPixel LED Strip

The NeoPixel RGB LED strip is composed of LEDs whose color and brightness can be adjusted individually. This tutorial instructs you how to use ESP8266 to control the NeoPixel RGB LED strip. In detail, we will learn:

We only need to use one ESP8266's digital pin to control all the LEDs on the strip.

Hardware Preparation

1×ESP8266 NodeMCU
1×Micro USB Cable
1×NeoPixel RGB LED Strip
1×1000uF Capacitor
1×470Ω resistor
1×5V Power Adapter
1×DC Power Jack
1×Jumper Wires
1×(Optional) 5V Power Adapter for ESP8266
1×(Optional) ESP8266 Screw Terminal Adapter

Or you can buy the following sensor kit:

1×DIYables Sensor Kit 30 types, 69 units
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. We appreciate your support.

Overview of NeoPixel RGB LED Strip

The NeoPixel LED Strip Pinout

The NeoPixel RGB LED Strip has three pins that require specific connections:

  • GND pin must be linked to GND (0V)
  • VCC pin should be connected to the 5V of an external power source
  • Din pin is the pin that receives the control signal and should be linked to an ESP8266 pin.
NeoPixel pinout

※ NOTE THAT:

The sequence of pins may differ depending on the manufacturer. It is IMPERATIVE to use the markings printed on the LED Strip.

Wiring Diagram

The wiring diagram between ESP8266 NodeMCU and NeoPixel RGB LED strip

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.

How To Program For NeoPixel RGB LED Strip

  • Create a NeoPixel object
#define PIN_NEO_PIXEL D1 // The ESP8266 pin that connects to NeoPixel #define NUM_PIXELS 30 // The number of LEDs (pixels) on NeoPixel Adafruit_NeoPixel NeoPixel(NUM_PIXELS, PIN_NEO_PIXEL, NEO_GRB + NEO_KHZ800);
  • Strip with 30 LEDs
  • Initiates the NeoPixel strip which contains 30 lights.
NeoPixel.begin(); // INITIALIZE NeoPixel strip object (REQUIRED)
  • Specify the colour of each LED (known as a pixel).
NeoPixel.setPixelColor(pixel, NeoPixel.Color(255, 0, 0));
  • Adjust the brightness of all the strips.
NeoPixel.setBrightness(100); // a value from 0 to 255

※ NOTE THAT:

  • NeoPixel.setBrightness() is used to adjust the brightness of all pixels on the LED strip. To set the brightness for each individual pixel, we can scale the color values (R,G, B) with the same ratio.
  • The values set by NeoPixel.setBrightness() and NeoPixel.setPixelColor() will only be applied when NeoPixel.show() is executed.

ESP8266 Code

The code below does the following sequences:

  • Turn pixels to green one-by-one with a delay between each pixel
  • Turns off all pixels for two seconds
  • Lights up all pixels red simultaneously for two seconds
  • Repeats this process endlessly
/* * 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-neopixel-led-strip */ #include <Adafruit_NeoPixel.h> #define PIN_NEO_PIXEL D1 // The ESP8266 pin that connects to NeoPixel #define NUM_PIXELS 30 // The number of LEDs (pixels) on NeoPixel Adafruit_NeoPixel NeoPixel(NUM_PIXELS, PIN_NEO_PIXEL, NEO_GRB + NEO_KHZ800); void setup() { NeoPixel.begin(); // INITIALIZE NeoPixel strip object (REQUIRED) } void loop() { NeoPixel.clear(); // set all pixel colors to 'off'. It only takes effect if pixels.show() is called // turn pixels to green one by one with delay between each pixel for (int pixel = 0; pixel < NUM_PIXELS; pixel++) { // for each pixel NeoPixel.setPixelColor(pixel, NeoPixel.Color(0, 255, 0)); // it only takes effect if pixels.show() is called NeoPixel.show(); // send the updated pixel colors to the NeoPixel hardware. delay(DELAY_INTERVAL); // pause between each pixel } // turn off all pixels for two seconds NeoPixel.clear(); NeoPixel.show(); // send the updated pixel colors to the NeoPixel hardware. delay(2000); // off time // turn on all pixels to red at the same time for two seconds for (int pixel = 0; pixel < NUM_PIXELS; pixel++) { // for each pixel NeoPixel.setPixelColor(pixel, NeoPixel.Color(255, 0, 0)); // it only takes effect if pixels.show() is called } NeoPixel.show(); // send the updated pixel colors to the NeoPixel hardware. delay(2000); // on time // turn off all pixels for one seconds NeoPixel.clear(); NeoPixel.show(); // send the updated pixel colors to the NeoPixel hardware. delay(2000); // off time }

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.
  • Click to the Libraries icon on the left bar of the Arduino IDE.
  • Search for “Adafruit NeoPixel” and locate the NeoPixel library from Adafruit.
  • Then, press the Install button to install the NeoPixel library.
ESP8266 NodeMCU NeoPixel library
  • Copy the code and open it in the Arduino IDE.
  • Click the Upload button to send the code to the ESP8266.
  • Check out the LED effect.

※ NOTE THAT:

For any intricate LED effect, we provide the paid programming service

Video Tutorial