Robotic Arm Plays Tic Tac Toe





Introduction: Robotic Arm Plays Tic Tac Toe

About: My name is José Luis Bejarano Vásquez.Tecnólogo en electrónica, estudiante de ing electrónica, desarrollador de software. Aficionado a la robótica. Blogger.

A few months ago I bought a robotic arm of 4dof made in acrylic and this week i decided to program it to play the ancient and famous game Tic Tac Toc against me, and also for participate in the robotic contest. So, if you like this project please voted for it. ;)


  • (1) Robotic arm with gripper.
  • (1) Web cam.
  • (1) Arduino One.
  • (1) Board of 3 x 3 squares.
  • Red and white pieces.

The web cam is for the image capture of the board. The image is analyzed by a visual basic program and using the minimax algorihm decides where the computer must place it´s piece. The program sends commands to the arduino one and this moves the robotic arm to the choosen position by the algorithm.

Step 1: The Robotic Arm

I bought the robotic arm here but you can build your own robot.

This robotic arm contains of 3 servos S3003, 1 for the base, 1 for the shoulder and 1 for the wrist. Also uses a servo sg90 for the gripper.

It is controlled by the Arduino One which is commanded by a visual basic program.

Step 2: The Diagram

As mentioned in the first step, 4 servos are connected to the pins 5,6,9 and 10 of the arduino one.

Step 3: The Image Capture

The web cam captures the image of the board and through a procedure in the visual basic program (you have to install the web cam library) , the image is divided into nine squares and each square is traveled for determining if contains red pixels. If so, the pixel is marked with red and later if the amount of points red of the square is greater than 60, then there is a red piece in that square and is interpreted with a "X", which is the movement of the human player.

Step 4: The Logic of the Game


The visual basic program determines the position of the computer thanks to an algorithm called miniMax, which is an algorithm used in games of two players and helps to decide the best movement for the computer turn.

Clic here for downloading the full code in visual basic of the project.

Note: the source code of the minimax algorithm used in the program was written by Ralph Main, you can see the article here.

Step 5: The Movement of the Arm

When the Artificial Intelligence of the minimax algorithm decides in which position the piece must be placed, it sends a string with the number of that position to the arduino through the serial port. The arduino receives the data and according to the position calls to the function that contains the movements in angles for moving the robotic arm.

Example of code for moving the arm to the second square:

if (dato=='2')





void segundaPosicion()

//Move the base


{ base.write(i);




//Moves the wrist to 90







//Moves the shoulder to 160


{ hombro.write(i);




//Moves the wrist







//Open the gripper




delay(20); }


//Up the shoulder







posicionInicial(); //Moves to its initial position


Download the full code in arduino here

Thanks for watching, comments, sugestions, please let me know it.



    • Clocks Contest

      Clocks Contest
    • Make it Move Contest

      Make it Move Contest
    • Planter Challenge

      Planter Challenge

    We have a be nice policy.
    Please be positive and constructive.




    The game can detects any color not just red pixels i try another color and the game work will how can make the game only work for red color.

    Sorry for my english.

    Nicely done.... good use of VB like it.

    Very cool Instructable! I love this! Thanks for posting!

    1 reply

    Very nice instructable!

    I noticed in the video that the arm only won because the human played made poor choices (they failed to play the forced winning move on their third turn and failed to block on their fourth turn). I was unable to download the source code, so I only can assume that the game solver is rather easy to beat? This is not bad, because no one wants to play a machine that is unbeatable. I could see where children might particularly find it fun to play against.

    A very nice fusion of mechanics,computer vision, and electronic communication in a fun package!

    2 replies

    The source code of the minimax algoritm used in the program was writtten by Ralph Main, you can see the code here for analyzing.

    Hi, thanks for your comment. In the video i placed the pieces in those posiitons only for demostrating the robot could win. But you can see that the artificial intelligence of the minimax algorithm is very good, because wont let me win when i have the chance of do it, but it is not unbeatable. Sorry for my english.