Rubiks-Cube-Recover-Bot 魔方复原机器人

A robot to recover Rubik's cube, based on Arduino, using Python combined with Machine Learning and OpenCV.

Thanks to these projects and resources:


1. Software Part

The function of this robot is divided into two parts: Rubik's cube recognition and automatically recover it.

The first part focuses on recognizing and solving Rubik's cube, which is mainly written in Python. The second part focuses on controlling the Arduino to move the Rubik's cube, which is written in a .ino Arduino program.



1.1. Recognizing and solve the Rubik's cube 魔方识别与求解

Written in Python. There are 5 steps:

  1. Using Keras to build up a Unet model, and carve up the 6 faces of the Rubik's cube.
  2. Using OpenCV to parse the colors of the bricks.
  3. A GUI to show the colors, allowing correcting the color.
  4. Solving the Rubik's cube via Two-phase algorithm.
  5. Sending the solution to the Arduino via serial port.

First, the computer's front camera captures images of every three sides of a Rubik’s cube. The the machine learning program then recognizes the six faces of the Rubik’s cube, with OpenCV to detect the color of each brick and read the HSV value in the image. In real-time, a visualization window outputs colors of 54 bricks on six faces. After obtaining the color data, Herbert Kociemba's two-phase algorithm solves the Rubik's cube. Then, the solution, in the form of a string, is transmitted to the Arduino through the serial port, and the Arduino controls the stepper motor to rotate the Rubik's cube and recover it.


  1. 采用Keras库建立Unet模型,实现从摄像头图像中分割魔方的6个面。感谢RubikKeras项目提供的模型。
  2. 采用OpenCV,解析魔方块的颜色
  3. 以GUI界面显示魔方色块,提供手工校正魔方色块的功能
  4. 用Two-phase算法求出魔方最优解
  5. 将魔方解以字符串形式通过串口传给Arduino板

首先通过电脑前置摄像头对魔方的三个面进行图像捕捉,使用机器学习分两次完成对魔方六个面的识别,配合OpenCV对图像中魔方块的颜色进行实时检测并读取HSV值,并输出六个面54块颜色,通过Tkinter做出可视化页面,显示解析结果。获得魔方数据后使用Herbert Kociemba's two-phase algorithm对魔方进行求解,得解后将解以字符串的形式通过串口传给Arduino,Arduino控制步进电机转动魔方将其还原。

1.2. Turn the cube using Arduino 用Arduino转动魔方

2. Hardware Part

  • Arduino MEGA2560
  • 42 Stepper * 6
  • L298N to drive

The most common stepping motor-- 28BYJ-48 four-phase stepping motor was initially selected. But in the initial test, it was found that its torque was too small to turn the Rubik's Cube, so it was replaced with the 42 stepper motor with greater torque, driven by L298N. Due to the need to drive 6 four-phase stepper motors at the same time, the commonly used Arduino UNO has insufficient digital signal ports, so the Arduino MEGA2560 is used for control.

步进电机最开始选择了最为常见的28BYJ-48四相步进电机,在最初的测试中发现其扭矩过小,无法转动魔方,于是更换为扭矩更大的42步进电机,通过L298N驱动。由于需要同时驱动6个四相步进电机,常见的Arduino UNO存在数字信号端口输出不够的情况,因此选择Arduino MEGA2560来进行控制。