Learn to code

ARDUINO & CODES

Previous Post
Control the sequence LED with a potentiometer and Arduino
Next Post
Introduction into Arduino DC Motor Control with Arduino

Controlling and powering NeoPixels with Arduino

in Arduino Visual Output

LED pixel strips are strips which have small LED modules mounted on it. One of these LED modules is the WS2812B which features a cascading port transmission mechanism which needs only a single line.

This means that multiple WS2812B can be cascaded and individually controlled by a single connection for data transmission. Therefore, cascading many WS2812B modules requires only wiring for VCC, GND, and the data transmission. As a result, the WS2812B modules are well suited for LED pixel strips. Today, WS2812B-based LED pixel strips are available in all kinds of variants. Typically, strips can be bought with 30 to 144 WS2812B modules per meter. When having 144 modules per meter, there is almost no space left between the modules. In this tutorial it is shown how to program an Arduino in order to control a WS2812B-based LED strip with the official Adafruit NeoPixel Library.

Important things about NeoPixels

Before you can start expirimenting with NeoPixels that are a couple of things that are worth mentioning.

  • Not all adressable LEDs are NeoPixels. NeoPixel is a brandname from Adafruit for individually adressable RGB Pixels and strips based on the WS2812, WS2811 and SK6812 LED / Drivers. If the RGB Pixel or strip that you are using does not contain this, please search for another tutorial.
  • NeoPixel don’t light up on their own. Connecting NeoPixels to a power source does not light up the Pixels. To light NeoPixels up you will need to have a microcontroller such as the Arduino Uno.
  • Be aware of the power supply. Supplyinh power to NeoPixels can be tricky. Therefore it is wise to know how much current your project will draw on forehand. Below are some examples how to calculate the ampere that the NeoPixel can draw.

Parts you will need

Arduino Uno Rev3 Arduino Uno Rev3 × 1
WS2812 NeoPixel strip WS2812 NeoPixel strip × 1
Dupont Wires Dupont Wires × 5
USB Solder Iron USB Solder Iron × 1
Solder Tin Solder Tin × 1

Arduinoplatform is a participant in several affiliate programs. This means that I will earn a commision if you buy a product from the affiliated websites through clicking on the links provided above.

Connecting the NeoPixel

So let’s get started connecting a NeoPixel strip to an Arduino Uno Board. Let us assume that the power comes from the USB port connected to your computer. The USB port receives 5V and 500 mAmps from your power source (your laptop, a charger or battery). That’s the maximum that your Arduino can handle and also the maximum that Arduino can provide to your leds. If you try to draw more than 500 mA from your laptop or battery, the laptop, battery and Arduino may get damaged or will shut themselves down.

The USB will provide power to the Arduino Board as well as the NeoPixels. As long as you connect 8 or less Neopixels you can power them straight from your Arduino board. Assuming you want to be able to burn them at full brightness (white light at full power). One color only uses about 20mA, so if you only show one color at a time you can connect more than 8 leds.

Wiring a basic setup as described above is quite easy. The NeoPixel strip has only three input pins which must be connected. GND, VDD 5v and a data connection. The strip that I am using has three wires: white (GND), green (data) and red (Vdd).  An Arduino can provide enough power to an LED strip with about 8 WS2812B modules. This is done by connecting the 5V output pin of the Arduino to the red wire and the GND pin to the GND wire while you connect the data wire (green) to any PWM pin on the Arduino Board. In this example, you are using pin 3.

For connecting a separate 5V DC power supply to the NeoPixel: connect the 5V input on the NeoPixel strip to the positive terminal on the power supply. The Data connection to pin 3, or any other PWM pin. And connect the GND to the NeoPixel and the Arduino Board. When using a separate power supply GND of the Arduino Board, NeoPixel and Power Supply must be connected.

Connecting more than 8 NeoPixels to Arduino

Each NeoPixel can draw up to 60 milliamps a maximum brightness white. In reality, you probaliy will not turn all NeoPixels white and since dirrent colors draw different milliamps it is difficult to estimate a single number that a NeoPixel will draw. You can use 20 milliamps per NeoPixel as a average, and 60 milliamps to be sure.

To estimate the power supply for the NeoPixels, multiply the number of pixels by 20 and devide by 1000. Or use 60 if you wan to be absolute safe.

20 NeoPixels × 20 mA ÷ 1,000 = 0.4 Amps minimum
30 NeoPixels × 60 mA ÷ 1,000 = 1.8 Amps minimum

Keep in mind, 60 mA is a worst case estimate and never power your NeoPixel with more than 5V. You can power them with less than 5V .However, this will cause the NeoPixels to be less brighter.

The Code

#include  <Adafruit_NeoPixel.h>
 
#define PIN 3
#define LEDS 8
 
// Parameter 1 = number of pixels in strip
// Parameter 2 = Arduino pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed
//   NEO_GRB     Pixels are wired for GRB bitstream (most NeoPixel products)
//   NEO_RGB     Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
//   NEO_RGBW    Pixels are wired for RGBW bitstream (NeoPixel RGBW products)
//   NEO_KHZ800  800 KHz bitstream (most NeoPixel products WS2812 LEDs)
//   NEO_KHZ400  400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
Adafruit_NeoPixel strip = Adafruit_NeoPixel(LEDS, PIN, NEO_GRB + NEO_KHZ800);
 
void setup() {
  strip.begin();
  strip.show();  // Initialize all pixels to 'off'
}
 
void loop() {
  strip.setPixelColor(0, 255, 0, 0); //RED
  strip.show(); //Show The Color
  delay(1000);
  strip.setPixelColor(0, 0, 255, 0); //Green
  strip.show(); //Show The Color
  delay(1000);
  strip.setPixelColor(0, 0, 0, 255); //Blue
  strip.show(); //Show The Color
  delay(1000);
}

Code Explanation

Installing the Library

Recent versions of the Arduino IDE (1.6.2 and later) make library installation super easy via the Library Manager interface. From the Sketch menu, > Include Library > Manage Libraries…  In the text input box type in “NeoPixel“. Look for “Adafruit NeoPixel by Adafruit” and select the latest version by clicking on the popup menu next to the Install button. Then click on the Install button. After it’s installed, you can click the “close” button.

or you can Manually install the library by going to GITHUB ADAFRUIT NEOPIXEL 

#include  <Adafruit_NeoPixel.h>
Define Some parameters

to be able to control the NeoPixel you need to define which pin you are using. In the Example you are using pin 3. You can chose any PWM pin that you like. Furthermore, we define the number of LEDs that are attached on the strip. Keep in mind to change this number if you have more LEDs.

#define PIN 3
#define LEDS 8
 
// Parameter 1 = number of pixels in strip
// Parameter 2 = Arduino pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed
//   NEO_GRB     Pixels are wired for GRB bitstream (most NeoPixel products)
//   NEO_RGB     Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
//   NEO_RGBW    Pixels are wired for RGBW bitstream (NeoPixel RGBW products)
//   NEO_KHZ800  800 KHz bitstream (most NeoPixel products WS2812 LEDs)
//   NEO_KHZ400  400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
Adafruit_NeoPixel strip = Adafruit_NeoPixel(LEDS, PIN, NEO_GRB + NEO_KHZ800);
 
void setup() {
  strip.begin();
  strip.show();  // Initialize all pixels to 'off'
}
Controlling the NeoPixels
To control the NeoPixel you can use the function setPixelColor. The first element consist of the Pixel that you want to light up and then the color.
 
strip.setPixelColor(n, red, green, blue);
 

The first argument — n in this example — is the pixel number along the strip, starting from 0 closest to the Arduino. If you have a strip of 8 pixels, they’re numbered 0 through 7. This is since Arduino starts counting from 0.

The next three arguments are the pixel color, expressed as red, green and blue brightness levels, where 0 is dimmest (off) and 255 is maximum brightness.

If you want to change the color from the example code. Search for RGB color picker on Google and you find it directly.
 
setPixelColor() does not have an immediate effect on the LEDs. To light the Pixels on you have to use the command:
 
strip.show();
 
    void loop() {
  strip.setPixelColor(0, 255, 0, 0); //RED
  strip.show(); //Show The Color
  delay(1000);
  strip.setPixelColor(0, 0, 255, 0); //Green
  strip.show(); //Show The Color
  delay(1000);
  strip.setPixelColor(0, 0, 0, 255); //Blue
  strip.show(); //Show The Color
  delay(1000);
}

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed

Menu