How to Write Your Own 4x4x4 LED Cube Show for Arduino

98,863

97

42

Published

Introduction: How to Write Your Own 4x4x4 LED Cube Show for Arduino

About: “To invent, you need a good imagination and a pile of junk.” ― Thomas A. Edison

There are Many Instructables out there teaching you how to build an LED cube, however only a few actually help you write the code to go with it but rather just give you some to use, thus stopping your creative potential! Also there are many different ways to program the cube, but this is one of the simpler ways I found so far. If you don't know where to start, start here!

This instructable will be an "addition" onto my previous one on how to make the LED cube itself. It can be found HERE.
For those of you that built my cube, consider this the last few steps.

This instructable assumes you either know how a LED cube works or have built one yourself.

Also I assume that you have built a 4x4x4 cube (because that is what I have to take pictures of). However, this will work with any cube if wired properly.

Note that the code and cube run on Arduino.

Also, I lay no clam to the code found in this instructable, I have simply altered an existing code that has been floating around the internet. My hope here is to further explain this code in a way that people which have no prior knowledge of coding can form and write their own shows. If you are or know who originally wrote this code feel free to contact me!

Things Needed:

  • An LED Cube
  • An Arduino mega or uno, according to cube size
  • Arduino IDE
  • Computer
  • A fair amount of patience and time


Video of new pattern I wrote(Show 2):

UPDATE:

Since the 4x4x4 cube I have built a 8x8x8 LED cube and interfaced it with an old IPod Nano to play music. The animations on the cube were simply programmed and timed correctly to correspond to the music.

Step 1: Setting Up Cube

For the main construction of the cube refer to my other instructable here.

However, we are going to change some things with the setup.
   
First off the transistors i used are not needed for these types of codes. Don't cut them, but just tuck the wires to the side and ignore them. They are still needed for some other codes.

In this setup run wires directly from the ground layers directly into Arduino. Notice in Photo one i just tapped into the ground solder lines that go to the transistor board. (Red trails are solder on other side)

Then the wire go directly into the arduino where the transistor board used to connect. (Photo 2)
I color coded them for easy connections.

The 16 Columns are all wired the same as they were before. 

NOTE:
Find the part in the code that looks like this:

int LEDPin[] = {13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 14, 15};
int PlanePin[] = {16, 17, 18, 19};

The order the pins are listed changes the order they are in the code, so change this to fit your needs.



Once you have your cube set up properly move on to the next step. 


Step 2: Download the Codes

Extract the files and read the READ ME.

And if you don't have Arduino IDE, Download it.

Step 3: Understanding How the Code Works

Before writing the code you must first have a full understanding of how it works.

This entire program is based off of one line of text:

B0000,B0000,B0000,B0000,B0000,B0000,B0000,B0000,B0000,B0000,B0000,B0000,B0000,B0000,B0000,B0000,10,
     1         2          3        4          5         6        7          8          9         10        11       12       13       14       15       16

The 16 B0000 sets all represent a row of LEDs in the cube.
(The number under is the row number in relation to the code) refer to photo 2
Now don't confuse rows and columns. Rows are horizontal, columns are vertical.
Look at Photo 1



And the 10 at the end is the speed at which that frame is played.

Think of each line of code being a frame in a movie, if you slow it down there are individual pictures, but when sped up, its an animation.

Now lets break it down even further:
Each 0 is an LED, 1 is on, 0 is off.
For instance: B0000 is a entire row turned off, B1111 is an entire row turned on.

Now even further, you want to turn on an individual LED.
refer to Photo 3, it will be your best friend while doing this, write it down yourself. 
When looking at a row from the front, the first LED is the last 0 in B0000 
go look at Photo 3....pictures explain better than words.

For example, you want to turn on the bottom left hand corner LED on and everything else off.
it would look like this:
B0001,B0000,B0000,B0000,B0000,B0000,B0000,B0000,B0000,B0000,B0000,B0000,B0000,B0000,B0000,B0000,10,


Play with this yourself, the more you experience it, the faster and easier you can program.




 


Step 4: Writing Your Own Show!

First off open open LED_1cube_4x4x4_Show_2 to make sure your cube is wired correctly. It should look like the video in the beginning.
 
Once verifying that, take a look at the code and how it is put together.
The only part that matters is the B0000 stuff. Everything else just don't touch.(with the exception of changing pin #)

Lets Write your first show!
Open LED_1cube_4x4x4_Blank_Show_
This is the show, but blank! 

There is one frame there to get you started and row numbers across the top.
To turn a LED on just replace some zeros with some ones! not much too it!
Copy and paste this over and over again and your set!
B0000,B0000,B0000,B0000,B0000,B0000,B0000,B0000,B0000,B0000,B0000,B0000,B0000,B0000,B0000,B0000,10,

One last thing. 
Near the top you can find this:
#define TIMECONST 20
You can change the 20 to whatever number you want (its the time to view each frame in milliseconds).

Use this as a programming tool, slow it down a lot to look at individual frames to make sure everything is right or to figure out what is wrong. 




Step 5: Examples

All take place when looking from front.

Turn on bottom left hand light:
Photo 1
B0001,B0000,B0000,B0000,B0000,B0000,B0000,B0000,B0000,B0000,B0000,B0000,B0000,B0000,B0000,B0000,10,

Turn on row 1:
Photo 2
B1111,B0000,B0000,B0000,B0000,B0000,B0000,B0000,B0000,B0000,B0000,B0000,B0000,B0000,B0000,B0000,10,

Turn on all four corners: 
Photo 3
B1001,B0000,B0000,B1001,B0000,B0000,B0000,B0000,B0000,B0000,B0000,B0000,B1001,B0000,B0000,B1001,10

Turn on all of layer 1:
Photo 4
B1111,B1111,B1111,B11111,B0000,B0000,B0000,B0000,B0000,B0000,B0000,B0000,B0000,B0000,B0000,B0000,10,

Step 6: Finishing Up

Hope this helps someone!
Took me a while to figure out how to do this, hopefully save everyone some time and headache.
Feel free to comment or message me!
Thanks for reading.

Share

Recommendations

  • Oil Contest

    Oil Contest
  • Planter Challenge

    Planter Challenge
  • Make it Move Contest

    Make it Move Contest
user

We have a be nice policy.
Please be positive and constructive.

Tips

Questions

42 Comments

Circuit diagram show

Plz send program document led cube klkeshava@gmail.com

Pls send program document led cube

sir

exit status 1

'prog_uchar' does not name a type

errro

sollution this program

Sir, I am having a problem with your code, it says

"Arduino: 1.6.4 (Windows 7), Board: "Arduino Uno"

LED_1cube_4x4x4_Show_2:15: error: 'prog_uchar' does not name a type

LED_1cube_4x4x4_Show_2.ino: In function 'void loop()':

LED_1cube_4x4x4_Show_2:447: error: 'PatternTable' was not declared in this scope

'prog_uchar' does not name a type

This report would have more information with

"Show verbose output during compilation"

enabled in File > Preferences."

Please help!

3 replies

Change that line to this

const int PatternTable[] PROGMEM = {

Yes, but when you do that it doesn't upload.

Worked for me 9 months ago. Things might have changed.

Sir, I am having a problem with your code, it says

prog_uchar does not name a type

Will the program work with audrino mega

to do one at a time you need up down side code like 123wich will light the led one up two to the side and three up

Hello tjmanders!

Great post, good information.

I have a question that has been on my mind that I cant seem to solve (anyone please reply). After reviewing the code, the last part looks as though the entire cube is light up at once. Is this not drawing too much power from the arduino? is there some part of the code that I am not seeing that implements POV where only one led (or layer) is on at any given time?

Hi jtmanders. Thanks for the user friendly programming tutorial.

I understand the concept, but like hwillard my 4^3 cube's layers are connected to anode and my columns are connected to cathode resulting in my cube not working.

I built my cube using the KyleTheCreator's instructable (https://www.instructables.com/id/4x4x4-LED-Cube-Arduino-Uno/?ALLSTEPS)

It's probably a stupid question, but how do i alter your code to make it match my cube?

Thanks in advance.

3 replies

Actually I think this is easy to solve. Assuming your cube is exactly the same and exactly the same wired but the only difference is you switched anodes and cathodes in your cube you would just need to add transistors between the arduino and the cathodes.

The backside with this is now you need 16 transistors, instead of the usual 4, but just adding these 16 transistors would seem to solve the problem and the code is the same.

Wow, thanks! I will definitely try that.

Your cube would be like this, but you remove the 4 transistors of the blue wires and just run the blue wires directly from the arduino to supply +5V to the 4 layers.

Then you put 1k resistors on the 16 red wires and then to the base of the transistors. And then you just add your led current limiting resistors before the collector pin of each transistor and then gnd every single emitter!

Hope this helps!

Krets.jpg

Hi.. thanks for sharing.
I made the cube. following your steps.. But leds arent bright enough.
Can u explain way u use 22k resistor for bases of transistors.
thanks...

1 reply

The resistors are heavily dependent upon the LEDs you use, if they are dim simply exchange the 22k for some lower, maybe 10k?

Did you ever figure out who wrote the original code? I made a web application for editing these animations, and I would like to know who to credit :)

1 reply

Micheal_Bell - Yes this interested me too. I asked exactly the same question on the Arduino forum here http://forum.arduino.cc/index.php?topic=193436.0 There are plenty of people all over the net laying claim to this code and sorry to be a "doubting Thomas" but looking at the OP's other code example like _4x4_LED_Cube_Sequence_1 which is a low level beginner coding, I sincerely doubt the the OP wrote the more complex code examples that he is claiming are his. Sad really that people feel the need to plagiarise the work of another to make themselves feel important. Also I would be really interested in having a look at your web app for this code if you are willing to share it. I promise that I will not claim it as my own work :-)