Introduction: Rubik's Cube Solver


So here's a project I've been working on for a while...

This robot that can solve a Rubik's cube using Arduino.

I learned how to solve a Rubiks cube last year, and I was also into Arduino, so eventually I ended up with an idea to make my own Rubik's cube solver.

Like many people searching for "Rubik's Cube Solver" on Google, one of the first robots I found was the Tilted Twister design by Hans Andersson:

I looked around at some other Rubik's cube solvers, but I liked that design because of it's (relative) simplicity, so I built a similar version of it with popsicle sticks, an Arduino and 2 servos.

In the end, my robot takes about 20 minutes to enter the cube state and solve.

Step 1: Materials

Electronics:

- Arduino UNO R3

- 2 servos (I used Hitec HS-311's)

- wires

- USB cable

You can get all of these parts from any robotics store. I got them from canadarobotix.com

Hardware:

- popsicle sticks

- wooden skewers

- thin plywood

- wooden wheel (skewer must fit inside the hole)

- hot glue

- paper towel roll

Software:

- Arduino IDE - (Algorithms in C++)

- Python 2.7+ and Tkinter - (GUI)

- Pyserial

Step 2: Electronics

Wiring up the servos to the Arduino is pretty straightforward

1. Connect the yellow (signal) wires from the push and rotation servos to pin 6 and 9, respectively.

2. Connect the positive and negative wires to the 5v power source and ground. Sometimes the servos had jitters, so I think a few capacitors could have smoothed out the current.

Step 3: Mechanical Design

I looked at a few designs but settled on the Tilted Twister because it only required 2 servos to operate, which wasn't as complex mechanically as other robots such as the Cubestormer.

So here's how the mechanism basically works:

1. A platform holds and rotates the cube.

2. The arm pushes and holds the cube.

When I first tried to build the solver, I tried to follow building instructions here:

But I was pretty lazy and didn't follow the instructions, so I made a wooden replica with a few tweaks.

I haven't documented every step of the build process, but I think you can reproduce the tilted twister design with a few tweaks:

1. Slanted sticks to prevent the cube from snagging.

2. To build the arm joints,

- I drilled a hole into ends of the popsicle sticks.

- I cut out 2 cm of a wooden skewer.

- I inserted the skewer into the sticks

- Glued wooden wheels that act as washers on each end.

Honestly building the physical solver was a lot harder and complex than I expected, so if you have any questions on how I built the solver, just ask.

Step 4: Arduino Sketch - Cube Solving Algorithm

[Code: https://github.com/matt2uy/Cube-Solver]

So basically the code I wrote uses combinations of pushes, holds and platform rotations to apply algorithms to the cube.

There are 3 main parts of my code: the algortihm, GUI (enter cube state) and the serial communications

Let's start with the Arduino sketch. It basically uses algorithms to manipulate the cube colours:

1. There are 6 char arrays represent each face of the cube.

2. Using a basic layer by layer method of solving the Rubik's cube, I made a function, cube_decide(), that goes through each stage or the cube solving process.

3. Within each stage, such as cube_decide_cross() (solving the cross), the program would check for specific locations of colours in the cube, and if an algorithm was matched with the condition, cube notation like 'U' (Up) or entire algorithms, like fix_cross_instance_1() would be run.

4. The cube notation and algorithms control the servo functions, such as push_cube() or rotate_one().

Here's a basic overview of the code structure (layers of abstraction):

Cube Decide functions < Cube Algorithms < Cube Move functions < Servo functions < Move Function

One major breakthrough I had was that I actually simulated the cube's movements in the program. The program reassigns the values in the arrays to simulate and cube rotation. This way, the program can simulate the cube move before it physically executes it.

Step 5: Python GUI

I needed a better way of entering the cube state than directly typing them into the Arduino sketch, so I got the idea of creating a GUI from this robot:

Because I'm new to making Tkinter GUI's (Graphical user interface) and didn't want to make one from scratch, I found a tic tac toe GUI here: http://www.dzone.com/snippets/gui-tic-tac-toe-less...

Then I turned the 3x3 array of buttons into something resembling a cube and added a few buttons. I also added buttons that would change colour each time they were clicked. Finally I made 5 more windows that would pop up one after the other, and transfer the colours entered into a local array in the script.

However, I really slapped the code together in a mess, since I simply copy and pasted 6 separate windows, with almost identical functions. Any suggestions on how I could clean up the code are welcome!

Step 6: Connecting It All - Serial Communications

Once the algorithm in the Arduino sketch and the python GUI were ready, I needed something something that would use the cube state collected from the GUI and transfer it to the Arduino sketch. After the sketch receives the data, it can put the cube state through the algorithms and physically solve the cube.

To do that, I used a library called Pyserial, which helps my python script communicate with the Arduino over a serial interface.

Basically, this is how I coded the the serial communication:

1. After the cube colours were entered in the GUI in Send_Cube_State.py, I converted the data into a string

2. Used a 'handshake', which in this case the Arduino tells the computer it's ready, then the computer transfers the data to the robot.

3. The Arduino converts the string received into chars, which are assigned to arrays in the sketch.

4. Now that the cube colours have been successfully transferred from the computer to the Arduino, the cube colours can be put through the algorithms and solve the Rubiks cube.

Step 7: How to Use

If you're trying to build the robot yourself, or just trying out the code, here's a checklist to help you out:

1. Download and install these packages and applications

- Arduino IDE (http://arduino.cc/en/Main/Software)

- Python 2.7 (Tkinter is included) (https://www.python.org/downloads/)

- Pyserial (https://pypi.python.org/pypi/pyserial)

2. Download the source files here: https://github.com/matt2uy/Cube-Solver

3. Copy and paste Cube_Solver.cpp on to the Arduino IDE.

4. Plug the Arduino in and upload the sketch.

5. Right after uploading, take note of the serial port number at the bottom right corner. (See picture above)

6. In Send_Cube_State.py, change the address in line 18 to match the number on the IDE. (See picture above)

7. Open the command prompt (Windows) or terminal (Mac/Linux)

8. Go to the directory where you put the source files in:

- using the command 'cd', for example:

- cd 'path/to/Cube-Solver'

9. Run the .py script with: python send_cube_state.py

10. If you see a series of 'y's being print on the screen:

- wait about 5 seconds

- Exit using ctrl-c or command-c

- Repeat step 9 again.

- There should be a GUI that shows up.

11. Enter the Cube colours in the correct orientation:

Colour in Front | Colour on Top

1. Yellow | Blue

2. White | Green

3. Blue | White

4. Red | White

5. Green | White

6. Orange | White

12. Put the cube inside the solver in the orientation seen here: http://goo.gl/tSqSpp

13. Click 'Solve!'

14. If that didn't work:

- Copy and paste Cube_Solver_No_GUI.cpp on to the Arduino IDE

- In line 32-54, enter the cube colors in the same convention as in step 11.

- Do step 12

- Upload the sketch on to the Arduino.

Comments

author
ArnavN (author)2017-08-14

Very nice project. Can you plz make an instructable on how to make that popsicle stick hardware model

author
ArnavN (author)2017-08-08

Can micro servo be used instead

author
Bahjatr made it! (author)2017-08-07


how i can it

wdwed.JPG
author
shaditheman123 (author)2014-11-09

voilaaa!! i just tweeked the mechanism a little to get the best accuracy for a the 5 to 8 min solves...thanks man for this project.

temp_756938420.jpg
author

Hello, it would be possible for you to send me the step by step of this construction and programming. I am a mathematics teacher and would like to build with my students. email:

Cassianomarbo@gmail.com

author
ffarook (author)shaditheman1232014-12-15

plz give a your ardino programme

author
matt2uy (author)shaditheman1232014-11-09

Nice robot!
How did you get it to solve in 8 minutes?

author
splodgie (author)matt2uy2014-11-13

Hi Matt, I cannot wait to see the video of this 5/8 min in action, man that's fast. Bet you cannot wait to see the code tweaking as well, he must be using a negative wait time between actions ?.

author
splodgie (author)shaditheman1232014-11-13

Hi, 5/8 min that is mega fast, you will have to do a video of it in action and upload the code, I tried to speed up the one i made and got it down to 15ish min from17ish min but it was shaking it's self to bits and would start to walk round the table when in action.

I see you have powered your servos from a separate power supply as the Arduino will not pass enough mAh's. I have modded my one so it can do two different cubes that I have 55mm and 57mm (one at a time that is)

author
nic1294 made it! (author)2015-02-11

I made one too!

I rewrote all the code from scratch (it uses m2/old pochman, so it's reeeeally slow, 300+ moves).

Main difference is you input your cube state with buttons and a lcd :)

Here's a video:

Thank you for this instructable!

Nicola

Immagine.jpg
author
Cassianomarbo (author)nic12942017-06-16

Hello, it would be possible for you to send me the step by step of this construction and programming. I am a mathematics teacher and would like to build with my students. email:

Cassianomarbo@gmail.com

author

Hey nic please can you teach me completely that how did u made it cos i cannot afford arudino frm my pocket money

author

I used an arduino pro mini clone, you can find one for 4-5€ shipped online. The servos are a bit more expensive, I paid 20€ for both.
The ice lolly sticks are around .03€ each, you'll need about 100 of them I guess.
To build it I suggest you follow this awesome instructable, it's pretty straightforward :)

author
splodgie (author)nic12942015-02-15

Hi, Nice one looks like it works very good and seems to have some speed to it. The one I build had a massive change done to it when I put it on me computer chair, forgot it was there and sat on it. I will build another some time soon.

author
matt2uy (author)nic12942015-02-11

That looks great!

How did you construct the metal joints on the arm? They look pretty solid.

author
nic1294 (author)matt2uy2015-02-11

M3 (16mm) hex bolts, 9mm washers and full nuts locked in position with hot glue :D

author
matt2uy (author)nic12942015-02-11

Nice!

I'll keep those in mind for the next Rubik's cube solver i'm building.

author
UditA8 (author)2017-03-23

for some reason when I send the info from GUI, CDM does everything and prints on the screen everything perfectly but the servos do not move a bit. Also many a times arduino IDE says 'uploading' for half an hour but does not upload. Also one of the servo jitters a lot. HELP PLZ

author
WafflesAreBae (author)2017-03-18

Does Tkinter come with python 2.7?

author
DwightR10 (author)2017-03-15

can you direct me to the part where it creates the algerithm to be executed? I am doing a similar project and I am trying to find code for it.

Thanks

-Dwight

author
UditA8 (author)2017-03-12

Almost done. Can you explain me better how to make the machinery itself. I wanna blow people's minds at the science fair. Dont worry, I'll give you the credits

author
UditA8 (author)UditA82017-03-12

Thanks dude:)

author
StawbB (author)2017-03-01

How to download the pyserial for python 2.7....pls provide me with a link bcause the site gives too much options n i get jumbled up as i m new

author
WafflesAreBae (author)StawbB2017-03-11

if you are to lazy to even find the correct download, I don't think you are qualified to build this project.

author
TenseT (author)StawbB2017-03-01

just goto python install directory and do

pip install pyserial

author
ZealotC (author)2016-12-17

When I try to run the .py script
NO Module Named serial COmes up like shown below. Need help please.
I dont have knowlodge about python and c++
Design is same so far this step is making me stuck.

serial.PNG
author
TenseT (author)ZealotC2017-03-01

Install the Pyserial libary

author
StawbB (author)2017-03-01

What if i dont use python and pyserial...will it still work?

author
TenseT (author)StawbB2017-03-01

No, Pyserial is used to communicate with Arduino

author
CrakTazz (author)2017-02-28

Hello, friend, I have to do a project for the university, so I decided to choose this one because I like it a lot, but I had a problem, when I went to look for the Python code the web page presents problems and does Do not give me the download link, and another question, the code should be edited in order to receive the cube faces instead of just "x" or "o". Please help me, I expect a prompt response. I'm from Colombia.

author
AnujV15 (author)2017-01-08

What should be the starting position of the motor?? Please reply

author
JeffS282 made it! (author)2016-12-03

Thanks for the instructions Matt2uy. With some tweaking of the program due to a slightly different mechanical design, I was able to make it work.

Photo attached, video link to follow.

IMG_4148.JPG
author
Khangv2 (author)JeffS2822017-01-07

hi, can i ask you something about that design :( i'm stuck. please help me
my gmail: skyhostvn@gmail.com
thanks!

author
kcgoh (author)2016-11-15

I'm having problem, can you help to take a look, which values i should adjust for the code? push_pos, rotate_pos, rotate_finish, etc - https://www.youtube.com/watch?v=Z-q8WYRX7fc

author
Technovation (author)2016-05-20

in how many moves and in how much time does it take to solve it?

author
Gcools93 (author)Technovation2016-11-13

It all depends on the solve and the algorithm input into the computer. Sometimes it can take up to 120 moves. Other times it can take 5-10. And it also depends on the speed of the servo's turning. So it will vary quite a bit.

author
Ryan tan (author)2016-09-15

what is the paper towel roll for?

author
Gcools93 (author)Ryan tan2016-11-13

In the images, it shows the paper towel roll being used to add height to the project to allow the arm to not hit the table or whatever it is sitting on.

author
Khangv2 (author)2016-11-05

Hi :( i use arduino UNO r3. and Futaba S3003 Servo. it turns not like 90 degrees :( please help me.

author
rushic24 (author)2016-10-10

which servo motor should i choose for it ? ie what should be torque required fot it ?

author
rushic24 (author)rushic242016-11-01

is 1.6kgf torque servo enough ??

author
Fathomlis (author)2016-10-09

Wow its amazing and out of popsicle sticks too!

author
Pantheon Vlad (author)2016-09-03

Hi, I just finished building the Cube solver and now made my way to the coding part, it seems that the tic tac toe GUI isn't available to download anymore. Is there another GUI link available?

author
kcgoh made it! (author)2016-08-25

is this correct? seem like repeating the same thing....

Capture.PNG
author
PrathameshK5 (author)2016-07-03

Hey matt2uy even I am facing the problem of getting endless loop of 'Whole Cross: F'. I tried entering differnt combinations but get same result for all of them.

Any suggestions?

author
shaditheman123 (author)2015-05-30

hey man im having one problem with the rotating platform, its very glitchy.in some directions it rotates 90 degrees fine and alignes perfectly with the hand but in other directions it totally misses and the platform becomes not centered so the cube wont flip.ive tried a lot of thing but i couldnt fix it.hope anyone helps .
thanks!

author
aceeeeyyy (author)shaditheman1232015-06-29

Same here. I'm almost done with my rubik' solver and i have a problem with the rotating platform. My platform does not align with the pushing arm unlike their work which works perfectly fine. I really need help with this problem too! I hope they could read our comments.

author
shaditheman123 (author)aceeeeyyy2016-05-27

try getting a clean power supply, lithium batteries work great .
if you still have the problem try getting the same hitec servos because they are more precise because some servos don't need to be that accurate like servos used in rc stuff.

author
PeterC98 (author)aceeeeyyy2016-05-24

Did you ever fix the problem of it not rotating completly? I am having the same problem

author
kikinick1234 (author)2016-05-10

cool!

About This Instructable

138,067views

544favorites

License:

More by matt2uy:Rubik's Cube SolverHow to make a vertical phone stand
Add instructable to: