Projects / Lights-out puzzle

Lights-out puzzle

Intermediate1.7 hGuided buildReviewed

Build it step by step

0 of 7 done
  1. NANO

    Lay out the nine buttons

    In a three by three square on the breadboard to match the grid.

  2. NANO

    Wire each button

    One leg to D2 to D10 in order, the other to GND.

  3. NANO15.2 mA

    Wire the matrix

    DIN through 330 ohms to D11, 5V and GND.

  4. NANO

    Upload the sketch

    A grid of amber and blue blocks appears.

  5. NANO

    Press a button

    That block and its neighbours flip.

  6. NANO

    Solve the puzzle

    The grid turns green and a new puzzle starts.

  7. NANO

    Check the serial monitor

    It counts your moves.

In short

How do you build a lights-out puzzle?

Nine lit squares in a three by three grid and nine buttons: pressing one flips that square and its neighbours, and you try to turn them all off. A little logic game with a pattern of moves that you can work out on paper. You need Arduino Nano, WS2812B 8x8 RGB LED matrix, Tactile push button 6 mm × 9, Resistor 330 Ω, Breadboard 830 Tie Points and Jumper wires male to male (40 pcs, 20 cm). It takes 7 steps and about 100 minutes.

Level
Intermediate
Time
About 100 min
Parts
6

Parts list

Tick what you already own. It saves to My Garage, so “Build with what I have” knows.

Wiring

FromToWhy
Matrix DINNano D11 through a 330 ohm resistorData to the first LED
Matrix 5V and GNDNano 5V and GNDPower, kept dim in the sketch
Buttons 1 to 9, one leg eachNano D2 to D10 in orderButton 1 is top left, button 9 is bottom right
Buttons 1 to 9, other legNano GND (the shared rail)Pressing joins the pin to ground

Code

Each press flips the pressed square and the squares above, below, left and right of it. The sketch shuffles the board by making twelve legal presses from an all-off board, so every puzzle can be solved. The grid is drawn as nine 2 by 2 blocks with a gap between them on the 8 by 8 matrix, lit amber when on and a faint blue when off. When every block is off the whole grid turns green. Install "Adafruit NeoPixel" from the Library Manager. Most 8 by 8 matrices wire their rows in a zigzag; if the squares look scrambled, change SERPENTINE to false. The brightness is kept low and only a few LEDs are lit at once, so USB power is fine, and the 330 ohm resistor on the data line protects the first LED. Two useful facts for solving: pressing a square twice undoes it, and the order of presses does not matter, only which squares you press an odd number of times. Try writing the nine squares as a grid on paper and working out which presses clear a single corner light.

Runs on a virtual Arduino Nano in your browser, with sliders for the sensors. Happy with it? Verify and upload to your real board from the same page (Chrome or Edge on a computer).

// Library: Adafruit NeoPixel.
#include <Adafruit_NeoPixel.h>

const uint8_t DATA_PIN = 11;
const uint8_t BTN[9] = {2, 3, 4, 5, 6, 7, 8, 9, 10};
const bool SERPENTINE = true;      // set to false if your matrix rows all run the same way

Adafruit_NeoPixel strip(64, DATA_PIN, NEO_GRB + NEO_KHZ800);

bool cell[9];
bool prevDown[9];
unsigned int moves = 0;

uint16_t px(uint8_t x, uint8_t y) {
  if (SERPENTINE && (y & 1)) x = 7 - x;
  return y * 8 + x;
}

void paintCell(uint8_t i, uint32_t colour) {     // each cell is a 2 by 2 block with a gap between
  uint8_t x0 = (i % 3) * 3;
  uint8_t y0 = (i / 3) * 3;
  for (uint8_t dy = 0; dy < 2; dy++)
    for (uint8_t dx = 0; dx < 2; dx++)
      strip.setPixelColor(px(x0 + dx, y0 + dy), colour);
}

void show(bool win) {
  strip.clear();
  for (uint8_t i = 0; i < 9; i++) {
    if (win) paintCell(i, strip.Color(0, 40, 0));
    else paintCell(i, cell[i] ? strip.Color(50, 25, 0) : strip.Color(0, 0, 3));
  }
  strip.show();
}

void flipCell(int i) {
  if (i < 0 || i > 8) return;
  cell[i] = !cell[i];
}

void press(uint8_t i) {
  uint8_t x = i % 3, y = i / 3;
  flipCell(i);
  if (x > 0) flipCell(i - 1);
  if (x < 2) flipCell(i + 1);
  if (y > 0) flipCell(i - 3);
  if (y < 2) flipCell(i + 3);
}

bool allOff() {
  for (uint8_t i = 0; i < 9; i++) if (cell[i]) return false;
  return true;
}

void newGame() {
  do {
    for (uint8_t i = 0; i < 9; i++) cell[i] = false;
    for (uint8_t n = 0; n < 12; n++) press(random(9));   // legal moves only, so it can always be solved
  } while (allOff());
  moves = 0;
  show(false);
  Serial.println("New puzzle. Turn every square off.");
}

void setup() {
  for (uint8_t i = 0; i < 9; i++) pinMode(BTN[i], INPUT_PULLUP);
  Serial.begin(9600);
  strip.begin();
  strip.setBrightness(80);
  randomSeed(analogRead(A0) ^ micros());
  newGame();
}

void loop() {
  for (uint8_t i = 0; i < 9; i++) {
    bool down = (digitalRead(BTN[i]) == LOW);
    if (down && !prevDown[i]) {
      delay(20);                                  // short debounce
      press(i);
      moves++;
      show(false);
      Serial.print("Moves: ");
      Serial.println(moves);
      if (allOff()) {
        show(true);
        Serial.print("Solved in ");
        Serial.print(moves);
        Serial.println(" moves!");
        delay(2500);
        newGame();
      }
    }
    prevDown[i] = down;
  }
}

What the serial monitor should show

Open the serial monitor at the baud rate in the code. This is what our simulator prints for a virtual Arduino Nano with its sensors at their default values. Your numbers will follow your real readings, but the format should match.

New puzzle. Turn every square off.

Test checklist

What you should see at each check. If a reading is far off, stop and use the troubleshooting list.

CheckExpected
Press the corner buttonThree blocks flip (itself and two neighbours)
Press the centre buttonFive blocks flip
Press the same button twiceThe board returns to how it was
Solve itGreen grid and a new puzzle
MovesCounted on the serial monitor

If it does not work

Blocks appear in the wrong places

Change SERPENTINE to false.

Nothing lights

Check DIN goes to the input end of the matrix, not DOUT.

A button does nothing

Check its legs straddle the breadboard centre gap and one goes to GND.

Colours look wrong

Your matrix may be RGB ordered, change NEO_GRB to NEO_RGB.

Matrix flickers

Check the grounds are joined and the wires are short.

Still stuck? Find the fault step by step with your multimeter →

Ask Lark AI

Common questions

What parts do I need for the lights-out puzzle?

Arduino Nano, WS2812B 8x8 RGB LED matrix, Tactile push button 6 mm × 9, Resistor 330 Ω, Breadboard 830 Tie Points and Jumper wires male to male (40 pcs, 20 cm).

How long does the lights-out puzzle take to build?

About 100 minutes for a intermediate maker, in 7 steps.

How do I know the lights-out puzzle works?

Press the corner button: Three blocks flip (itself and two neighbours). Press the centre button: Five blocks flip. Press the same button twice: The board returns to how it was.

Blocks appear in the wrong places: what should I check?

Change SERPENTINE to false.

Nothing lights: what should I check?

Check DIN goes to the input end of the matrix, not DOUT.

A button does nothing: what should I check?

Check its legs straddle the breadboard centre gap and one goes to GND.

Colours look wrong: what should I check?

Your matrix may be RGB ordered, change NEO_GRB to NEO_RGB.