Arduino Tutorials

Connect & use the NeoPixel RGB LED Ring

Neopixel RGB LED Ring

In this tutorial you will learn how to connect and use the Adafruit NeoPixel RGB LED ring (or a compatible ring)*to your Arduino. The ring is available with different numbers of LEDs, we use a 12 LED ring here.

The special thing about the NeoPixel is that you can control each LED individually and also assign it an individual RGB color tone. So you can easily create animations with great color gradients.

Preparations & connection of the RGB LED ring

In most cases you will get the NeoPixel LED ring without pins. To use it, you have to solder pins to GND, 5V and IN (or DI) first. Be careful not to heat the connections on the ring for too long, otherwise they can become loose and the component becomes unusable.

When the connectors are in place, connect GND and 5V to the pins of the same name on your Arduino. The pin IN (or DI) goes to a digital pin of your choice – in our example sketch this is pin 2.

The right library

Like many components, the NeoPixel RGB LED Ring has a library to make your life easier. Open your Library Manager and search for Adafruit Neopixel. Then install the latest version:

neopixel led ring library

Controlling the RGB LED ring

Now you can start. Copy the following sketch and upload it to your Arduino. If everything fits, all the LEDs on your ring will light up red one after the other and turn off together.

//Library for the NeoPixel
#include <Adafruit_NeoPixel.h>

int leds = 12; //Number of LEDs
int ledPin = 2; //Pin the NeoPixel is connected

int red = 255; //Red
int green = 0; //Green
int blue = 0;  //Blue

//Initiate NeoPixel as "pixels"
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(leds, ledPin, NEO_GRB + NEO_KHZ800);

void setup() {
  pinMode (ledPin, OUTPUT);
  pixels.begin();
  pixels.setBrightness(100); //Brightness: 0 (off) - 255
}

void loop() {
  for (int i = 0; i < 12; i++) {
    pixels.setPixelColor(i, pixels.Color(red, green, blue));
    pixels.show();
    delay(500);
  }
  pixels.clear();
  delay(500);
}

Let’s go through the sketch step by step: First you include the library and define some variables for the number of LEDs, the connection of the ring and the color values.

#include <Adafruit_NeoPixel.h>

int leds = 12;
int ledPin = 2;

int red = 255;
int green = 0;
int blue = 0;

With one RGB LED, you can output more than 16 million different colors composed of the three single colors red, green and blue. For each of these colors you can set a value between 0 and 255 – it’s the mix that matters!

With this tool you can determine the appropriate single values for your desired color.

Next you create the instance pixels:

Adafruit_NeoPixel pixels = Adafruit_NeoPixel(leds, ledPin, NEO_GRB + NEO_KHZ800);

Here you define the number of LEDs on your ring using the variable leds and the pin ledPin to which your NeoPixel RGB LED ring is connected. The other parameters are not important at first.

The setup function

Here you define the ledPin as OUTPUT and activate the LED ring. Finally you define the brightness of the LEDs:

void setup() {
  pinMode (ledPin, OUTPUT);
  pixels.begin();
  pixels.setBrightness(100);
}

The Loop

Each LED has its own number from 0 to 11, with which you control it individually. Counting is normally done clockwise from the LED at the bottom of the connectors.

In this tutorial you switch on the LEDs one after the other with a short delay. For this you can use a for-loop:

void loop() {
  for (int i = 0; i < 12; i++) {
    pixels.setPixelColor(i, pixels.Color(red, green, blue));
    pixels.show();
    delay(500);
  }
  pixels.clear();
  delay(500);
}

Within the For-Loop you first define the color that the LED i should output. The function pixels.Color() contains the single values you defined at the beginning of the sketch:

pixels.setPixelColor(i, pixels.Color(red, green, blue));

Then you turn on the LED with pixels.show() and wait 500 milliseconds. Afterwards the For-Loop takes care of the next LED i.

When all 12 LEDs are lit, the sketch jumps out of the For-Loop and switches off all LEDs with pixels.clear(). After another 500 milliseconds it starts all over again.

What’s next?

Now you know the code for a simple animation in one color. But there are no limits to your imagination, how about a LEGO ISS that changes color for the duration of the flight over the real ISS?

You may also like

Comments are closed.