How to Control WS2812 RGB LED (NeoPixel) W/ Arduino [Tutorial]

by Electropeak in Circuits > Arduino

6006 Views, 5 Favorites, 0 Comments

How to Control WS2812 RGB LED (NeoPixel) W/ Arduino [Tutorial]

rgb_gif_compressed.gif

Overview

NeoPixel LEDs are widely used these days in electronic projects, due to their attractive visual effects. These LEDs are available in various sizes and shapes and in the strip form. In this tutorial, you’ll learn about NeoPixel LEDs and how to control them with Arduino.

What You Will Learn

  • What NeoPixel is
  • Why NeoPixel
  • interfacing the NeoPixel with Arduino

Supplies

Hardware Components

WS2812 RGB LED Ring Module *1

Arduino Uno R3 *1

Software Apps

Arduino IDE

What Is NeoPixel?

rgb-colors.png
rgb-inside.jpg

After 1962, when the first LED was made and tested, this valuable piece became an inseparable part of our lives. In most electronic projects, you can find at least one single LED. Using the LEDs in different colors was so attractive that it caused the production of multi–color LEDs or RGB LEDs.

The RGB LED creates all the colors based on the three colors of red, green, and blue. for example, a red and blue combination produces a magenta color. In this model, each color has a value between 0 and 255 for each red, green and blue colors. For instance, these values are 255 0 255 (maximum red, maximum blue, and minimum green value) for Magenta. This numeric value is expressed by the Hex code (2550255 = # FF00FF). RGB LEDs make a variety of colors based on this model.

To control each RGB LED, you need three digital pins of a microcontroller (or the development boards like Arduino). For example, if you want to control an RGB LED string containing 60 LEDs, in order to control the color of each LED separately, you need 180 digital pins! So you have to forget controlling each LED individually or use LEDs that are addressable.
Addressable LEDs are a new generation of LEDs including a controller IC, in addition to RGB LEDs. This controller IC, usually WS2812, allows you to access multiple LEDs with a single digital pin by assigning an address to each LED and providing one wire communication. But unlike simple LEDs, these types of LEDs do not turn on only by applying voltage, they also require a microcontroller. NeoPixel is the Adafruit brand for addressable LEDs.

Why NeoPixel?

rgb-module.jpg

The ability to control each LED in an LED strip will create great visual effects in your projects. But it should be noted that in the very fast processes like POVs, the use of NeoPixels is not recommended. The other important advantage of the NeoPixels is their lower price compared to other addressable LEDs. NeoPixels are also available in the ring, strip, square and circular models and you can select the suitable model according to your project.

Neopoxes are also chainable, so you can control multiple NeoPixels with only one command line and one power line.

Note Increasing the number of NeoPixel LEDs will require more RAM, more power and more processing time, so choose the most optimal NeoPixel according to your microcontroller type.

Required Materials

rgb-required-material.jpg

NeoPixel and Arduino Interfacing

rgb-pinout.jpg
rgb-circuit-734x1000.jpg

Note The resistor must be used to prevent the NeoPixel from damage and to transmit data correctly.

Note If you use a strip LED that contains a high number of LEDs, we recommend you to put a large capacitor (eg 1000uf) in parallel with + and – of the supply voltage.

Note The best distance to connect the first NeoPixel module is about 1 to 2 meters from the controller board.

Ex.1: Setting Up the NeoPixel With Arduino

In this example, you are going to turn on the NeoPixels and control the color and intensity of each LED separately using the Arduino UNO. Use the NeoPixel Adafruit library to set up the NeoPixel with Arduino.

Code Explanation

Adafruit_NeoPixel pixels(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);

The above function determine the number of LEDs and Arduino pins.

pixels.begin();

This function does the initializations.

pixel.setBrightness(b);

The above function set the light intensity. (The minimum number is 1 and maximum number is 255.)

pixels.setPixelColor(Wich LED,Wich color(Red,Green,Blue));

Defines the LEDs color with the RGB system, after specifying the LED number (from 0 to NUMPIXELS-1).

pixels.show();

Displays the applied values.

Downloads

Ex.2: NeoPixel Blinking Mode With Arduino

In this example, we set up NeoPixels in blinking mode. To make the blinker, all the LEDs must be turned on and off at the same time, and they can have different colors when they are turned on.

Downloads

Ex.3: NeoPixel Fading Mode With Arduino

Fading is one of the fascinating effects of NeoPixels. The slower the fade, the better the effect will be.

Downloads

Ex.4: NeoPixel Random Mode With Arduino

In this example, we use the random(num1, num2) function to generate and display a random number between num1 and num2 to choose a color and an LED.

Downloads

Ex.4: NeoPixel Rainbow Mode With Arduino

rgb-gen-3.jpg
rgb-string.jpg
rgb-effect.jpg
rgb-rainbow.jpg
rgb-generate-code.jpg

One of the most interesting tools on the web, to create effects on NeoPixels is the NeoPixel Effects Generator, which lets you specify the number of LEDs and Arduino pins, and after creating the effects and required settings, you can click on the generate Arduino code and copy the generated code to IDE. To do this, do the following steps:

  1. Click on Add Led Strip after entering the website.
  2. Click on the added NeoPixel Strip and specify the number of LEDs and Arduino pins.
  3. Click on the Add Effect in the loop part and choose an effect.
  4. Apply your preferences in the Animation and Color sections.
  5. Click on generate Arduino code and copy the generated code to Arduino IDE.

What’s Next?

  • Test the other effects on your NeoPixel.
  • Try to control your NeoPixel wirelessly. (WiFi, Bluetooth, …)

Similar Tutorials

Play with Fire Over WIFI! ESP8266 & NeoPixels (Including Android App)