loading
Picture of 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.

 
Remove these adsRemove these ads by Signing Up

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

Picture of 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

Picture of Mechanical Design
2014-05-18 15.01.08.jpg
2014-06-19 13.50.25.jpg
2014-06-19 13.00.06.jpg
2014-06-19 13.00.25.jpg
2014-06-19 13.01.42.jpg

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

Picture of Arduino Sketch - Cube Solving Algorithm
notation.png
2014-06-20 15.52.14.jpg

[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

Picture of 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

Picture of 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

Picture of How To Use
Screenshot from 2014-07-15 18:29:43.png
Screenshot from 2014-07-15 18:32:38.png

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.

1-40 of 142Next »
lgiusti1 made it!18 days ago

Thank you very much Matt for this amazing instructables, can't replicate exactly your version, mechanics was for me too complicated, so i choose this slightly different...

https://www.youtube.com/watch?v=NRRSYEWIQ_w

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!

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.

Extra power connection using USB power bank.

Rubik Extra power.jpg
kousheekc1 month ago
How to chamge the angle that the push servo is pushing at?
Thanks in advance
AxelG2 months ago

I have an Arduino MEGA 2560, is it compatible? Thanks!!

matt2uy (author)  AxelG2 months ago
Yes, you can use any arduino for the project.
AxelG matt2uy2 months ago

Maaaadeeeee!!! Thank you so much!!!!

AxelG matt2uy2 months ago

Thanks, but when i'm uploading the program to the Arduino, it sends me a message avrdude: stk500v2_ReceiveMessage(): timeout. Never happened before.

SarvagnyaP2 months ago

Hi matt, why do you have so complicated rotation function?

kousheekc2 months ago

is the rotate servo a full rotation one

nic1294 made it!5 months ago

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
Hey nic please can you teach me completely that how did u made it cos i cannot afford arudino frm my pocket money
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 :)
splodgie nic12945 months ago

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.

matt2uy (author)  nic12945 months ago

That looks great!

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

nic1294 matt2uy5 months ago
M3 (16mm) hex bolts, 9mm washers and full nuts locked in position with hot glue :D
matt2uy (author)  nic12945 months ago

Nice!

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

kousheekc3 months ago

i like your instructables

splodgie made it!9 months ago

Hi, Well after waiting weeks for the servo to turn up I completed the the Project (98% ish, still needs a bit of cleaning up to call it finished). Used a Arduino Nano V3 as it was cheaper, $2.50, 2x Servo's $6.50 of eBay, £1.00 2x 50 Garden marker sticks from Wilko's, Tube of Uhu glue from £1 shop, (guess what it cost), and about 40Hrs work in total, Setting it up and working out the bugs was a bit of a pain but part of the fun as well, made it so it all pulls apart if needed, so in all it cost about £10.

One things sure without the Instructable and Matt2uy ,I could never have worked it out, The best bit when it's working is the way it och's the cube into line so it's squared up for the next move, brilliant bit if coding Matt.

As you can see in the video I had a play with the GUI code to make it easy for me to use, (I know nothing about coding apart from look and see, and maybe that may work).

Did a quick Video to show it working. Now it's time to get some sleep .

Thanks again for the Instructable.

Dave

cubeca splodgie3 months ago

Hey splodgie, can I see your GUI code?

splodgie cubeca3 months ago

Hi, When you have time upload a video so we can see it in action.

cubeca splodgie3 months ago
Hi splodgie,
I have it running perfectly! I am trying to add a couple of more buttons on the GUI. I'd seen your GUI and want to do something close to it. I appreciate your reply!
matt2uy (author)  splodgie9 months ago
That's awesome!

I watched your video and I think your mechanical design is a lot more reliable and well constructed than the one I made.

It's really awesome to see someone actually use my instructable to help make their own robot!

When you're 100% done, I think you should make an instructable if you want.

Matthew
splodgie matt2uy9 months ago

Hi Matthew, Thanks for your comment and very happy you like my attempt at taking your design and hard work in trying to replicate it in the best way I could.
I tried to stay within the materials used and method of your design, (A evo design based on the master prototype).
As you know getting it to work after you have made your design is the hard part, or you are very lucky if it works first time.

I may try to show some of the design and the measurements I have in my copy at a later date. As some one said ' the way it works and the materials used to complete the mind bogeying complication of completing the Rubik's cube just has to makes you smile'. you should be on Dragons Den with it.

Dave

matt2uy (author)  splodgie9 months ago

Yes, getting it to work probably took up more time than actually making it for me, but that was probably due to my build quality.

I probably won't be on Dragons Den, but I hope that this project is a stepping stone for some more projects I'll be making.

MasterP23 months ago

hi matt, i have got your reply.

but still i have big problem in step7 from points 6 to 9..

i am not able to understand these points. i a totally new to c++ and python. so could please ..........please tell me briefly the step 7 one by one

it's a humble request one again. thanks in advance.

matt2uy (author)  MasterP23 months ago

So basically, point 6:

- open the file named Send_Cube_State.py and on line 18, replace the '0' with the correct address at the bottom right of the Arduino IDE. (see both pictures in step 7)

Point 7, 8 and 9:

- to open the command prompt, just enter 'cmd' in Start or Run.

- then use the 'cd' command to go to the right folder (where Send_Cube_State.py is)

- use the 'python' command to run the script

- you can learn more about command prompt stuff from here: http://dosprompt.info/folders.asp, or on google.

cubeca3 months ago

Hi Matt, can you provide me the link for the tic toe GUI you were using? Really appreciate your help! :)

matt2uy (author)  cubeca3 months ago

I found it here: http://www.dzone.com/snippets/gui-tic-tac-toe-less-100-lines

MasterP24 months ago

this error is always coming.

please help and thanks in advance.

1.PNGcube state.PNG
splodgie MasterP23 months ago
aceeeeyyy4 months ago

Hi Matt, I have a question. I am currently building my robot and I keep getting a blank GUI every time I run the send_cube_state.py code on my terminal. I'm using a mac. What should I do to make the colors appear on the GUI? Here is a screenshot so you can see if there is a problem. Any help is much appreciated. Thanks in advance!

Screen Shot 2015-03-14 at 13.57.30.png
matt2uy (author)  aceeeeyyy4 months ago

Sorry for the late reply, my email notifications didn't work.

Anyway, I'm not sure how to fix this problem because I don't use a mac. It might be a problem with tkinter, but i'm not totally sure.

Someone else had the same issue here: https://github.com/matt2uy/Cube-Solver/issues/4 so it could still work if you memorized the order of the colours, or you could tinker with the python script.

aceeeeyyy matt2uy3 months ago

I considered using different versions of mac OS and none of them worked. I switched to a windows PC and the GUI showed up! Complete with all set of colors! I will update you when I finish building my project. Thanks by the way!

MasterP24 months ago

hi matt, my python is not able to run the send_cube_state.py file.

could you please send me the exact code and method to upload, run and make the rubik's cube gui, to my email id ( helloiamparas@gmail.com).

it's a humble request. please send as soon as possible.

matt2uy (author)  MasterP24 months ago

Everything you need is in step 7: "How To Use".

BTW, you should run the script from the command prompt, not the python interpreter.

If step 7 is missing anything, just ask me.

Yusuf BeenH4 months ago
cubeca4 months ago

Hi Matt,

Another question regarding the rotating servo, what is its range of motion? The one I got only rotates up to 180 degree but looking at your code it seems like it needs over 270 degrees range. Thanks in advance!

matt2uy (author)  cubeca4 months ago
I used servos with 180 degrees of motion.
1-40 of 142Next »