Built-in project · Arduino Uno R3

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 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.

Diwali diya row · Arduino Uno R3live0.000 s 0.00x
Watch the diyas light, then press the button to light them again
The circuit itself, running here on the simulator. Press what can be pressed; click anything else to open it in the editor.

What you'll learn

  • Driving addressable LEDs with the Adafruit_NeoPixel library
  • A series resistor on the data line
  • Random drift for natural-looking flicker
  • Timing with millis() instead of delay()
  • A pull-up button that restarts an animation
  • Difficulty: Beginner
  • Time: About 20 minutes
  • Board: Arduino Uno R3
  • Parts: 5

How it works

Eight WS2812B LEDs sit in a chain, fed from pin 6 through a 330 ohm resistor. Each LED has its own memory, so one wire drives all eight. At power-up the sketch lights them left to right, one every 700 ms.

Every lit diya keeps its own brightness that drifts toward a random goal between 150 and 252, plus a small chance each frame of a quick dip. That is why the row flickers out of step, like real oil lamps. The color is a warm yellow with a touch of orange.

A pushbutton on pin 2 (to ground, with the internal pull-up) clears the row and lights it again.

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 pinConnects to
Resistor, 330 Ω pin 1Arduino Uno R3 pin 6
Pushbutton pin 1aArduino Uno R3 pin 2
Addressable RGB LEDs pin VDDArduino Uno R3 pin 5V
Pushbutton pin 2aArduino Uno R3 pin GND
Addressable RGB LEDs pin VSSArduino Uno R3 pin GND
Addressable RGB LEDs pin DINResistor, 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.

sketch.ino · Arduino Uno R3
// 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);
}

Code walkthrough

The key functions of the sketch, and what each one does.

setup sketch.ino

Starts serial, sets the button pin to INPUT_PULLUP, and starts the pixel strip at brightness 180. It gives every diya a starting level and goal of 180, and seeds randomSeed from the unconnected pin A0 so the flicker differs each run.

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");
}

loop sketch.ino

First it checks the button: on a press it sets lit back to 0, clears the strip and waits for release. Next, once nextLight has passed, it lights one more diya. Then, for every diya, it drifts the level toward its goal by 3, sometimes dips it to two thirds, and sets the color. Unlit diyas are set to off. A 20 ms delay ends each frame.

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
  // ... 5 more lines in the editor

Troubleshooting

The usual mistakes with this circuit, and the fix for each.

  • No LEDs light, or the colors are wrong. This project
    Check the data goes in at DIN, not out. The resistor sits between pin 6 and DIN, and VDD and VSS go to 5V and GND.
  • The button does nothing. This project
    Pin 2 goes to the button, and the other side of the button goes to GND. There is no external resistor, since INPUT_PULLUP supplies it.
  • 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.