Projects / Two-player chess clock

Two-player chess clock

Beginner1.2 hGuided buildReviewed

Build it step by step

0 of 7 done
  1. NANO10.9 mASSD1306

    Wire the OLED

    SDA to A4, SCL to A5, 5V and GND.

  2. NANO

    Wire three buttons

    D2, D3 and D4, each other leg to GND.

  3. NANO

    Wire the buzzer

    D8 and GND.

  4. NANO

    Upload the sketch

    The screen shows the setup message and 05:00 for each player.

  5. NANO

    Press the mode button

    The time cycles 03:00, 05:00 and 10:00.

  6. NANO

    Press a player button

    The other player's clock starts.

  7. NANO

    Play a round

    Press your own button to hand over the move.

In short

How do you build a two-player chess clock?

Two big buttons and a small screen make a chess clock: press your button to stop your time and start your opponent's, with a beep when a player runs out. You can set the time with a third press. A good practice project in state machines. You need Arduino Nano, Tactile push button 6 mm × 3, SSD1306 OLED Display, Passive buzzer, Breadboard 830 Tie Points and Jumper wires male to male (40 pcs, 20 cm). It takes 7 steps and about 70 minutes.

Level
Beginner
Time
About 70 min
Parts
6

Parts list

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

Wiring

FromToWhy
Player A buttonNano D2 and GNDPress after your move
Player B buttonNano D3 and GNDPress after your move
Mode buttonNano D4 and GNDChoose the time, pause and restart
Passive buzzer + and -Nano D8 and GNDBeeps
OLED SDA and SCLNano A4 and A5I2C data and clock
OLED VCC and GNDNano 5V and GNDPower for a 0.96 inch module

Code

The clock has five states: SETUP, A running, B running, PAUSED and OVER. The rule is simple: you press your own button after your move, which stops your clock and starts your opponent's. In SETUP, the mode button steps through 3, 5 and 10 minute games, and the first press of a player button starts the other player's clock. Pressing the mode button during play pauses the clock and pressing it again resumes; when someone runs out of time the buzzer sounds and the mode button returns to SETUP. The sketch subtracts the time since the last pass of the loop from the running player's clock, so button handling never slows it down, and the screen is redrawn every tenth of a second. Install "Adafruit SSD1306" and "Adafruit GFX" from the Library Manager. Most 0.96 inch OLED modules use address 0x3C, and a few use 0x3D. Use two large buttons, or mount two buttons on a card, so players can hit them quickly. Try adding a Fischer increment that adds a few seconds to your clock when you press your button.

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

// Libraries: Adafruit SSD1306 and Adafruit GFX.
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

Adafruit_SSD1306 oled(128, 64, &Wire, -1);

const uint8_t BTN_A = 2;
const uint8_t BTN_B = 3;
const uint8_t BTN_MODE = 4;
const uint8_t BUZZ = 8;

const long PRESETS[3] = {180000L, 300000L, 600000L};   // 3, 5 and 10 minutes
uint8_t preset = 1;

enum State { SETUP, RUN_A, RUN_B, PAUSED, OVER };
State state = SETUP;
State resumeState = RUN_A;
long timeA, timeB;
unsigned long lastTick, lastDraw;
bool prevA, prevB, prevMode;

void resetTimes() {
  timeA = PRESETS[preset];
  timeB = PRESETS[preset];
}

void fmt(long ms, char *out) {                     // writes mm:ss
  if (ms < 0) ms = 0;
  long s = (ms + 999) / 1000;
  sprintf(out, "%02ld:%02ld", s / 60, s % 60);
}

void draw() {
  char a[8], b[8];
  fmt(timeA, a);
  fmt(timeB, b);
  oled.clearDisplay();
  oled.setTextColor(SSD1306_WHITE);
  oled.setTextSize(1);
  oled.setCursor(0, 0);
  if (state == SETUP) oled.print("Pick time, press to go");
  else if (state == PAUSED) oled.print("PAUSED");
  else if (state == OVER) oled.print(timeA <= 0 ? "A is out of time!" : "B is out of time!");
  else oled.print(state == RUN_A ? "A is thinking" : "B is thinking");
  oled.setTextSize(2);
  oled.setCursor(0, 22);
  oled.print("A ");
  oled.print(a);
  oled.setCursor(0, 46);
  oled.print("B ");
  oled.print(b);
  if (state == RUN_A) oled.fillCircle(120, 29, 4, SSD1306_WHITE);
  if (state == RUN_B) oled.fillCircle(120, 53, 4, SSD1306_WHITE);
  oled.display();
}

void setup() {
  pinMode(BTN_A, INPUT_PULLUP);
  pinMode(BTN_B, INPUT_PULLUP);
  pinMode(BTN_MODE, INPUT_PULLUP);
  pinMode(BUZZ, OUTPUT);
  Serial.begin(9600);
  oled.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  resetTimes();
  draw();
}

void loop() {
  bool a = (digitalRead(BTN_A) == LOW);
  bool b = (digitalRead(BTN_B) == LOW);
  bool m = (digitalRead(BTN_MODE) == LOW);
  bool pressA = a && !prevA;
  bool pressB = b && !prevB;
  bool pressM = m && !prevMode;
  prevA = a; prevB = b; prevMode = m;

  unsigned long now = millis();
  long elapsed = (long)(now - lastTick);
  lastTick = now;

  if (state == SETUP) {
    if (pressM) { preset = (preset + 1) % 3; resetTimes(); draw(); }
    if (pressA) { tone(BUZZ, 1200, 40); state = RUN_B; }   // A has moved, so B's clock starts
    if (pressB) { tone(BUZZ, 1200, 40); state = RUN_A; }
  } else if (state == RUN_A || state == RUN_B) {
    if (state == RUN_A) timeA -= elapsed; else timeB -= elapsed;
    if (pressA && state == RUN_A) { tone(BUZZ, 1200, 40); state = RUN_B; }
    else if (pressB && state == RUN_B) { tone(BUZZ, 1200, 40); state = RUN_A; }
    if (pressM) { resumeState = state; state = PAUSED; }
    if (timeA <= 0 || timeB <= 0) {
      state = OVER;
      tone(BUZZ, 800, 1000);
      draw();
    }
  } else if (state == PAUSED) {
    if (pressM) state = resumeState;
  } else if (state == OVER) {
    if (pressM) { state = SETUP; resetTimes(); draw(); }
  }

  if (now - lastDraw > 100) {
    lastDraw = now;
    draw();
  }
}

Test checklist

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

CheckExpected
Start of the gameTimes show for both players
Mode button in setup3, 5 and 10 minutes
Player button pressOther clock runs, marker moves
Mode button in playPauses and resumes
Time runs outLong beep and a message

If it does not work

Blank screen

Check the OLED wires and try address 0x3D.

Times are not changing

Press a player button first to start the clock.

Buttons do nothing

Check each goes to its pin and to GND.

No beep

Check you have a passive buzzer and the pins are right.

Display looks cramped

Keep text size at 2 for the times.

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

Ask Lark AI

Common questions

What parts do I need for the two-player chess clock?

Arduino Nano, Tactile push button 6 mm × 3, SSD1306 OLED Display, Passive buzzer, Breadboard 830 Tie Points and Jumper wires male to male (40 pcs, 20 cm).

How long does the two-player chess clock take to build?

About 70 minutes for a beginner maker, in 7 steps.

How do I know the two-player chess clock works?

Start of the game: Times show for both players. Mode button in setup: 3, 5 and 10 minutes. Player button press: Other clock runs, marker moves.

Blank screen: what should I check?

Check the OLED wires and try address 0x3D.

Times are not changing: what should I check?

Press a player button first to start the clock.

Buttons do nothing: what should I check?

Check each goes to its pin and to GND.

No beep: what should I check?

Check you have a passive buzzer and the pins are right.