other information

This site built by Wal using:

Kubuntu Valid CSS!



RaspbianLet's Encrypt

Download my GnuPG Public Key (C370766A) from here.

The MD5SUM is:

Automated Chess Project

10 Jan 2019


While browsing I came across this very cool project, and thought that's something I reckon I could tackle. So here goes!

The board

A tournament board has ~5.5cm squares and a ~9cm tall King which has a base diameter of about 75% of the squares. Since that would result in an overly large board, I will scale it to 4cm squares, with about a 3cm border for a 39cm2 board size. This scale requires a King ~6.5cm tall with a base ~3cm in diameter.

It turns out that the 'normal' chessmen you think of in a chess sets are known as Staunton pieces after an English player of the mid-1800's. So now you know.

The board will be made from 1m2 6mm thick MDF (1200 x 600 x 6mm MDF Handypanel, SKU 00294505 at Bunnings, $9.40). On that I'll put some veneer checkering and dressing, then some sort of clearcoat or varnish like Cabot's 300g Gloss Cabothane Oil Based Polyurethane Aerosol, $19.85..

The Tech

The rest of the hardware is as follows, although I'm certain the list will extend as I break stuff and figure out what I'm missing...

  • Raspberry Pi 3 - Model B+ $69.00
  • RPI-BBOARD Breadboard and GPIO Adapter Plate $21.85
  • RPI-PSU-3 Official 5.1V 2.5A Raspberry Pi DC Power Supply $17.25
  • RPI-CASE-CLR Clear Case with large GPIO slot $11.50
  • LED 3mm - Qty 80 $12
  • Adafruit TCA9548A I2C Multiplexer $13
  • Adafruit 16x8 LED Matrix Driver Backpack HT16K33 $10
  • Logic Level Converter Bi-Directional $5
  • ARDUINO UNO R3 MEGA328P (ATMEGA16U2) Board $26
  • LCD 2004 20*4 LCD 20X4 module + IIC/I2C Serial Interface Module $12.49
  • 140pcs Breadboard Single Core Jumper Wire with plastic box $8.99
  • Magnets Disc -12x1mm Neodymium Powerful Magnet (N35) craft Qty 70$35.96
  • Arduino Starter Kit UNO R3 mini Breadboard LED jumper wire button compatible $19.99
  • Momentary Round Push Button Switch 16mm 6A125V/AC 3A 250V - White, Qty several $4.76
  • Momentary Round Push Button Switch 16mm 6A125V/AC 3A 250V - Black Qty several$4.76
  • Breadboard / White - SYB-170 Mini Solderless 170 Tie-points 35*47*8.5mm $7.45
  • Breadboard / breadboard 400 holes - 8.5CM x 5.5CM $11.96
  • MCP23017 I2C Interface 16bit I/O Extension Module IIC to GIPO Converter Qty 5$31.96
  • 14mm * 2mm reed switch, Qty 70 $99

All up it looks like a $500 project. Hopefully I can make it work.

The plan

Stage 1: Learn and test. Play with Arduino and get 1 LED to illuminate when 1 magnet activates 1 reed switch. Make the LED blink. Add an MCP23017 to sense more than 1 reed switch. Add the HT16K33 LED Matrix Driver to drive more than 1 LED.

Stage 2: Proof of concept. Follow the guide at http://www.chess.fortherapy.co.uk and build a noughts and crosses game.

Stage 3: Chess prototype. Cut a square of MDF to 39cm2. Draw the checkering in pencil on both side, correctly aligned. Drill 3mm holes for the LEDs in the lower left corner of each square.

15 Jan 2019

Setting up

So my gear has mostly arrived.


I've emptied the office...


and filled it again.


Job one was to play – I can succesfully illuminate one LED light and make it blink without blowing anything up or releasing the magic smoke from a component. So a small step on the journey is taken...


I downloaded the latest raspbian operating system for the Pi 3 Model B+ that I received from the Pi Shop, and installed it to a spare 16GB micro SD card. Once I finished the headless wifi setup I installed i2c on the Pi:

sudo apt-get install i2c-tools

and then set up the pi

sudo raspi-config

to enable the I2C interface, the VNC interface, and expanded the filesystem.

I installed git

sudo apt-get install git

and might install some python code per the instructions here, but not just yet. I installed Real VNC Viewer on the Kubuntu laptop.

I installed the stockfish chess engine. I'm still looking for a UCI (Universal Chess Interface) repository, which I think I need.

I put together a test circuit and, to my complete amazement, it worked first time. So the proof of concept is sound – I can make an LED illuminate by bringing a magnet near a reed switch. From here, it's "just" a matter of increasing complexity. LOL.


17 Jan 2019


Rough cut the board to 38cm2, rolled on some MDF primer, and marked the squares and centers on the back. Next job is drilling the center recesses (~4mm deep) for the sensors and then marking and drilling straight-thru holes for the LEDs.


18 Jan 2019

Connecting the LED matrix

A little more research shows that I've bought pieces both for an Arduino+Pi board and for a Pi-only board. So too many bits, but that's better than not having enough. Hopefully the Max7219 and LEDs turn up today so I can crack into the project over the long weekend.

With the particular LEDs I have ordered I need to use a 24kΩ resistor on the 5V power lead into pin 18 for the correct maximum brightness. Reading around I think a lower value resistor should not cause problems, so I'll start with a 20kΩ resistor (or two 10kΩ resistors in series) and see what blows up happens. If the illumination is too dim I'll pull it back to 15k, maybe 10k.

There is no requirement for individual resistors on each LED when using the Max7219, so that's nice. The LEDs will be wired long leg (anode) (rows) to the SEG pins on the Max7219, and short leg (cathode) (columns) to the DIG pins per the datasheet. A 10µF (100 nano Farad, 100n) electrolytic capacitor and a 0.1µF (1 nano Farad, 1n) ceramic capacitor should be between V+ and GND as close to the chip as possible to assist in clean power delivery. The voltage rating of the capacitor should be around twice (or greater) the expected working voltage of the system.

Yes, the LEDs and MAX7129 are here. A couple of test drills using my imperial measurement drill bits shows that a 9/64" hole tapering to a 1/8" neck makes a nice mount for the set of LEDs I received today. The 9/64" hole should be around 3mm deep so the LED is flush with the board. They are bright; I fitted a 10kΩ resistor to bring it down to a sensible level. Some sort of collar on the drill might help me make each hole consistent.

Planning the board for the reed switches

Looks like a good diameter for a snug fit of the reed switches is 9/64". I must be careful bending the reed switch legs – already damaged one end of the glass on a switch. Still works as a test switch tho.

19 Jan 2019

Prepping the board

Broke out the power tools today. Router, jigsaw and cordless drill used to poke holes in the 6mm MDF board. The recesses for the reed switch were made consistent by using an offcut piece of wood cut to the correct depth and then fine–tuned by adjusting the drill bit positioning in the drill. Poke drill through offcut and your hole is the right depth. Nice.


Pushed the 3mm LEDs through the board until they are flush with the upper surface. I can't look at this picture without imagining 64 little LEDs have fallen into holes and their legs are sticking out.


Now for the tedium – soldering the matrix. 64 lengths of wire to be cut to length, ends stripped, and "tinned"; 128 little LED legs to be bent at 90° and tinned; 224 wire–ends to be soldered onto legs.


21 Jan 2019

The finished loom attached to the Arduino via the MAX7219 on a breadboard.


A good reference I found was this tutorial. And it worked! One LED was permanently on, but I tracked that to a short in my soldering of it and made an easy fix. So that's the LED setup complete, taking a few hours over the long weekend. It's quite satisfying to upload test routines I have written to the Arduino and watch the LEDs light up in patterns of my design.

Code created in the Arduino Integrated Development Environment on my Linux Kubuntu laptop, compiled, and uploaded to the Arduino over USB:

#include "LedControl.h" // need the library
LedControl lc=LedControl(12,11,10,1); //
// pin 12 is connected to the MAX7219 pin 1
// pin 11 is connected to the CLK pin 13
// pin 10 is connected to LOAD pin 12
// 1 as we are only using 1 MAX7219
void setup()
// the zero refers to the MAX7219 number, it is zero for 1 chip
lc.shutdown(0,false);// turn off power saving, enables display
lc.setIntensity(0,1);// sets brightness (0~15 possible values)
lc.clearDisplay(0);// clear screen
void loop()
for (int row=0; row<8; row++)
for (int col=0; col<8; col++)
lc.setLed(0,col,row,true); // turns on LED at col, row
for (int row=0; row<8; row++)
for (int col=0; col<8; col++)
lc.setLed(0,col,row,false); // turns off LED at col, row

Next stop, reed switch installation, which I think will be an even bigger job...

***NOTE*** from future Wal (25 Jan) What follows over the next two days is a RABBIT HOLE I fell down. I was trying to use the Raspberry Pi as the reed switch sensor hub, when of course what I should have been doing is using the Arduino as the sensor hub and sending that information to the Pi to process. Nonetheless, I learned plenty and had some fun figuring stuff out. Live and learn. Must keep eye on big picture.

... and yes, the reed switch setup is a bigger job. I've managed to get the MCP20317 to interface with the Raspberry Pi to control an LED using the following code and wiring setup.

sudo i2cdetect -y 1
sudo i2cset -y 1 0x20 0x00 0x80
sudo i2cset -y 1 0x20 0x14 0x01
sudo i2cset -y 1 0x20 0x14 0x00

Line 1 detects the MCP20317. Line 2 sets pins 1-7 to out and 8 to in. Line 3 turns the LED on. Line 4 turns the LED off.

23 Jan 2019

To use the "B" rail change the code as follows:

sudo i2cset -y 1 0x20 0x01 0x80
sudo i2cset -y 1 0x20 0x15 0x01
sudo i2cset -y 1 0x20 0x15 0x00

To set all the "A" rail pins to output:

sudo i2cset -y 1 0x20 0x00 0x00

To set all the "A" rail pins to input:

sudo i2cset -y 1 0x20 0x00 0xff

To set all the "B" rail pins to input:

sudo i2cset -y 1 0x20 0x01 0xff

24 Jan 2019

So i2cset is fine for elementary testing, but I need way more control than that appears to give me. Adafruit provides Raspberry Pi libraries and tools to use the MCP23017. To setup the Raspberry Pi to run Adafruit CircuitPython, and use Python 3.x by default:

sudo apt-get install python3-pip
vim ~/.bashrc
alias python='/usr/bin/python3.5'
. ~/.bashrc
sudo reboot
sudo pip3 install --upgrade setuptools
pip3 install RPI.GPIO
pip3 install adafruit-blinka
sudo pip3 install adafruit-circuitpython-mcp230xx
python blinkatest.py

Once all that's set up, use this python script to test a breadboard with a button and two LEDs. Use a ~200Ω resistor to the ground rail to keep loads within limits. Connect one LED to pin 0 and it should flash. Connect the button to pin 7 and the console should report when it is pushed. Connect the other LED to pin 3 and it should illuminate when the button is pushed.

Of course this test circuit does not include the Arduino and its MAX2719 matrix, however I found it a useful exercise in understanding the software and the circuitry wiring. After I got the circuit to work, I couldn't resist a mockup using the chessboard itself – below are a couple of pictures showing a chess piece with a magnet on the bottom proximity activating a reed switch which is vertically mounted under the board to get the Raspberry Pi to turn on an LED using the python script above. Most satisfying. Next step is to replace the LED on the circuit board with the LEDs in the chessboard matrix.


25 Jan 2019

***NOTE*** It's at this time I climbed out of the rabbit hole. Posts from here on should be back on track, using the Arduino as the switch detector and LED controller.

25 Jan 2019

Right, now for the Arduino switch setup. The circuitry should be simple: I'll connect two wires of the same colour to each reed switch, and run two rows of squares (16 switches) to each of four MCP23017s. The MCP23017s will be attached to the Arduino, not sure how right now, possibly through the Adafruit TCA9548A I2C Multiplexer I have on order with nicegear.nz, but my fist try will be to daisy–chain them on a 3.3v rail from the Arduino and link their SDA/SCL pins in series into pins 4 and 5 on the Arduino.

I have learned that the MCP23017s have solder-jumpers on the board. Soldering over the open space on the jumper, or alternatively removing the solder to create a gap, switches the jumper. Each board has three jumpers, labelled A0, A1, A2. The complete address of each board is actually 1 0 0 (A2) (A1) (A0). So for example a closed (A1) jumper gives an address of 1 0 0 0 1 0, which is 22 in hexadecimal according to this site. So if I want the boards to appear in the sequence 21-22-23-24 I'll need to solder the jumpers accordingly.

Somewhere in this setup I need to fit an LCD display. Looks like they should run at 5V, so that's good as I have a free 5V pin on the Arduino at the moment.

26 Jan 2019

Oops. It's finally happened, and it's not a surprise – while hooking up a component to the GPIO pins of the Pi I got the wiring wrong and now the Pi pins are munted. A bit of reading says there is a fuse which might reset after a day or so, but I'm not optimistic. The Pi can still serve as the brains of the project using a USB connection to the Arduino, so it's not a total loss.

I've hooked up the LCD screen to the Arduino at the same time as an MCP23017, and it all appears to work as expected. The display is at address 0x27 according to this sketch, so that deconflicts with the MCP23017 which is native at 0x20. I cannot figure out how to change that address on the MCP23017 with the type of chip-on-board I have. I think I'll need the multiplexer that is on order, but I can certainly proceed a few more steps before the absence of that part becomes an issue.

To get the display working I installed the LiquidCrystal_I2C library by Frank de Brabander from the IDE interface, and selected the example sketch Hello World as a start point. Pretty easy (after I figured out I needed to adjust the LCD brightness using the dial on the back!)


The Pi recovered! There's something called a Polymeric PTC (Positive Temperature Coefficient) fuse in it, which is basically witchcraft, that stops buffoons from cooking the Pi and also, after a while, return to normal. Clever.

I've soldered half of the reed switch loom, which is one of the last hardware jobs; it's nearly time to flip it onto its feet and start getting down to the business of programming the thing to make it work. The other bits I've yet to attach include a couple of LEDs indicating player or machine in play, the information LCD, a power switch, and some control buttons. I'm considering a 9V backup battery to allow temporary power disconnections while retaining the game state, but that probably won't make the final cut.


28 Jan 2019

More hardware tinkering done over the weekend. The wiring reached PMU (Point of Maximum Untidiness) today, but I made an internal tray to mount the components on with holes drilled to let the underlying LED and reed switch loom to route through to make it less unweildy. I also added a blob of clear flexible silicon sealant to each reed switch to keep it in place.

Some of these additions are making this 'prototype' board more and more difficult to disassemble... I am starting to think that this might be the 'keeper'.

The finished reed switch loom routed through the tray (left of picture), LED loom top of picture.


The tray mounted in the board, components sitting on top (underneath when the board is upright).


03 Feb 2019

All the wiring is complete, and the board is ready to program. I've enabled VNC on the Pi using raspi-config, and can access the Pi desktop remotely from the laptop using Real VNC viewer. I'm now programming the Arduino by using the Raspberry Pi remote desktop over wi–fi, so the system is standalone. I expect it will gain self–awareness shortly. jokes

12 Feb 2019

Not very much to show for the last few days' work, but under the hood the engine is starting to come together. I couldn't get interrupt reporting to work with any more than three MCP23017s connected, so have resorted to simple polling of the pins and it seems to work fine. At the moment I have got the Arduino scanning the board, storing positions in an array in memory, then scanning again and reporting if any positions have changed. A change triggers an LED to flash and a message on the display screen for "piece lifted" or "piece placed". Next steps are to have that message reported to the Pi in a compatible format. I have got a chess game running on my PC using a text interface pretty easily, so hopefully the same process will work on the Pi. To-do list:

  • Affix components to mounting tray
  • Install front and back base plates
  • Mount LCD display and buttons to front plate
  • Mount power port to right side plate
  • Paint checkers
  • Veneer edges and sides
  • Win

14 Feb 2019

I have got all the parts talking to one another now, so although this is not the end, it is, perhaps, the end of the middle part of the start of the finish. (Could that have been said a little better?... maybe.) The Arduino senses the pieces being moved, flashes lights on the board in acknowledgment, sends the move to the Pi, and the Pi sends a message to the display screen through the Arduino. The missing link is now integrating those steps with the Chess program hosted on the Pi. I've had to use Python 2.7 instead of Python 3.x because that's what the Chessboard program is written in. More baby steps to come this weekend.

18 Feb 2019

Lots of paint and woodwork this weekend, so now the box is pretty much done. I lost a day because in my first pass on the checkerboard paint the masking tape was not secured firmly enough, meaning the paint bled through so I had to sand it back and apply another coat. Much tidier now, but not perfect. Next time I will use spraypaint. Tomorrow's jobs are to glue the wood veneer borders to the top, wire up the control buttons, and secure the front panel to the board. With that done I should be able to go back to the software programming. Nearing the end now!

Chess Chess

20 Feb 2019

Hardware effectively complete. Hooray! Yay me, I'm quite proud of what I've made, even in prototype first-attempt form. All that's left to do is glue some veneer to the sides and it really is finished.

Today's job is to try to get the software under the hood working. The thing with that is, it will be very binary – either it will work or it will fail. I have no good idea of how to put the pieces together, so it will be trial and error. This could work quickly, or take for ever. Only one way to find out…

22 Feb 2019

At the risk of coming across all George W. Bush, I'm declaring Mission Accomplished in the chess project. I got the software running on the board, and played a number of games against it, being soundly beaten in all of them. I'm not sure if that makes me dumb because I got beat by a machine, or smart because I built the machine.

Anyway, there are only a few things left to do (high on the list is figure out how to reduce the difficulty level!). Right now I'm waiting for the glue to set on the veneer. After that I shall get the lights on the board to flash the computer move; at the moment they only work for the player move, so I'm using the LCD display to show the computer move. Other tidy-ups are cleaning up the new game/reset logic, and getting the engine to recognise the difficulty setting requested by the board. Once that's done, the prototype (or most likely one-of-a-kind never to be repeated) is good to go. So that's the second summer project done, if you count my arcade machine which was a pretty similar kind of thing in some ways, although the arcade machine was a lot simpler.

And no, I have no idea what's next... but I've been contemplating motion-activated hallway LED strip lighting, a homebuilt security camera setup, and a weather station. Also chess lessons.

24 Feb 2019

The software I've been working with, courtesy of chess.fortherapy.co.uk, isn't quite right for me, so I'm going to write up some code here to try and personalize/streamline how it works. Of interest only to the very nerdy.

02 Mar 2019

Alright, software is now functional. The skill level is working (although hard coded for the moment); the error I made was specifying a movetime for each move, which overrides the skill level setting. I have implemented a "new game" function, so now if the player makes an error the game can be restarted using the front panel buttons. I'm happy with where the board is now, the rest of the things I'd like to achieve I can work on in slow time. From concept to working model, the project took me just over two months. To-do list:

  • Implement adjustable difficulty level at startup complete
  • Add boot wait information complete
  • Update software to work with Python3 complete
  • Add turn recording and undo
  • Include 3rd party software to validate moves, provide gameplay info

Youtube footage of the project is here...