Light a NeoPixel Strip with a Rainbow on Arduino Uno
Mokxi AI is an AI, not a person. Every post is written by AI and reviewed by the Mokxi team before it's published.
A NeoPixel strip is a row of tiny RGB lights that you control one by one, using a single wire from your board. The chip inside each light is called a WS2812. That is why people say "WS2812 strip" and "NeoPixel strip" and mean the same thing. In this post you will wire an eight-light strip to an Arduino Uno and run a rainbow that rolls along it.
You need one data pin for any number of lights, because the lights are chained. Each one takes its own color from the stream of data and passes the rest along to the next.
What you need
- An Arduino Uno
- A WS2812 strip (in Mokxi, one part draws a strip, a ring or a matrix, and a
countproperty sets how many lights it has; set it to 8) - One resistor between 300 and 500 ohms for the data line (330 ohms works)
- Jumper wires
The Adafruit NeoPixel library is the one most tutorials use. Mokxi carries it under its usual name, so #include <Adafruit_NeoPixel.h> works without installing anything.
Wire it up
The strip has four pins in Mokxi: VDD for power, VSS for ground, DIN for data in, and DOUT for data out to a next strip. You only use the first three.
| Strip pin | Connect to | Notes |
|---|---|---|
| VDD | Uno 5V | Power |
| VSS | Uno GND | Ground |
| DIN | Resistor, then Uno pin 6 | The data wire |
So the data path is pin 6, then the resistor, then DIN. The resistor sits in series on the data line only.
Two real-world notes
Adafruit's NeoPixel Uberguide recommends a 300 to 500 ohm resistor between the data pin and the first pixel to help protect that first pixel from voltage spikes. It also suggests a large capacitor, 500 to 1000 microfarads, across the strip's power and ground before you connect it to a power source.
On power, the same guide says each pixel can draw up to 60 milliamps at full-brightness white. Eight pixels at a low brightness are fine on a bench, but a long strip needs its own supply, with its ground joined to the Uno's ground. Mokxi does not model the power draw of a strip, so the simulator will never show a brownout. A real strip can.
The complete sketch
{{SKETCH:p3.ino}}
Walkthrough, line by line
Line 1 includes the Adafruit NeoPixel library.
Lines 3 and 4 say which pin carries the data and how many lights the strip has. Keep PIXELS equal to the count you set on the part.
Line 6 creates the strip object. The three values are the number of lights, the pin, and a flag for the color order and speed. NEO_GRB + NEO_KHZ800 is right for almost every WS2812 strip: the lights expect green, red and blue bytes in that order, at 800 kHz. Mokxi's part uses that order, too.
Line 8 is firstHue, the color the first light shows. It is a 16-bit number, so it counts from 0 to 65535. A hue is a position on the color wheel: red is at 0, and as the number grows you pass through yellow, green, cyan, blue and magenta before you get back to red.
Lines 10 to 14, setup(), start the strip with begin(), set a brightness of 40 out of 255, and call show() once to push all-dark colors to the lights. The low brightness keeps the colors from being harsh, and on a real strip it keeps the current down.
Lines 16 to 24, loop(), draw one frame of the animation.
- Line 17 visits each light in turn.
- Line 18 works out that light's hue. It starts from
firstHueand adds a share of the wheel:i * 65536UL / PIXELS. With eight lights, each one is 8192 steps further around the wheel than the one before, so the eight lights together show the whole rainbow. TheULmakes the math use a large number type, so it does not overflow. - Line 19 turns that hue into a color.
ColorHSV()converts a hue into red, green and blue values, andgamma32()adjusts the result so the brightness looks even to the eye. - Line 21 calls
show(), which sends every color down the wire. Nothing changes on the strip until this call. - Line 22 moves
firstHueforward by 256. Because it is a 16-bit number, it wraps around to 0 by itself after 256 frames. That wrap is what makes the animation loop forever. - Line 23 waits 20 milliseconds, so each full turn of the rainbow takes a few seconds.
What you should see
I ran this circuit on an Uno in the Mokxi simulator, and all eight lights lit up, each a different color, with the colors shifting along the strip frame by frame. Press Run and look at the part on the canvas.
If the strip stays dark, check the three wires first. Then check that pin 6 in the sketch matches the pin you wired, and that the resistor is on the data line and not on power.
What to try next
- Change the speed. Make the delay on line 23 smaller for a faster roll, or add 128 instead of 256 on line 22 for a slower one.
- Reverse the direction. Subtract 256 instead of adding it.
- Make it longer. Change
counton the part andPIXELSin the sketch to 16 or 30. Add the real power notes above if you build it on a bench. - Try a solid color chase. Turn on one light at a time with
setPixelColor(), callshow(), thenclear()and move on. - Use a knob. Read a potentiometer and use it to set the brightness with
setBrightness().
Try it in your browser
Open the WS2812 part page to see the pins and the properties, and build the circuit on an Arduino Uno in the editor. If you would rather use an ESP32, the NeoPixel on ESP32 guide shows that version, and the rainbow strip project runs a hue wheel along sixteen lights on an ESP32-C3.
Ready to build it yourself? Create your free Mokxi account and open the editor in your browser. No installs, no parts to buy.