Diwali diya row on an Arduino Uno
Eight WS2812B LEDs inside clay diyas light one by one along a windowsill for Diwali, then flicker together like small oil lamps. A button lights the row again. 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.
Beginner Runs in your browser. Free, and no account needed.
The code
The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.
// Diya row: a row of eight LED diyas for Diwali that light one after another,
// left to right, and then flicker together like small oil lamps along a
// windowsill or a doorstep.
//
// diyas 8 WS2812B LEDs, DIN <- 330 ohms <- pin 6, one in each lamp
// button pin 2 -> button -> GND (INPUT_PULLUP): light them again
//
// Diwali, the festival of lights, is celebrated by Hindus, Sikhs, Jains and
// many Buddhists. The name comes from the Sanskrit for "row of lights", and
// families line their homes with diyas, small clay lamps with a cotton wick
// in oil or ghee. Put one LED inside each clay diya, or behind a paper cutout
// of one.
//
// Each flame has its own state: a brightness that drifts toward a random goal
// and a small chance each frame of a quick dip, so the row never pulses in
// step. The color is a deep yellow with a touch of orange, the color of a
// small oil flame.
//
// The diyas light by themselves at power-up. Press the button and they go out
// and light again, one by one.
//
// Things to try. Press the button. Make it a longer row with DIYAS. Light
// them from both ends toward the middle.
#include <Adafruit_NeoPixel.h>
const int DIYAS = 8;
const int BUTTON_PIN = 2;
Adafruit_NeoPixel row(DIYAS, 6, NEO_GRB + NEO_KHZ800);
uint8_t level[DIYAS], goal[DIYAS];
int lit = 0;
unsigned long nextLight = 800;
void setup() {
Serial.begin(9600);
pinMode(BUTTON_PIN, INPUT_PULLUP);
row.begin();
row.setBrightness(180);
row.show();
for (int i = 0; i < DIYAS; i++) level[i] = goal[i] = 180;
randomSeed(analogRead(A0));
Serial.println("Happy Diwali! Lighting the diyas");
}
void loop() {
unsigned long now = millis();
if (digitalRead(BUTTON_PIN) == LOW) {
lit = 0;
nextLight = now + 600;
row.clear();
row.show();
Serial.println("Lighting them again");
while (digitalRead(BUTTON_PIN) == LOW) delay(10);
}
if (lit < DIYAS && now >= nextLight) {
lit++;
nextLight = now + 700;
Serial.print("Diya ");
Serial.print(lit);
Serial.println(" lit");
if (lit == DIYAS) Serial.println("The whole row is glowing");
}
for (int i = 0; i < DIYAS; i++) {
if (i >= lit) {
row.setPixelColor(i, 0);
continue;
}
if (abs(level[i] - goal[i]) < 3) goal[i] = random(150, 253);
level[i] += level[i] < goal[i] ? 3 : -3;
int v = level[i];
if (random(0, 40) == 0) v = v * 2 / 3; // a quick dip in a draft
row.setPixelColor(i, v, v * 11 / 20, v / 16);
}
row.show();
delay(20);
}
Parts list
5 parts, plus the jumper wires. Every one is in the editor's parts bin.
- 1 × Arduino Uno R3
- 1 × Half breadboard
- 1 × Pushbutton
- 1 × Resistor, 330 Ω
- 1 × Addressable RGB LEDs
How it is wired
5 connections, pin by pin, read from the circuit itself. Each line is one set of pins joined together, by a jumper wire or a breadboard strip.
- Arduino Uno R3 pin 6; Resistor, 330 Ω pin 1
- Arduino Uno R3 pin 2; Pushbutton pin 1a
- Arduino Uno R3 pin 5V; Addressable RGB LEDs pin VDD
- Ground: Arduino Uno R3 pin GND; Pushbutton pin 2a; Addressable RGB LEDs pin VSS
- Resistor, 330 Ω pin 2; Addressable RGB LEDs pin DIN
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.