In this instructable I’ll show you how you can build your own LED Rubik’s cube. This project was done for an introductory electronics course at Pomona College. Groups were given approximately two months to complete a project of their choosing, the only requirement being that the project rely on electronics built by the students.
Our group decided to build a Rubik’s cube that uses RGB LED’s for its faces, and buttons to simulate rotations. This should walk you through the process in enough detail for it should be clear what you would have to do to recreate our project.
Step 1: Essential Electronics.
Below are the components on which the cube’s electronics run:
- 5mm RGB LED x 54
- 330 Ohm Resistor (1/4 Watt) x 162 (3 per LED)
- 74HC595 8-bit Shift Register x 54
- Amico 8.5mm 4 pin DIP pushbutton x48
- Arduino Uno
- 28 Gauge Wire
The RGB LED’s, resistors, and pushbuttons are self-explanatory, but the Shift Registers aren’t. The LED’s can generally be found for about $.75 each, while the shift registers are about a buck a piece on Amazon.com. The buttons are on Amazon and are about $.05 a piece.
Step 2: The Shift Register
The Single Shift Register
In isolation, a single shift register gets you eight outputs for the price of three I/O’s on your Arduino. It is an integrated circuit that requires power and ground, and takes 3 inputs from the Arduino. These input pins (11,12,14) are called the clock pin, latch pin, and data pin respectively. Of the shift register’s sixteen pins, eight are outputs which can be used to control various devices digitally. The states of these outputs are determined by sending the shift register data. Given one byte of data the shift register will turn its eight outputs either high or low, 1 for high, 0 for low. The Arduino uses the clock and latch pins to tell the shift register when to receive data and when to process it, while the the data pin is where the data is actually carried. The Arduino has a built in command to give data to the shift register. A tutorial can be found here: http://www.arduino.cc/en/Tutorial/ShiftOut. Knowledge of the shiftOut command described in the tutorial is important, but easily summarized. The syntax is:
digitalWrite(latchpin;LOW); //signals shift register that it is about to receive data
shiftOut(dataPin, clockPin, MSBFIRST, data); //sends out byte named ‘data’, starting with the most significant bit.
digitalWrite(latchpin;HIGH); //signals shift register to process the data, switching desired outputs.
All this command does it tell the Arduino to send out whatever byte we have called ‘data’ to the shift register. The shift register will take a byte, and use the value of the first bit to control its first output and so on. The MSBFIRST command tells the Ardunio which bit to send out first. The leftmost bit is the most significant. So, as an example, if data=11111111, the shift register will write all of its outputs to high. If data=00000000, the shift register will write all of its outputs to low.
Multiple Shift Registers: The Daisy Chain
The real power in the shift register comes from their ability to be linked together in a daisy chain. If two shift registers have their clock pins and latch pins connected, and we connect the data out pin (9) of one shift register to the data pin (14) of another, the Arduino can communicate to both serially. In this fashion the Arduino can communicate to a virtually unlimited number shift registers and other devices, still using only three I/O’s. If we replace our lone byte ,’data’, by an array of bytes such as: data[]={a,b}; where a=00000000 and b=11111111, then the command shiftOut(dataPin, clockPin, MSBFIRST, data); will give byte a to the second shift register, and byte b to the original shift register. In this way an arbitrarily long daisy chain works just like a conveyor belt. The first byte in the data array is the first one to be shifted out, and goes straight to the first shift register, which hands it off the next shift register and so on until the end of the chain. This happens with each byte, so the last shift register ends up receiving the first byte in the array, while the first shift register receives the last. This makes chain of shift registers a great way to control a series of 54 RGB LED’s by coding with an Arduino.
Step 3: Concept
By connecting each LED to a shift register, and shifting out a data array of 54 bytes to a daisy chain of shift registers, we can control our LED’s however we want. Using only digital writes, the three diodes in an RBG LED provide us with 8 colors to choose from: Red, Green, Blue, Yellow(RG), Violet(RB), Teal(BG), and White(RGB). If we connect our LED’s systematically so that their leads correspond to the same pins on each shift register, then we can define 8 bytes, each of which represent a color an LED receive.
To operate the cube we create a 54-byte array, in which each of the six colors we choose appears nine times. The order of bytes in this array is important, because, upon start, this array will get shifted out immidiately, and must bring the cube to a state in which it is already solved. Rotations are executed by swapping array values in a one-for-one manner, and then shifting out the array again. As each LED always corresponds to the same index in the array, we can easily figure out which values must be swapped to achieve a given rotation.
Upon inspecting a Rubik’s cube you should find that there are exactly twelve distinct operations that can be performed. A Rubik’s cube has six faces, and we can take any face and rotate in one of two directions. Keeping a fixed orientation, each rotation will change the position of twelve squares along the edge of the cube, as well as 8 squares on one of the faces. Thus, in our code, we must have twelve subroutines that each swap twenty bytes in the array. For operating the cube, we decided on 8 buttons per face, so that we can rotate either of the four edges of that face in either direction. For a cube with six faces, and 48 buttons, there will four sets of twelve identical buttons. To account for this we electrically connect identical buttons, so that they can share an input at the Arduino. Thus, we use 15 pins on the Arduino in all. We could have cut this number down buy addressing the buttons with a multiplexer that requires fewer inputs.
Step 4: Designing the Cube, Part 2
While the theory behind how the cube works is straightforward, implementation was difficult. Our first prototype was built on a breadboard, and contained an unruly amount of wires. Doing some numbers:
9 LEDS x 4 wires per LED=36 wires
27 Resistors x 2 leds per resistor= 54 leads
9 Shift register x 7 wires per LED (clock, data, latch, data out, power x2, ground x2)=63 Wires
8 buttons x2 wires per button (one wire to Arduino and one to ground) = 16 wires.
Let’s be nice and say we would required about 90-100 wires per face. At six faces, it becomes impossible to design a self contained Rubik’s cube using this many wires. At a certain point we came to the reality that we were going to have to design our own circuit boards.
Step 5: Designing the Cube, Part 3
Designing a circuit board is not too difficult with some guidance. We used Eagle Hobbyist to design a 4″x4″ circuit board that did everything our bulky prototype was capable of doing. Eagle is a neat software package that works by compiling enormous libraries of parts that one can add to a schematic file. In the schematic file we add our parts and then use the ‘net’ function to electrically connect the necessary parts. From the schematic file, Eagle automatically creates a board file, in which we design the board’s layout, including the size and spacing between parts. We then use the auto-route function to calculate the most efficient way to electrically connect all of the pins as designated in the schematic file. Once satisfied with the board, we export a series of gerber files that any circuit board printer readto fabricate printed circuit boards (PCB’s). Eagle does offer links to services where you can order your board boards directly. The two photos above are images of our schematic and board files.
Step 6: Assembly, Part
With our circuit boards in hand, we had a LOT of soldering to do. All of our components must be soldered directly into each board, a total of nearly 300 joints per board.
For more detail: LED Rubik’s Cube With Arduino Schematic