File Exchange

## Rubik's Cube® Solver using an Arduino and MATLAB

version 1.2.0.1 (12.3 MB) by MathWorks MATLAB Hardware Team

### MathWorks MATLAB Hardware Team (view profile)

Solve Rubik’s Cube® using image processing and Arduino-controlled servo motors

Updated 01 Sep 2016

In this entry, we build a robot that solves a Rubik’s Cube® using MATLAB, a SparkFun RedBoard (Arduino Uno clone), two servo motors, and a webcam. The submission contains all of the code used to control the robot, a detailed article describing how the robot was built and programmed, and several videos of the final result. Download this submission if you’re interested in learning about our project. Or use it as a starting point for building and controlling your own Rubik’s Cube solving robot.
In addition to hardware components, this project requires MATLAB and Image Processing Toolbox. It also uses MATLAB Support Package for Arduino® Hardware, which can be downloaded for free here: http://www.mathworks.com/matlabcentral/fileexchange/47522-matlab-support-package-for-arduino%C2%AE-hardware
The initial setup of the project involved building the robot from readily available materials. In addition to the Arduino device, webcam, and servos, we used LEGO bricks, an acrylic sheet, and hard polystyrene foam. The robot was designed with two main parts. A holder holds the cube from below in an open-topped box and can rotate the cube in the horizontal plane. A gripper arm can be lowered over the cube from above to hold two layers still while the holder rotates one face from below. The gripper arm can also be pushed forward to flip the cube to a different side.
With the entire robot assembled and calibrated, solving the cube involves two steps:
1. Read the initial state of the scrambled cube.

MATLAB functions were written to control the servo motors on the Arduino so they could rotate the entire cube in the holder and flip it to a different side. These functions are used to rotate the cube so each of the 6 faces are facing up and capture an image of the face with a webcam. Using Image Processing Toolbox, the color of each facelet is determined. A custom MATLAB UI is used to validate the results and correct any errors.

2. Solve the cube.

The algorithm used for solving the cube is the Thistlethwaite 45 (T45) algorithm. It was used because a MATLAB implementation of the algorithm was already available on the MATLAB Central File Exchange as part of the submission here: http://www.mathworks.com/matlabcentral/fileexchange/31672-rubik-s-cube-simulator-and-solver

The output of the T45 algorithm determines which faces should be turned. This is then converted to a sequence of moves on the robot that could rotate the cube, flip it to a different side, and turn a single face. Once all the steps are complete, the cube is solved.

 1 Sep 2016 1.2.0.1 Updated license 10 Feb 2015 1.2.0.0 Added Trademark recognition (®) to first mention of Rubik's Cube® in each section, both in the File Exchange Title/Summary/Description and in the text of the html article which is part of the submission.
##### MATLAB Release Compatibility
Created with R2014a
Compatible with any release
##### Platform Compatibility
Windows macOS Linux