Raspberry PI Noughts and Crosses / Tic Tac Toe

 

Build this noughts and crosses game first and you can build the chessboard!

 

This project requires only a basic ability to set up a Raspberry pi and solder electronic components, but you will learn all you need to build the chess system.

This section gives detailed instructions for building a Raspberry PI Noughts and Crosses game, know in the USA as Tic Tac Toe, hereafter referred to as NOX.  This is an ideal project to build before trying to build the chess board. It uses exactly the same components, but instead of being played on a 8×8 grid like chess, its played on a 3×3 grid. So their are many fewer connections and the game logic is simpler.

In this build I use exactly the same components as for the chess board, just fewer of them. Even the most basic builder and coder should be able to build this. When you build this you will learn useful skills and techniques so that when you come to build the chess board it will go more smoothly. This makes it a bit overkill for NOX, but a great training project.

In play there will be wooden X and O pieces with magnets on the back that are placed on a board with Reed switches that register the placement. To move the NOX game will flash LEDs to show where it wants to go. The screen and buttons will indicate who’s move it is, allow setting of levels etc.

As with the Chess game the NOX game uses these components:

rpii2cbusnox

The Raspberry PI drives the other bits using an I2C bus, so there are only 4 wires to connect to the PI. You can use any PI. As this is a simple program in a small enclosure I use a PI Zero , but a PI2 or PI3 should work just as well. In this design the reed switch grid is a 3 x3 matrix which can be controlled by just one MCP23017 and the LED driver controls the 3×3 LEDs.

Raspberry Pi NOX Parts List:

Electronic components list
1 Raspberrty Pi Zero  £       4.00
1 MCP23017  £       0.64
9 Reed Switch Glass Normally Open  £       0.99
1 Adafruit HT16K33 16×8 LED Matrix Driver  £       7.99
9 5mm LED s 5v  £       0.64
32 10mm x 1mm Neodymium magnets  £       1.30
1 Solderless Breadboard Bread Board 400 Tie Points  £       1.64
1 Sain Smart I2C IIC LED Screen + Keypad  £       6.99
Total Cost  £    24.19

The prices given are just a guide. In addition you will need wood or board for the case, and  wires. The wire I use is 1/0.6 single core equipment wire (not stranded wire) and I have lots of colours. Single core wire is easy to work with and can be inserted directly into a breadboard. The colours help get connections right. Single core wire is not good if it needs to flex or bend in use. Then use stranded wire.

Steps in building the Raspberry Pi NOX game:

  • Prepare the Pi and load software
  • Build the case
  • Mount the components
  • Wire the LED grids
  • Wire the Reed switches
  • Connect the display

Prepare the Pi and load software

I am assuming that you know how to set up a Pi with the Raspian operating system, so go to the Raspberry Pi Download page  and get the latest version. If you have an existing PI ready loaded you could update and upgrade it, but I find that its quicker to re-load from scratch, especially as the new version includes VNC and I2C.

As part of the Raspberry Pi configuration make sure you enable I2C in rasp-config.

It is not vital but I find the easiest way to develop systems is to enable VNC and run the Pi headless from a PC over a wireless network.

Test you have I2c configured by typing:

sudo i2cdetect -y 1

and you should see:

pi@raspberrypi:~/$ i2cdetect -y 1
0 1 2 3 4 5 6 7 8 9 a b c d e f
00: — — — — — — — — — — — — —
10: — — — — — — — — — — — — — — — —
20: — — — — — — — — — — — — — — — —
30: — — — — — — — — — — — — — — — —
40: — — — — — — — — — — — — — — — —
50: — — — — — — — — — — — — — — — —
60: — — — — — — — — — — — — — — — —
70: — — — — — — — —

Which shows its working, but nothing is connected.

load the Adafruit libraries for Raspberry PI

>git clone https://github.com/adafruit/Adafruit_Python_LED_Backpack.git
>cd Adafruit_Python_LED_Backpack
>sudo python setup.py install

>git clone https://github.com/adafruit/Adafruit_Python_CharLCD.git
>cd Adafruit_Python_CharLCD
>sudo python setup.py install

Load my noughts and crosses files:

>git clone  https://github.com/mdobres/maxnox.git

This will copy over the following files:

noxled.py         tests the LED grid
noxreed.py       tests the reed switches
noxledreed.py  tests the reed switch with LEDs
noxfullsys.py    tests all components
maxnox.py       full system game

REBOOT!   – always a good idea

Build the case

 

nox_box

Case and front view

I am using a board with 1.75″ squares. This is a good size for the eventual chess board and makes it easy to see. I am also leaving plenty of space for the components, so the case measures 7″ wide x 11″ long and is about 1.5″ deep. The front and back are made from 3mm board and the sides from 6mm x 3mm Pine strip wood. All of which I had left over from other projects. I cut a hole for the display and then sprayed it silver.

Rear of front panel

Rear of front panel

 

Note that the hole is a bit of a mess. This will not matter as it will be covered later with a flexible skin.

Then I drew the NOX grid on both sides  checking that they aligned. This makes fixing components so much easier.

 

 

 

Mount the components

nox_breadboard

Solderless Breadboard Bread Board 400 Tie Points

We need a way of mounting the MCP23017 and the Adafruit HT16K33 16×8 LED Matrix Driver. There are lots of ways to do this. I chose a breadboard for simplicity. It also means you can undo connections if you get them wrong or remove components if they fail. The solid core wire I am using goes straight into the connection holes and holds pretty tight.

 

The HT16K33 comes with legs to be soldered on, so do that first. Then position the two components on the breadboard as shown above. To make it easy to follow my instructions make sure each part is oriented as shown.  Double click on the image and you will see that the HT16K33 has the cathode connections C7 to C0 uppermost in the diagram and the MCP23017 is positioned with the little semi-circle cut out to the the left.

All the Red wires are 5v Live(+)  and the Green wires are Ground(-). Yellow is SDA and Orange is SDL.

The HT16K3 has labels on its connections, it has its GND  connected to the (blue) Ground(-) rail of the breadboard by a Green wire and the VDD(+) to the live rail, by a Red wire. A red wire also connects the two + rails of the breadboard. The default I2C address of the HT16K3 is X70 which requires no further connection. (more on this later)
mcp23017-addresspins1-300x203

The MCP23017 first needs to have its I2C address set using pins A2,A1, A0. We want to use Address 0X21, because the Sainsmart display already uses 0X20. From this table you can see that this means connecting: A2=GND; A1=GND; A0 = + (it says 3v, but the data sheet says 5v is OK)

mcp23017 pinout

mcp23017 pinout

This pin-out chart shows the semi-circle cutout out so you can see that  A2,A1, A0 are on the top right of the MCP23017 in the picture and are connected as described above to GND, GND, 5v+.

Pin 18, Reset and Pin 9 VDD also need to be connected to “+”. Pin Vss is connected to GND .

Finally SCL (pin 12) is connected to SCL on the  HT16K3 (Yellow wire) and SDA(Pin 13) to HT16K3 SCL (Orange wire)

I2c Connection to the Raspberry Pi

Now you can connect four wires  5v VCC(+), GND(-) , SCL & SDA  from the appropriate slots on the breadboard to the PI to test that you can see the devices. I suggest you use Red, Green, Yellow & Orange wires for clarity and consistency. I use pre-wired jumper wire for this that has one male and one female end.

Note: under most circumstances RPI pins need to be at 3v, connecting a 5v device can cause severe damage however according to Adafruit:

“Since these io expander chips use i2c to communiate, you can power them from 5V while still connecting the i2c data lines to a 3.3V device like the pi. That’s because the Pi has two i2c resistors that pull up SDA/SCL to 3.3V. Just make sure not to connect any resistors to SDA/SCL to 5V and you can power the chip from 5V (and have 5V input/output on the MCP chip). Its also fine of course to power the MCP chip from 3.3V but the 5V line on the Pi has more current capability so you might find its better to go that way.”

The Sainsmart LCD does not work well at 3v, so I have used the 5v connection and not had a problem, but its your risk.

Make sure your PI is switched off and powered down before you make the connections.

I2c Connections

I2c Connections

Once you are sure the connections are right, boot up the PI, 
get to a command prompt and type:

                                            i2cdetect -y 1

You should get:
pi@raspberrypi:~/$ i2cdetect -y 1
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- 21 -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: 70 -- -- -- -- -- -- --

This shows that you have two devices connected to the Raspberry PI I2C bus with address 21 (MCP23017) and 70 (HT16K3). Congraulations! How Shutdown the PI and carry on with the next stage. If you get a 71 instead of a 70 read the note at the end on issues.

Wiring the LEDs

Now we wire the LED matrix. First drill holes, insert the LEDs and identify the longer “Anode” leg of the LED and bend it so its horizontal as in this picture, then when you solder the vertical wires they will join all the Anodes. So in this picture the pink purple and white wires join anode legs and connect to A0, A1 & A2 on the HT16K3. The you solder the cathodes to wires that go to C0, C1 and C2 on the HT16K3. When you are done trim the LED wires back to the solder joint as shown.

nox_led

A note on wires: You will see that the wire is only exposed where it is soldered onto the LED leg. We need the insulation to protect from other crossing wires. I found a neat way to do this. so you get the gaps in the right places.

Now we can test this matrix by connecting our Raspberry Pi with the four I2c wires and running the python program noxled.py.  If it all works you will see the LEDS light one by one in the order 1 to 9 as shown above. Try and get then all to light in the right order by checking the wiring, i.e. that the correct wire goes to the correct connection on the HT16K3 as the other program use this to match with the reed switches and game logic. If no LEDs light you may have got the Anodes and Cathodes of the LEDs round the wrong way.

Wiring the Reed switches

The Reed switches are connected to the MCP23017 see pin-out above. Each Reed switch has two connections. One end goes to a common Ground , the other end goes directly to one of the connections GPA0-A7 (pins 21-28) and GPB0 (pin1). It doesn’t matter which end of the Reed switch.  The first step is trim the Reed switch legs and glue the Reed switch to the board. Note the position, not in the centre, but midway on a half diagonal to the corner as shown below. Then solder an Ground wire that connects the ends of all the Reed switches to the Ground rail of the breadboard. Prepare the wire using the  method described in Tips & Techniques, so that it the insulation is only bare where it needs to be soldered.

nox_earth

Next we connect the other end of each of the Reed switches directly to the MCP23017 connection on the breadboard.

Using the 1-9 numbering in the LED diagram the connections are:

reedconnect

And when its done it looks like a load of spaghetti, so be careful to get the connections right. At least using a breadboard you can replug if you get them wrong.

nox_reeds

When you think its right connect your Raspberry Pi with the four I2c wires. Run

i2cdetect -y 1

to check you still see both I2c devices, then run the python program noxreed.py. Stick one of the magnets on a spare bit of wood and hover it over a Reed switch. The Switch is “normally open” so when you place the magnet it closes and when you remove it, it opens again so when you put the magnet near the program will display a message on the console:

Square 7 Reed switch close

when you put the magnet over the switch and

Square 7 Reed switch open

when you remove it.

Test for synchronization of reed switches and LED

If that works run

noxledreed.py

Then move your magnet over the reed switch. If its all connected correctly this will light the LED for that square as the magnet closes  the reed switch and switch off the LED when the magnet is lifted. If this works, you’ve done the hard part. Congratulations!!

Connect the I2C  LED Screen + Keypad

 

lcd-connections

Shutdown and power off the Pi and now fix the “Sain Smart I2C IIC LED Screen + Keypad” to the front panel (I used bolts)  and connect the four wires  5v VCC(+), GND(-) , SCL & SDA  from the appropriate slots on the breadboard to the display as shown.

Everything Connected!

Everything Connected!

Connect your Raspberry Pi with the four I2c wires. Run

i2cdetect -y 1

You should now see three connected devices at 0x20, 0x21 and 0x70. The display and keypad is 0x20. In fact its controlled by a built in MCP23017.

To test the display just run the example program that came with the software you downloaded earlier. Its in the directory

pi/adafruit/Adafruit_Python_CharLCD/Examples

and called

char_lcd_plate.py

Run this and it tests the display and all the buttons.

Now run this final test program that tests all three components. When you place a magnet on a square it lights up and the square is identified in the display.

noxfullsys.py

If that works you are ready to put the finishing touches to your project and run the maxnox.py application.

Finishing Touches for the Raspberry PI Naughts and Crosses / Tic Tac Toe

Now we need to just finish off by installing a skin over the keypad/display to tidy up the front. This template

lcdfullcovertemplate

can be laminated using a home laminator and then fixed to the front cover as shown to give button labels and cover the rough hole. If you do this you will find there is a gap between the cover and the buttons. Fix this by gluing 8mm shirt type buttons to the back of the template as shown. Don’t glue them to the LCD buttons, you can easily glue then shut that way.

You will also have to decide how you are going to power this. You could build in a battery pack or just have a USB connection on the side as I have. I leave that to you.

If you are keeping this as a permanent project you should work out how to get the program to start at boot.

If your Raspberry PI Naughts and Crosses / Tic Tac Toe system is working you now have all the skills and knowledge you need to build a Rasperry Pi chess system. The components are the same, there are just more of them.

Issues:

 

Before

Before

HT16K33  LED Matrix Driver

When we type this should show up as address ox70. However I got a faulty one that

swapped between 70 and 71. I checked with Adafruit and they said it was a faulty resistor and the address floated so they sent me a new one.

 

Solder blob on A0

Solder blob on A0

I fixed this by forcing the address to be ox71 permanently by using three A0, A1 A2 solder jumpers. Each one of these is used to hardcode in the address. By bridging  jumper A0 with solder it adds 1 to the address making it 0x71. This seemed to work.

Next I then edited the line that defines the address in code:

display = Matrix8x8.Matrix8x8(address=0x70, busnum=1)

to change address=0x70 to address=0x71

 

Pi Zero

I had a very old Pi zero that I bought the day they came out. I loaded a copy of the latest operating system and when I typed i2cdetect -y 1  I got a blank grid (so the software worked) even though devices were attached. I put the same SD card in a newer Pi zero and it worked fine. Checking on the internet others seem to have had the same problem.

 

6 Responses to Raspberry PI Noughts and Crosses / Tic Tac Toe

  1. Viktor says:

    Hi,
    in the SolusChess project the problem with the “ghost key” effect was described. That is why one should use a signal diode for each reed switch. Don’t you have such an effect or is there a reason you didn’t used those signal diodes?

    • Max says:

      Viktor, the SolkusProject uses the Reed switches arranged in an 8×8 matrix, so the “ghosting” problem can occur because other chess pieces on the board close switches and provide other routes to each switch. The Diode stops this. In my design each Reed switch is wired directly to a port on the MCP2307 so there are no alternative routes and no need for the diode.

  2. MS says:

    Hi,
    I am trying to build the chessboard using Raspberry pi and MCP23017 only. Can you please upload your code to github for chess project (just like you did for this project)?

    It will be helpful.

    • Max says:

      MS, My original working Chess board is Arduino based linking to the PI running the chess engine, and I have not finished the code for the full Pi version yet, I keep getting side tracked. So the only working code I can offer is the NOX code. Also have a look at Brian’s page and you will see he has a full download there.

      • Fritzmann says:

        Max, thank you for the project. I have nothing to do with this matter but chess! And I work at the evening and the weekend at my nox pi 😉 next step is to mount the lcd and run the tests! Never soldered anything before! Never used python, never had a Raspberry pi…. Thank you very much for this great project! You have to know that I am waiting for your update to raspberry pi only code/project. I was so sad when your site was down yesterday! One question: how long should the reed switch be for 5 cm square or should I just use bigger magnets? Cheers, Fritzmann

        • Max says:

          Hi, I use the same standard Reed switch for the chessboard as I do the NOX system. the key is to place them off centre as described. Pi only code still on the back-burner I am afraid.

Leave a Reply

Your email address will not be published. Required fields are marked *


*