Thursday, September 11, 2014

How to Build an Arduino Powered Chess Playing Robot [Full Tutorial]


Judging by the sheer number of chess related Instructables, I think it's safe to say the community enjoys the game.  It can be difficult, however, to find someone who plays on the same level you do.  To solve this dilemma, and to increase my playing skills, I built this arduino powered chess playing robot.

The board works like any other xy table, with a few key differences.  First, the x axis has an extra servo attached to it, which raises and lowers a magnet.  The magnet is attracted to pieces on the chess board above, allowing them to move.  Second, embedded in the board are 64 magnetically activated reed switches, allowing the arduino to know the location of each piece.

What I love about this project is its adaptability.  If you decide you're done with it as a chess board, it can instantly convert into a CNC mill by modifying a few pieces.  I'll talk more about this possibility at the end.

All in all, though I learned a lot from this project and had fun building it, the board was not as successful as I had hoped.  The magnets were way too powerful, so extra pieces would almost always drawn in when they shouldn't have. However, with a few thoughtful changes I think this could have been a better, more functional project.  Until I build another, better board, though, I think this Instructable still serves as a pretty good guide to make your own chess robot.

Step 1: Parts and Materials

You may have many of the parts for this project already, but if you don't, the whole list costs ≈ $350, depending on where you get your parts from.  Many, many of them can be salvaged, so look to recycle before you buy!
  • 1 Arduino Uno or Diecimila
We'll be using this arduino to drive our stepper motors and servos.  You can pick these up just about anywhere online.  I got mine from Adafruit. $30
  • 1 Arduino Mega
This is the most expensive item in the project.  It'll be dealing with the inputs from each chess square to let the computer know where you've moved.  We're using the mega here due to its speed and number of inputs.  Adafruit $65
  • 1 Mux Shield
The mux shield (short for multiplexer) gives us even more inputs for our arduino mega.  We'll need 64 inputs in total, one for each square.  Sparkfun $25
  • Motor Shield
The motor shield will be controlling our stepper motors and servo.  You'll need to solder it together.  Adafruit $19.50
  • 1 Large chess board with pieces
This one is a little more self explanatory.  We want a large chess board here because the pieces need to be able to move in between each other with disrupting others.  Make sure you measure the diameter of the bottoms of the pieces.  We'll need that in a moment.  I'm not sure where mine is from, but you can pick them up from a flea market for a bargain.  The playable area of my board is 24".
  • 64 NO Reed Switches
Reed switches are magnetically activated switches.  They'll help us find the location of moved pieces.  NO stands for normally open, that is, the circuit is disconnected   Digikey ≈$30
  • 16 10K 1/4 Watt Resistors
These are the pull up resistors for the built in digital pins.  The mux shield, luckily, has integrated pull downs, so we don't need to worry about those. Digikey ≈ $2
  • Roughly 90 feet of 30AWG Wire

This is the hookup wire for all of our sensors. Radioshack ≈ $16
  • Neodymium Magnets to fit your pieces
This is where the measurements from the bottoms of your chess pieces come in handy.  You'll need disc magnets to fit underneath each piece.  For proper strength, they should be about 1/8" think.  A great source for these is K&J Magnetics.  ≈ $55
  • 1 Large Neodymium Magnet
This magnet will be attached to the XY table underneath the board, to move each piece around.  K&J Magnetics $19  Note: This was Waaaay too powerful.  It would draw in pieces it shouldn't have.  You'd be better off going with some smaller ceramic magnets, like you'd find at Staples or another office supply store.
  • 2 Pairs of 24" Drawer Bearings
The size of your bearings will depend on the playable area of your chess board.  These allow for the stepper motors to move back and forth underneath the board. Amazon ≈ $30
  • 2 Stepper Motors
Stepper motors can move in very precise increments.  In the late 90s they were in just about every piece of tech you could find.  The best place to get these are in old dot-matrix printers.  You can them at the flea market for next to nothing!
  • 2 Vex Rack and Gear Sets
The rack gears allow the stepper motors to travel on the drawer bearings.  See the Step 4 for a more detailed explanation.  Vex Store $40
  • 1 Standard Hobby Servo
This servo will be raising and lowering the powerful magnet below the board. You can find them at a hobby shop for ≈ $10, or Amazon ≈ $12
  • 1 2' x 2' Perf Board
The perf board is super thin and will be the mounting surface for all of our reed switches.  The price will vary greatly on this one, but I got mine from Home Depot for ≈ $5
  • 1 2' x 2' x 1/2" MDF Board
Similar to the perf board, I got this from Home Depot for  ≈ $5
  • Various lengths of scrap 1"x2" wood
This wood forms the bridge between the X-Axis drawer bearings.  Go behind any hardware store and you'll see dumpsters full of this stuff for totally free!
  • 5 Minute Epoxy
This stuff is a godsend.  It's used for just about everything in this project, from mounting motors to attaching the rack gears.  I'm in love -- and  I picked mine up from Radio Shack for $3
  • 1 Wood Saw
You probably already have this one, but if you don't, I picked mine up at Ace Hardware for $10 a couple of years ago.


Step 2: Design and Code Explanation

That parts list is a bit scary if you're not sure what everything is going to do, so here's how many of the pieces will be used.

You can see in the images below that each stepper motor can move freely about its axis thanks to the drawer bearings.  On the Y Axis, each rail is connected with the wooden structure, so that the X Axis may sit atop it. Also on the X Axis is the servo that raises and lowers the powerful magnet, so that it may position itself before moving pieces.

Feel free to download the sketchup file and mess around if you're not sure of anything.

Another interesting element of this design is how to code talks with the arduino and motors.  We need to address each square as a set of coordinates so that we may find slope and distance, however the traditional method of labeling squares A1, A2, etc. doesn't work particularly well in code.  Standard (x,y) coordinates are much friendlier.  Those coordinates, however, need to be in the form of a single number.  What I ended up doing is assigning each square to a number, as you can see in image #3.  Those numbers don't really work as coordinates on an 8x8 chess board, however, because we use a base 10 number system.

To solve that issue, we take the base 10 number of each square and convert it to base 8 using the modulus operator in C.  27, for example, is 33 in base 8, with the first digit being the x coordinate and the second the y.  If you count over three squares and up three squares, voila! You end up on square 27.  This converted coordinate system ends up looking like image #4.


XY.skp347 KB

Step 3: Mounting the Drawer Bearings (Y Axis)

The drawer bearings are what allow the axes to move in their respective direction.  The mounting instructions may vary slightly depending on the brand, but usually it's as simple as driving a couple of screws.

The only reason I've made this its own step is that aligning the bearings perfectly is key.  Should you fail to do this, and they both point slightly outwards, they'll stop at some arbitrary point and refuse to move once you connect them.  Save yourself a lot of trouble and use something you know is square as a reference for alignment.  The corner of a book is perfect.

Step 4: Building the Motor Mount (Y Axis)

The stepper motors we'll be using have fantastic torque, but are circular.  This means mounting them to our bearings later on will be nearly impossible, unless we build a square mount.  To build one, find a hole saw with a similar diameter to your motor.  You'll want to use a drill press rather than a portable drill for this, so I borrowed my school's.

Once you've cut the hole, slice the circle in half to get two mounts.  This chipped the tips of my semicircle, so I used some 220 grit sandpaper to clean up the edge.

My steppers came with mounting screw holes, which line up well with the wooden frame.  I used the smallest screws I could find.  Mine fit so well that it wasn't necessary, but you might consider adding a bit of epoxy to strengthen the bond.

Step 5: Installing the Rack Gears (Y Axis)

The rack gears are what allow the motor to latch onto a surface to pull itself along.  Your physics teacher probably defined them as a way to convert rotational energy to linear.

Again, we use the epoxy to attach the gears onto the MDF.  In addition to heavily applying epoxy to the board itself, make sure some is spread on the side of the drawer bearing, that way there is stability in two dimensions.  Do your best to prevent epoxy from getting in places it shouldn't be -- you may gum up your motor.

It works out that the rack gears extend a little bit off of each end.  This is a good thing -- it enables the gear to travel the full length of the board without running off.  The stepper motor will be offset just enough that if the gears only covered the board's length the whole motor assembly would get stuck at one end.

Also install the circular gears onto your motor at this time.  Mine had a set-screw, but you may wish to use some JB weld to hold your gear in place.  If you go that route, the joint needs to fully cure before you try to use it, or you risk the gear popping off!

Step 6: Wiring and Mounting the Motor (Y Axis)

The leads that come attached to the stepper motors are very short.  Because the Arduino is mounted off the board, the wires need to be at least the length of one side.  That made mine about 2' 5" long.  Heat shrink tubing is your friend here -- we're using enough power that it might arc if you're not careful.

If your stepper motor has 5 wires, you're all set.  If there are 6, however, it means you have to connect your center taps.  Jason Babcock has a great tutorial on reverse-engineering your motors.  In my case, however, the wires were the same color.

After extending the wires, the center taps go into the center of one of your motor hubs.  The wires from one coil go to one terminal on the motor shield, and from the other coil to the other terminal.  At this time we also hook up our 24v 1A power supply to the motor shield.  If you get the polarity wrong on this, your motor shield is toast.

After trimming the motor mounting block to about 4 inches, it's time to attach it to our bearings.  Mix up the epoxy, and liberally apply it to the area of the bearing the block will touch.

Also, if you have any pets, be sure to animal-proof the room you're working in.  Cats seem to have an affinity for knocking over things that are drying.

Step 7: Mounting the Crossbars (X Axis)

Atop the Y Axis sits a cross-bar which will hold the X Axis.  To do this, we mount another block on the opposite Y Axis bearing.  This will across from the motor mounting block.

Then we cut two 2' lengths of 1"x2" wood and mount them to the blocks with wood screws.  Make sure these screws are in tight, or the bearings might not move at the same time.  You might consider adding some wood-glue to lock them more tightly in place.  While these cross bars need to be large enough to support our X Axis bearing, we want to use the least amount possible to avoid unnecessary weight.  If the entire X assembly weighs too much, our Y Axis stepper motor won't have enough torque to move efficiently, or, if it's really heavy, at all.

Test that your bearings move evenly with each other.  The crossbars should be as close to perpendicular with the bearings as possible.

Step 8: Mounting the Drawer Bearing and Rack Gears (X Axis)

We are going to mount this drawer bearing in the same way we did on the Y Axis.  Using the included mounting screws, attach it to the crossbars, making sure to leave room on one side for the rack gears.

This time, however, we have some options for mounting the rack gears.  We can choose which side to mount them on.  This is going to vary depending on how your motors are placed on their blocks.  Once you've decided where to place them, though, they are glued in exactly the same way as on the Y Axis.  Extend the rack gears slightly off of each end to make sure we have full travel on our steppers.

Again, make sure to try and keep any glue out of the sliding mechanism.  If you do allow some glue to enter the mechanism, all is not lost.  Fortunately for us, drawer bearings come in pairs, so we'll have one left over anyway.  Mount it to the same screw holes and you're set!  Just don't do it again! :)

Step 9: Attaching the Magnet to the Servo (X Axis)

The chess board needs to be able to move into position and grab a piece.  To accomplish this, we achieve a pseudo Z Axis by mounting a magnet to a servo on top of the X Axis.  Use epoxy to attach your large magnet to a small piece of wood (Jenga blocks work wonderfully).  Once that has dried, attach the wooden block to your servo.

If you're concerned about the torque of your servo, you might consider adding a counterweight to make lifting the magnet less difficult.

At this time you should also extend the leads on your servo motor.  Do this the same way you did for your stepper, going one wire at a time and covering it with heat-shrink tubing or electrical tape.

Epoxy bonds best when there is a significant weight holding the two bonding surfaces together.  Keep in mind that while 5 minute epoxy dries in 5 minutes, it may not cure for several hours.

Step 10: Wiring and Mounting the Motor (X Axis)

We mount and wire our X Axis motor as we did earlier, however we also need to attach our servo assembly.  We need to mount the servo as closely to our motor as we can get without interfering with the gearing.  This way we are guaranteed to have maximum coverage underneath the board.

Using a piece of wire or cotton swab, carefully apply epoxy to the block where you've decided to attach your servo.  If you aren't careful, some epoxy may end up on your stepper motor, rendering it useless.  It's a real pain to clear mixed epoxy out of motors, so it's worth the extra time it will take.  Firmly hold your servo in place for several seconds, and leave it clamped to dry.

Once your servo is mounted, we can attach the motor block to our bearings.  Being cautious as to not get glue in the sliding mechanism and, like earlier, apply the epoxy to the exposed metal the block will touch.

After that is dry, wire the motor like you did before, extending each lead.  This time, however, we'll connect it to the other motor terminal.  Congratulations, the XY Table is done!


Step 11: Wiring the Sensors

Each chess piece has a magnet embedded in its bottom, which makes detecting location really simple.  Underneath each square is a magnetic reed switch hooked up to our Arduino mega.  When a change is detected, the Arduino spits out the coordinates.

Pull up resistors make sure we don't get false readings from our sensors.  48 of the 64 switches won't need pull up resistors, because the multiplexer has them built in.  Unfortunately, we still have to solder 64 sensors.  To make this go a lot faster, tin your wires before you try to solder them to the switches.  Basically, just add solder to the wire alone before soldering with it. Label each switch with tape as it is completed to avoid a wiring nightmare later!

Hook up each switch to the Arduino as you go along.  The multiplexer has a built in ground next to each input, which is really convenient.

To wire the pullup resistors to the built in pins, connect one resistor end to 5V and one to the pin you're using.  Then, skipping the resistor, connect one end of your switch to ground and the other directly to your pin.

Find a comfortable chair, because this is going to take a while!

Step 12: Place the Magnets

Each chess piece has a magnet embedded in its bottom to trigger the reed switches.  The only thing that's important here is that every piece on one side of the board has one polarity (like North), and every piece on the other side has the opposite (like south).  If you'd only like the computer to play one color, make sure it is the opposite of the magnet attached to the servo, so they will attract each other.

Other than that, though, this only takes a few minutes.

Step 13: Code, Final Assembly + Reflection

The code for this project is surprisingly simple.  It takes the reading from the chess board out of terminal, plugs it into a chess algorithm already built into Mac OS X (look inside the Chess.app bundle), and spits out the coordinates back into the Arduino window with some fancy applescript.  The algorithm is a fantastic open source project called Sjeng, meaning this will work cross platform as well.

The final assembly for this project is fairly simple.  Find something to hold up your sensor grid, and lay that down.  Next, place your chess board and pieces on top.  You're done!  Run the software and give it a whirl.

If you ever get bored of chess, it's really easy to make this into a CNC project.  Replace that magnet with an X-Acto knife, flip the entire assembly upside down, and you have a stencil cutter!  Replace that X-Acto knife with a pencil and you have a draw-bot.  Modify the servo to activate a dremel and you have a CNC machine!  The possibilities are, please excuse the cliché, endless!

Reflection

In retrospect, there are changes I would make to this project if I revisit it. The magnets were waaaaaay too powerful, and would always pieces in that they shouldn't have.  When all the pieces snap together, this would render a game unplayable. The mounting of the gears should be greatly improved;  a few times the gears would pop off due to the heating of the stepper motors.  Proper mounting with screws would fix this.  Second, the board gets (fairly severely) out of calibration after a few moves, presumably due to skipped steps, and must be manually moved back to the origin to avoid misalignment.  Some potentiometers allowing the board to know its absolute position would fix this.  The rails, despite my best efforts, were misaligned, making the last couple rows of squares impossible for the stepper motors, causing them to skip steps and click loudly (bigger steppers would be better).  Sometimes the motors would stop altogether and wouldn't be able to make it in those rear squares.  Aligning the rails better would fix this. Often what would happen was that when the large magnet flipped over, a whole bunch of pieces would get drawn towards it (and it's a real pain to place all the magnets back inside the pieces if you haven't attached them). Ceramic magnets like the ones people stick to whiteboards might work better.  Finally, the code could definitely be streamlined further; I completed this project when I was very new to C and Arduino.

Thoughts on the Epilog Challenge

It's kind of a funny feeling when as a maker you have something that can, well, build other things. An epilog laser would help me in some way with literally everything I make.  With this laser I would, on a regular basis, cut solder stencils for surface mount work, make more elegant housings for my projects, and engrave logos onto personal laptops and cell phones.  As a student, it would provide me with a small extra source of income from engraving the gadgets of others.  Rapid prototyping circuit boards would allow me to take my work to the next level, rather than having to spend several hours etching a PCB just to find out one trace is routed incorrectly.  This laser, combined with help from the awesome community here on Instructables, would help me so much in my pursuit of a career in engineering, my ultimate goal.

I'll leave it up to the merit of my Instructable, though, to decide whether I deserve this laser, not the list of sappy thing's I'd do with it. :D Instructables and Epilog have done something great here, and the projects created by this fantastic contest will set a standard of quality for years to come.


I hope you've enjoyed reading about this project as much as I enjoyed making it.  Make sure to leave a comment if this has inspired you to build anything, I'd love to see what you've made!
Chess.zip10 KB

10 comments:

  1. This is what i have been looking for, i want to build it into my coffee table and would be awesome to have it. How much did it all and all cost you? I will probably make it a few months project... Since i need to make time from work to do it.

    ReplyDelete
    Replies
    1. Never mind saw it cost you about $350, let me see if i can build this! Awesome!

      Delete
  2. I've created mine code in C for several months now (Since October 2016). Mind if you could share yours? 'Cause I'm dead meat now, if not my supervisor intended not to extend semester any further but decided to drop out :(

    my email : aqilahzainuddin92@gmail.com

    ReplyDelete
  3. I've created mine code in C for several months now (Since October 2016). Mind if you could share yours? 'Cause I'm dead meat now, if not my supervisor intended not to extend semester any further but decided to drop out :(

    my email : aqilahzainuddin92@gmail.com

    ReplyDelete
  4. hi, this is an awesome project!!, i'm curious as to how the reed switch circuit could avoid being triggered by the magnetic arm, care to elaborate?

    ReplyDelete
  5. Can u guide me to do this chess robot.

    ReplyDelete
  6. This comment has been removed by the author.

    ReplyDelete
  7. Hi mate,
    When I load the code on the arduino board the console did not respond to any position...Eg I put the coordenates 27 or 33 and the Arduino console did not respond.
    Hope you can help me.

    ReplyDelete
  8. I want to build this project as for the submission of final project. So, kindly help me in project as i am feeling uneasy in part of the coding. i will be thankful for your concern in my matter.

    ReplyDelete
  9. Good morning,
    would be possible to receive the code, please? thank you very much ( matteo_lcn@yahoo.it )

    ReplyDelete