Introduction: Playing Chess Against Arduino

This project has been developed with the “Games” contest in my mind, I hope you’ll like it and give your vote :)

The idea of this project is quite simple. I want to simulate as many board games as possible using a simple electronic assembly. Of course, every game with its own software, which must be loaded into the simulators microcontroller. Thus, I could simulate games like: Ludo, Ladders and Snakes, Morris, Checkers, a bunch of different dice based games... Nowadays, unfortunately, children have lost interest in board games, what attracts them are the computer games with the most realistic graphics and audio effects, that require a large storage space and dynamic memory. Who knows, maybe working with them in carrying out this project, we will be able to distract them from the computer screens and point them to the basic games. Of course, I am aware that there are many other game projects on Arduino, but most of them are based on the use of an LCD or OLED display, I have seen very few projects similar to mine that only use RGB LEDs assembly to simulate games.

For a first draft of this project, I thought of doing a simulation of the game Ladders and Snakes, but in the end I told myself why not do the oldest strategy game of all time, played by millions of people, the game of games, which is Chess. I could have made a system that connects to the Internet and takes over the moves from a powerful chess program (like for example stockfish...). I could have used a hybrid system like a microcontroller (in the game simulator) connected to a Raspberry Pi, I could have entered and displayed the moves on an LCD keypad, as in the micro-Max variant on the Arduino Mega. But no. I was stubborn enough to make a completely autonomous system to play chess on an... * drumroll * Arduino PRO MINI and a display made with an array of RGB leds .

Step 1: Overview

However, by using an Arduino Mini Pro I had to overcome a very large obstacle, the 2kb memory limit of the Atmega328p microcontroller! I found one chess engine, by H.G. Muller's micro-Max, ported to the Arduino UNO platform, you can find the complete project on Hackaday. As you can see, in this project, the chess game is displayed on the serial line monitor. So you need a computer connected to Arduino to play. Through my project, the simulator consists of a display of 256 RGB LEDs, type WS2812 (4 arrays of 8x8 LEDs), where the chessboard and the game pieces appear, and by using touch sensors, you can specify the moves. The system is completely independent, we only need a power source :)

In the first phase, I used the FastLed library to drive the display, * cough cough * but let's do a little bit of math: the colours for each RGB led are composed of one led for each basic colour (red, green, blue) and the values of the 3 colours are in the range 0-255 (so we can have for each led 255*255*255 = 16581375 colours). For each RGB led we need 8 bit information per basic colour, 24 bits (3 bytes) for the colour displayed by the RGB led. For 256 RGB LEDs, we need 3*256 = 768 bytes, which occupies 768 bytes of the 2 kbytes memory we have available. It is very, very much. We only have 1280 bytes left. I have also the program itself that takes up memory with the various global variables that I used in the program, at one point I had only 680 bytes available! It's too little for the chess program to work properly, I tried several variants, I tried to maximize the available memory, but after the first 4-5 moves the system crashes. So I had to use another library, another way to approach the problem.

The most used RGB led display libraries are Fastled and Neopixel, but I could not use them because of memory problems. Looking for a solution, I came across a small library that I had never heard of before, but it seemed to solve the negative aspects of the mentioned libraries. It is called FAB_LED and you can find it here on github. What does this library do? It can display colours in a specific palette, a palette that can be encoded in 8 bits (256 colours at a time), 4 bits (16 colours at a time) and 2 bits (4 colours at a time). Using this method of palettes and 2-bit colour coding (4 colours at a time - I only need 2 colours for the chessboard and 2 colours for the pieces), if we do a bit of math, we will find out that the 256 RGB LEDs will take up 256*2 = 512 bit memory, which means 64 bytes !!! So we have plenty of memory for the chess program to work.

In addition to the LED display, I also used 3 touch sensors (the project allows up to 8 touch sensors) to move the "cursor" to the right and forward, and to select the position. The development board I used is a 5 Volt Arduino Pro Mini board with the ATMega 328p microcontroller.

Step 2: Construction

Things needed for the project:

for the enclosure

- wooden photo frame (166x166mm interior- I ordered mine online from a a frame maker company);

- 165x165mm 3mm grey smoked transparent plexiglass sheet;

- a piece of 165x165 white copier paper sheet;

- 3D printed 165x165mm 16x16 plastic grid;

The enclosure is very similar to that from the VERBIS - word clock I just didn't use screws for the assembly but I glued everything with a hot glue gun. The parts and the assembly are depicted in the above photos.

for the electronics

- 65x65mm RGB 8x8 LedMatrix - 4 pieces;

- TTP223 capacitive touch sensor modules - at least 3 pieces;

- Arduino Pro Mini module 5v/16Mhz;

- Female DC Conector cable 5.5mmx2.1mm(2.5mm);

- 5v/2A Power supply with 5.5mmx2.1mm(2.5mm) Male connector;

- Some 2.54mm 1 row straight pin (male) header;

- Colored wires;

The schematics are provided below (also between the photos above), and in the pictures you can see the assembly during the first testings with an Arduino Uno.

Like I said I was using in the beginning the FastLed library and a Led Matrix library for easy of definition of the 16x16 led's matrix. The arrangement of the four 8x8 matrices must be made like in the image below.

In this way when I want to light up the 256 RGB leds one after another they will light up like in the video bellow, exactly how the connections are made.

But I need to define my LED's in an 2D coordinate system, I chose the origin to be in the down-left corner, the x axis to be horizontal from left to right and the y axis from down to up, in the video below you can see what am I talking about (more at the next step).

In the last main picture from this step you can see how the final electronic connections looks like.

Step 3: Software

As I said before, to drive the LED display I used the LED_FAB library. I will try to explain what some of the functions that I used do:

ClearBoard - at every step in the loop every led in the display is initialized to the (1,1,1) RGB color (dark grey), I thought that the redraw of the chess square model of the board and the redraw of the pieces will be enough to erase the previous colors but to my surprise not every "pixel" was erased, I don't know why...

DisplayBoard - with this function the square model of a chess table is drawn, using dark grey (1,1,1) and light grey (8,8,8) colors (codified as 0 and 1 in the palette);

DisplayChessPieces - the name tells the purpose of this function. I thought it would be nice to display the chess pieces on the board, you can play in this way, or you can use an old magnetic chess set that fits the square dimensions (you can see how it looks in the main video - next step). The pieces are displayed as below:

PawnRookKnightBishopQueenKing

ShowCursor - it displays a flashing square of 4 pixels (it changes the four individual leds color of the square) on the board and because it is called every 60 milliseconds, the flashing frequency is rather low

ShowMove - it displays 2 flashing squares, it is called every 20 milliseconds, so the flashing frequency is rather high

buttonRIGHT, buttonUP - takes the touch sensors signal and updates the cursor position

buttonOK - it selects the start and the end position of the human moves but also continues the game after the machine moves are shown

XY - transforms the x,y position into a led number on the display

The other functions are used by the chess engine..

I uploaded the code on github, you can download and program your own Arduino Pro Mini ChessMaster :)

Step 4: GamePlay

As you saw in the code, the serial monitor is not used, so you can rely only on the chessboard's visual feedback. But essentially, you need to move with the RIGHT and UP touch sensors the slowly blinking cursor under the chess piece that you want to move, select with the OK touch sensor the starting point, the cursor will start blinking much faster, move the cursor to the final move point in the same way again, and select with the OK button. Make your move with the physical chess piece (if you are using a chess set) and touch the OK button. Your move will be made, the machine will make its move, and you will see the move shown by two fast blinking squares. Move the machine's physical chess piece (if you are using a chess set). Press OK and you can continue the game. That's it. It seems a little bit complicated but after a little time you will get used to it. In the video above, I filmed a short game. I am a very weak chess player so I admit that I cheated. I used a site that recommended the next moves depending on a certain position. Of course, micro max was going to be defeated this way but I thought that it handled the task more than honorably (micro-Max is accounted with ELO of about 2000, which is not too bad).

What is still missing from the gameplay is the fact that it does not show an illegal move more visibly (now, the actual program does not make the move and only the cursor appears in the initial position) and also the fact that is does not signal the loss or gain of the game (the program simply stops).

However, in this state the game board is very well suited for understanding chess and its rules by children.
I hope as many of you will try to build this project and give it as much satisfaction as possible.

Games Contest

First Prize in the
Games Contest