WS2812 LED bar/How it works/06. Green, red, blue
How it works · 06 of 9

Green, red, blue

The LED takes its 24 bits as green first, then red, then blue, most significant bit first. A library has to be told that order once, in the line that creates the strip. Told RGB instead, it turns every red green and every green red, and blue, which is third either way, hides the mistake.

The first byte is green

The first byte is green
GRB: right
Ask for
Library told
Asked
255, 0, 0
Shown (R, G, B)
255, 0, 0
The library sends green, red, blue, the order this LED reads. You ask for red and get red.

Each LED keeps 24 bits and splits them into three bytes. The data sheet is plain about the order: green, red, blue, and within each byte the most significant bit first. A byte of 255 is that colour at full brightness, 0 is off.

People write colours red first, and so do most libraries' functions. The library turns red-green-blue into what the LED wants, and it has to be told the order to do that.

Tell the library once

In Adafruit NeoPixel it is the third argument when the strip is created:

Adafruit_NeoPixel bar(5, 4, NEO_GRB + NEO_KHZ800);

In FastLED it is the third template argument:

FastLED.addLeds<WS2812B, 4, GRB>(leds, 5);

What a wrong order looks like

Told NEO_RGB, the library sends red first. The LED reads that byte as green. So red shows green, green shows red, orange turns a yellow-green, and blue, third in both orders, looks right.

That last part is why the mistake survives a first test. Check the order by asking for pure red on the top LED, and nothing else.

When it does not work

Red shows green and green shows red.

The strip was created with the wrong colour order. In Adafruit NeoPixel change NEO_RGB to NEO_GRB; in FastLED change RGB to GRB in addLeds. Do not swap the numbers in setPixelColor instead: every colour in the sketch would then have to be written backwards.

Blue and white look right, so is the order right?

Not necessarily. Blue is the third byte in both orders and white is all three at the same value, so neither can tell GRB from RGB. Ask for pure red on LED1: if it shows green, the order is wrong.

Can I send a colour as one number?

Yes. Adafruit NeoPixel's setPixelColor takes a 32-bit value written 0xRRGGBB, red first as people write it, and reorders the bytes for the LED from the constant given to the constructor. So 0xFF0000 is red on this bar whenever the strip was created with NEO_GRB.

Where this goes next

Twelve milliamps a colour, and what that means for a USB port.

What five LEDs draw

Edit this page — content/books/ws2812-led-bar/green-red-blue.mdx

Community

Questions about this product

See what other owners have asked, and read their solutions.

Ask a question ↗

WS2812 RGB LED Bar

Loading discussions…

Discuss this article

Ask about this page. The answer stays here, on the page it belongs to, for whoever hits the same wall next.

Browse Modules and blocks on the forum