Rangoli lights on an Arduino Uno
A 16x16 WS2812B panel draws glowing rangoli patterns for Diwali, each symmetric eight ways from the center, with colors flowing outward and a button for the next design. The whole build, an Arduino Uno and 3 more parts, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.
Teaching? Assign it to your class in one click.
Intermediate About 20 minutes to build and run. Runs in your browser. Free, and no account needed.
The parts retail for about $33. On Mokxi they're free.
What you'll learn
- Symmetry in code by folding coordinates
- Addressable LEDs with Adafruit_NeoPixel
- Making colors with hue (ColorHSV)
- Changing designs with a button
- Why big LED panels need low brightness and their own supply
- Difficulty: Intermediate
- Time: About 20 minutes
- Board: Arduino Uno R3
- Parts: 5
How it works
A 16x16 panel of 256 addressable LEDs is driven from pin 6 through a 330 Ω resistor. The sketch draws rangoli patterns that look the same in eight mirrored places around the center, and the colors slowly flow outward.
For each LED it measures how far it is from the center across and down, then folds those two distances so only their sizes matter, not their order. A formula turns them into a color, so symmetric spots get matching colors.
A button on pin 2 moves between three designs: rings, diamond lotus and eight petals.
Parts list
5 parts, plus the jumper wires. They retail for about $33; on Mokxi they're free.
- 1 × Arduino Uno R3In Starter Kit, Claw Machine Kit, Vending Machine Kit.
- 1 × Half breadboard
- 1 × PushbuttonIn Starter Kit, Christmas Lights Kit, Logic Lab Kit.
- 1 × Resistor, 330 ΩIn Starter Kit, Claw Machine Kit, Halloween Prop Kit.
- 1 × Addressable RGB LEDsIn Claw Machine Kit, Halloween Prop Kit, Christmas Lights Kit.
Wiring table
Every connection, pin by pin, read from the circuit itself. Each row joins the pin on the left to the pin on the right, by a jumper wire or a breadboard strip.
| Part and pin | Connects to |
|---|---|
| Resistor, 330 Ω pin 1 | Arduino Uno R3 pin 6 |
| Pushbutton pin 1a | Arduino Uno R3 pin 2 |
| Addressable RGB LEDs pin VDD | Arduino Uno R3 pin 5V |
| Pushbutton pin 2a | Arduino Uno R3 pin GND |
| Addressable RGB LEDs pin VSS | Arduino Uno R3 pin GND |
| Addressable RGB LEDs pin DIN | Resistor, 330 Ω pin 2 |
The code
The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.
// Rangoli lights: a 16x16 panel of LEDs that draws glowing rangoli patterns,
// each one symmetric eight ways from the middle, slowly turning through the
// colors. A button moves to the next design.
//
// panel 256 WS2812B LEDs, a 16x16 matrix, DIN <- 330 ohms <- pin 6
// button pin 2 -> button -> GND (INPUT_PULLUP)
//
// At Diwali, families draw rangoli at the door: patterns of colored powder,
// rice or flower petals, often in rings of petals around a center. Most
// rangoli are symmetric, so the same shape repeats as you turn around the
// middle. That is easy to build in code: measure each LED's distance from the
// center across and down, and fold so that only the two distances matter and
// not their order. Any color worked out from them is the same in all eight
// mirror places.
//
// There are three designs: rings, a diamond lotus, and an eight-petal flower.
// Each colors an LED by a shape number plus a slowly growing offset, so the
// colors flow outward from the middle.
//
// The panel needs 768 bytes for its colors, more than a third of the Uno's
// memory. At full white 256 LEDs could draw 15 A, so the brightness is kept
// low and a real panel wants its own 5 V supply.
//
// Things to try. Press the button. Invent a fourth design in shape().
#include <Adafruit_NeoPixel.h>
const int SIZE = 16;
const int BUTTON_PIN = 2;
const int DESIGNS = 3;
Adafruit_NeoPixel panel(SIZE * SIZE, 6, NEO_GRB + NEO_KHZ800);
int design = 0;
uint16_t offset = 0;
const char *NAMES[DESIGNS] = {"rings", "diamond lotus", "eight petals"};
// A number for the LED at a, b: the two folded distances from the center,
// 1, 3, 5 ... 15 each, with a >= b.
uint16_t shape(int a, int b) {
switch (design) {
case 0: return (a * a + b * b) * 40; // rings
case 1: return (a + b) * 600 + (a - b) * 200; // diamond lotus
default: return (a - b) * 900 + (a * b) % 16 * 300; // eight petals
}
}
void setup() {
Serial.begin(9600);
pinMode(BUTTON_PIN, INPUT_PULLUP);
panel.begin();
panel.setBrightness(40);
Serial.println("Rangoli lights: design 0, rings");
}
void loop() {
if (digitalRead(BUTTON_PIN) == LOW) {
design = (design + 1) % DESIGNS;
Serial.print("Design ");
Serial.print(design);
Serial.print(", ");
Serial.println(NAMES[design]);
while (digitalRead(BUTTON_PIN) == LOW) delay(10);
}
for (int y = 0; y < SIZE; y++) {
for (int x = 0; x < SIZE; x++) {
int dx = abs(2 * x - (SIZE - 1));
int dy = abs(2 * y - (SIZE - 1));
int a = dx > dy ? dx : dy;
int b = dx > dy ? dy : dx;
uint16_t hue = shape(a, b) - offset;
panel.setPixelColor(y * SIZE + x, panel.ColorHSV(hue, 255, 255));
}
}
panel.show();
offset += 400;
delay(30);
}
Code walkthrough
The key functions of the sketch, and what each one does.
shape sketch.ino
Takes the two folded distances a and b and returns a number for the color of that LED. A switch on design gives one formula each for rings, diamond lotus and eight petals. Adding a case is how you invent a fourth design.
// A number for the LED at a, b: the two folded distances from the center,
// 1, 3, 5 ... 15 each, with a >= b.
uint16_t shape(int a, int b) {
switch (design) {
case 0: return (a * a + b * b) * 40; // rings
case 1: return (a + b) * 600 + (a - b) * 200; // diamond lotus
default: return (a - b) * 900 + (a * b) % 16 * 300; // eight petals
}
}setup sketch.ino
Starts serial, sets the button to INPUT_PULLUP, starts the panel and sets the brightness to 40, which keeps the current low.
void setup() {
Serial.begin(9600);
pinMode(BUTTON_PIN, INPUT_PULLUP);
panel.begin();
panel.setBrightness(40);
Serial.println("Rangoli lights: design 0, rings");
}loop sketch.ino
On a button press it moves to the next design and waits for release. Then for every LED it folds the distances, takes the hue from shape minus offset, and sets that pixel. It shows the panel, adds 400 to offset so colors move, and waits 30 ms.
void loop() {
if (digitalRead(BUTTON_PIN) == LOW) {
design = (design + 1) % DESIGNS;
Serial.print("Design ");
Serial.print(design);
Serial.print(", ");
Serial.println(NAMES[design]);
while (digitalRead(BUTTON_PIN) == LOW) delay(10);
}
for (int y = 0; y < SIZE; y++) {
for (int x = 0; x < SIZE; x++) {
int dx = abs(2 * x - (SIZE - 1));
int dy = abs(2 * y - (SIZE - 1));
int a = dx > dy ? dx : dy;
int b = dx > dy ? dy : dx;
uint16_t hue = shape(a, b) - offset;
panel.setPixelColor(y * SIZE + x, panel.ColorHSV(hue, 255, 255));
}
}
panel.show();
offset += 400;
delay(30);
}Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- A real panel flickers or the board resets This project
256 LEDs can draw a lot of current. Keep the brightness low and power the panel from its own 5 V supply. - The picture is garbled or dark This project
Check the data line: pin 6, through the 330 Ω resistor, to the panel's DIN. - The input reads random HIGH and LOW when the button is not pressed. Pushbutton
A floating pin picks up noise. Add a 10 k pull-down or pull-up resistor, or use INPUT_PULLUP and wire the button between the pin and GND. Then pressed reads LOW. - The button seems always pressed, or never changes. Pushbutton
A 4-leg tactile switch has legs joined in pairs. Place it across the breadboard center gap so the pairs sit on different rows. Wired along one side, the legs are already connected. - The LED is dim, burns out, or the pin reads wrong because the resistor value is off. Resistor
Read the color bands again. A 220 ohm to 330 ohm resistor suits a typical LED on 5 V. 10k is a common pull-up or pull-down value. Check with a multimeter if unsure. - A pull-up or pull-down resistor is missing, so the input floats and reads random values. Resistor
Tie the input to 5 V or ground through about 10k. Or use INPUT_PULLUP and wire the switch to ground. - The first LED flickers, shows wrong colors, or the whole strip glitches. Addressable RGB LEDs
Join the grounds of the strip and the controller. Add a 300 to 500 ohm resistor in series with the data line. Put a 1000 uF capacitor across the strip's 5 V and GND. - The strip works at low brightness but dims, turns yellow, or resets when many LEDs go white. Addressable RGB LEDs
Each LED can draw about 60 mA at full white. Use a 5 V supply sized for the count, feed power in at several points, and lower the brightness in code.
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.