Arduino Nano - NeoPixel LED Strip

The NeoPixel RGB LED strip is a line of LEDs where the color and brightness of each one can be adjusted independently. This tutorial instructs you how to use an Arduino Nano to control the NeoPixel RGB LED strip. In detail, we will learn:

All the LEDs on the strip can be controlled with only one digital pin of the Arduino Nano.

Hardware Preparation

1×Arduino Nano
1×USB A to Mini-B 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) 9V Power Adapter for Arduino Nano
1×(Recommended) Screw Terminal Adapter for Arduino Nano

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:

  • GND pin: must be connected to GND (0V)
  • VCC pin: must be connected to 5V of an external power source
  • Din pin: is the pin that receives the control signal and should be connected to an Arduino Nano's digital pin.
NeoPixel pinout

※ NOTE THAT:

The order of pins may differ between manufacturers. It is imperative to always use the labels printed on the LED Strip.

Wiring Diagram

The wiring diagram between Arduino Nano and NeoPixel RGB LED strip

This image is created using Fritzing. Click to enlarge image

How To Program For NeoPixel RGB LED Strip

  • Create a NeoPixel object.
#define PIN_NEO_PIXEL 2 // Arduino Nano 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);
  • Initializes NeoPixel.
NeoPixel.begin(); // INITIALIZE NeoPixel strip object (REQUIRED)
  • Specify the color of each individual LED (known as a pixel).
NeoPixel.setPixelColor(pixel, NeoPixel.Color(255, 0, 0));
  • Adjust the brightness of all the strip.
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 called.

Arduino Nano 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 in red for two seconds
  • Repeats this process endlessly
/* * This Arduino Nano code was developed by newbiely.com * * This Arduino Nano code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano/arduino-nano-neopixel-led-strip */ #include <Adafruit_NeoPixel.h> #define PIN_NEO_PIXEL 2 // The Arduino Nano 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

  • Click to the Libraries icon on the left bar of the Arduino IDE.
  • Search for “Adafruit NeoPixel” and locate the NeoPixel library by Adafruit.
  • Then, press the Install button to install the NeoPixel library.
Arduino Nano neopixel library
  • Copy the code and open it in the Arduino IDE.
  • Click the Upload button in the Arduino IDE to send the code to the Arduino Nano.
  • Check out the LED effect.

※ NOTE THAT:

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

Video Tutorial