Introduction: 3D Snake on Charlieplexed 4x4x4 LED Cube


This is my first instructable so while I'll try to make it as clear as possible, please leave a comment with the good/bad parts and/or spelling mistakes in my instructables. Furthermore, if you liked the instructable, you can vote for me in the Microcontroller Contest ;).

We will create a 3D Snake game based on a charlieplexed 4x4x4 LED Cube. We will use an old controller, an arduino joystick and 
a pushbutton to create our very own controller to control the LED Cube. Although I will try to explain the code as much as possible, it's still good if you have a little knowledge about arduino, c++ or at least any other Object Oriented programming language. I will work with some classes and references and pointers (not applicable to for example Java) and these will be very hard to understand without some basic knowledge. While my code may work for you, it's probably a good idea to understand what's going on because you might have to change some pin numbers, etc.

I tried to make this game by trying to buy the least possible. This is why you'll see me use an old cartboard box of my cellphone as the case, etc. I personally like the "create things with what you can find in your house"-mentality and I don't have a big budget for my hobby (yet) as I'm still studying. You can of course feel free to use more professional items and create a much cleaner game ;).

Some notes:
I never studied anything about electronics, so sorry if sometimes my explanation can be confusing or I don't use the correct terminologies.
- This was a project I did spread out over a couple of months :) That's why I tried to include many testing opportunities so you can check what you made up till then.

Step 1: What Will You Need?

I'll start with a list of needed parts you might need to buy:

- An arduino (with at least 13 digital pins and 2 analog pins) (I used an UNO)
- A joystick with X and Y axis at least and preferably a Z axis
- A push button (two if you have no Z axis on your joystick)
- A toggle switch
- Some electrical wire
- Much male-male jumper wires (I used around 40 of different sizes)
- Some female-female jumper wires (probably needed for the joystick + to create longer cables)
- In general all sorts of jumper wires can come in handy :)
- A breadboard (not really necessary but very practical)
- 4 resistors (check below for which value you'll need)
- 64 LED's of any colour you like (preferably the same)
- 9V battery
- A battery adapter (
- A 2.1mm power plug (one that fits in the arduino) [Not Necessary]
- Some heat shrink tube
- Some glue (for plastic)
- An old (broken) controller (Xbox/pc/...) [Not Necessary]
- A case to put your arduino and wires in (I used a cardboard box of an old cellphone but wood is also very nice!)
- A piece of wood (thicker than the plastic part of the LED and  bigger than 6cmx6cm)

The tools I used for this instructable:

- A soldering iron + solder
- A wire cutter
- Wire stripper
- Pliers
- Wood drill + 5mm drill bit (if your LED are 5mm)
- Ruler
- Computer with Arduino software
- Some Arduino/c++ programming knowledge

Resistor volt calculation
To calculate the volts needed, you'll need to know some values: the "Supply Voltage", "Voltage Drop Across LED"  and "Desired LED Current". The first one depends on your Arduino and normally is 5v but may sometimes be 3.3v. Be sure to check it.

The first two values should be included with the LEDs you bought or on the website. For my LEDs they where 3.3v and 20mA (see link below).

Once you know these three values, simply fill in the values in the link below (single LED) and check which resistor value is needed.

Step 2: The LED Cube: Introduction

I will start with an introduction to how we will build the LED-Cube. This part is the most time exhausting of the whole project so I'll try to elaborate it as clear as possible.

To start with the LED Cube you will need some (basic) electronics knowledge. If you don't have any knowledge, I suggest you read some stuff about multiplexing on the internet and the wikipedia article on charlieplexing and be sure you understand it. For those who like to watch video tutorials I can recommend those two videos:

If you understand how multiplexing and charlieplexing works, you will be fine for the electronics part.

Another skill I assume you have is soldering. If you don't know how to solder you might wath some videos like this one first (really, it's not hard).

For the code, I will always provide a link to a codebender project. Since I had some problems with the Arduino IDE I found codebender to be the perfect solution. Just install the plugin and you can upload my sketches directly to your Arduino! You can also choose to download my files or copy the code to your Arduino IDE yourself. I use multiple files/classes for the Snake game so you can check this tutorial so you get a hold on how to create custom libraries and use them in your Arduino IDE if you prefer to use the original IDE.

At last I would like to give the proper credit to kenyer for the charlieplexed 4x4x4 cube. I followed his guide and it's definitely been a great help for me. My guide for the LED Cube is mostly based on  my experience while following his Instructable (though my cube is not responsive and I changed some of the connections, ...).

Step 3: The LED Cube: the Rig

First you have to make a rig to make the soldering easier. Check the width of your LEDs for this first. Mine (and most) are 5mm. Because I liked 7.5mm between each LED, I chose 12.5 (5+7.5) as my distance to draw lines on the wood.

Take the piece of wood and draw 4 lines horizontally and 4 lines vertically on it with each time 12.5cm between each line. Of course you should pay attention that the distances are as correct as possible and that the lines are perpendicular. You can of course choose 12 or 13cm if that's easier.

When you finished these lines, carefully drill a (for me 5mm) hole on each of the crossing points of the lines and your rig is finished! Be aware that your LED shouldn't be able to move easily after being pressed in the rig, otherwise your soldering will be harder to perform.

Step 4: The LED Cube: Preparing the LEDs

For your future reference I'll here give some basic electronics about the LED and it's (+ and -) poles:

       +       |        -  

anode    | cathode
long leg | short leg
flat side  | round side

I'll always try to use +leg and -leg because I think all people understand what I mean by this (and find it easier than anode & cathode).

When I talk about the outer legs I means the ones on the sides of a matrix. For each row there are 8 "outer legs", 4 on one side and 4 on an adjacent side. Please watch all the pictures to make sure you understand this.

As a good practice I suggest you test the LEDs before you use them. One broken LED in the cube can cause half of the cube not lighting up so it's very hard to detect and replace broken LEDs in the cube. You can easily test them by holding a thin battery with the + pole on the long leg and the - pole on the short leg of the LED.

For the soldering, it's the easiest if you first bend all your LED legs in the right position before starting to solder. By doing this you can make sure the legs are bended exactly in 90°.  I will always give instructions on how to bend the legs of a layer.

If the legs of your LEDs are long enough to reach the leg of the LED two places away from him, I strongly suggest you first cut off a piece. Otherwise you will have a hard time later on because different legs should not (always) touch...
To understand better what I mean and how much to cut off the legs, please look at the pictures above.

This is not necessary for all the legs, the legs on the outside of the cube can be left as long as you want (for now).

Step 5: The LED Cube: Bottom Layer

For the first (bottom) layer the LEDs will need to be put in with the -leg in the bottom left corner and the +leg in the top right corner. The +leg will need to be bended to the right and the -leg down. Please note that the corners I'm referring to are not the corner holes but rather the position of the leg when you look straight at in (see picture).

After you bended 16 LEDs, you can start the soldering from the bottom right corner. Don't forget to cut off a piece of the leg when needed (previous step).This way you will have the least troubles placing your legs right. From there on I first completed the first row by soldering the next three LEDs on the bottom row to it.

The following pictures show more precisely what you must do, but be aware that I did not cut the legs here (on purpose) neither did I really solder them. I just made all the pictures with the same LEDs to make the process more clear (the pictures are not from the actual soldering process).

After the first row start with the second row (beginning from the right). This time you should be aware that the -leg of the second row LEDs (the ones that should face downwards) have a little kink in them: they should go over the horizontal +legs of the first row and be soldered to the +leg of the first row. I hope the pictures will make it more clear. To make sure the legs are not touching you can use a screwdriver or something similar to make some space between them.

If you have finished the first two rows, just complete the third and fourth row the same way. Always be sure none of the +legs are touching the -legs. This way you should end up with a 4x4 multiplexed LED matrix. This means you can control all 16 LEDs individually with using only 8 ports on your Arduino.

If you're feeling like you're on a roll, you can make an exact same copy of this layer already, as we'll need it for the top layer!

Step 6: The LED Cube: Testing the Bottom Layer

To be sure the first layer is working as it should be, we will now test this layer. Like this we can easily check if all the LEDs can be controlled individually and everything works as expected before soldering the different layers together.

The easiest way for doing this is by putting the +legs of the matrix in a breadboard and make the connection from there to pins 4 till 7 on your arduino. Now attach the resistors on the -legs and connects those to pins 0 till 3 on the arduino (using the other side of the breadboard for this might come in handy).

Once this is done, you can upload the following code directly by using my codebender sketch located here, chosing your arduino model and clicking USB flash after connecting your Arduino to your computer. Alternatively, you can copy and paste the code from this pastebin link.
You should see something similar to this:

If not all LEDs light up, please check your connections again and see nothing touches. Also be sure you connected everything correctly.

Step 7: The LED Cube: Second Layer

Be sure to put your first layer somewhere else or tag it so you can easily see what is the first layer later on!

Once you finished the first layer, the second one should be straightforward. Now you hold the LED with the -leg (short one) in the upper right corner and the +les in the bottom left corner (see picture). The -leg has to be bended to the right and the +leg downwards. All the other things remain the same. This way you will create a second LED matrix but now the horizontal legs are negative (the opposite from layer 1).

Once you finished your second layer, I recommend testing this layer first. Again put the +legs in a breadboard and connect them to ports 4 till 7. Then connect the -legs to a resistor and then to ports 0 till 3. By using the same code as before you should see something similar.

If this works, you successfully completed the second layer! To prepare for the next step you should bend all the 8 outer legs of the second layer downwards. As terms as downwards are a little tricky in this situation, I recommend you just look at the pictures and you'll understand what I mean. If you were to put the matrix back into grid (which I recommend), it would mean bend all the 8 outer legs up so they're pointing to the ceiling (upwards, but downwards as in terms to how the LEDs will be placed in the end :).

For the first layer you should only bend the +legs but in the opposite direction. This means you put the LEDs with the round side pointing up (the opposite of putting it in the rig which would be with round sides down). Now you have to bend the +legs so they are pointing upwards.  The -legs can stay as they are (they will be bended in the opposite direction later on). There are some pictures in the next step if you are not totally sure.

Step 8: The LED Cube: Charlieplexing the First Two Layers

Now it's time to solder the first and second layer together. Put the first/bottom layer on a piece of wood with the LEDs round sides pointing upwards (as they will be in the final cube). Now the +legs should be facing towards the ceiling. These +legs should be soldered to the downwards facing -legs of the second layer. If you did all correct that would mean the downwards facing +legs of the second layer can afterwards be soldered to the -legs of the first/bottom layer.

This soldering can be tricky because it might be hard to keep the layers in place while soldering. I tried to keep a distance of 5mm between the two layers. For this, you can put a stack of post-its or anything else on top of the first layer and put the second one on the post-its. Furthermore I tried to pin the legs from the second layer somewhere between the soldering of the two legs of the first layer. Once you have succeeded in soldering a few legs, the others should be easier.

Step 9: The LED Cube: Testing First Two Layers

The testing setup is the same as before: Connect the +legs of the first layer with pins 4-7 and the -legs of the first layer with a resistor which are connected to pins 0-3.

Now you can upload the code directly from here (codebender) or copy and paste it from here (pastebin).

You should see something similar to this video (but slower):

Step 10: The LED Cube: Third Layer

You can solder this layer exactly the same as the second layer. The bending of the legs is also completely the same!

If your matrix is soldered correctly (and preferably tested), you have to solder it onto the second layer. This time, the 4 +legs of the third layer will be soldered onto the 4 -legs of the second layer. You should be able to achieve this by positioning the layer the same as the second layer and then turning it 90 degrees clockwise.

To make sure you're about to correctly solder this all together: the 4 outer -legs should be on the opposite site of the +legs of the second layer (which should be soldered down on the first layer).
The -legs of the third layer should not be soldered to anything (yet)!

If you aren't really sure that what you're about to do is correct, please inspect the picture very carefully and I think you will get it. You should now have 1 side of the incomplete cube where there are no legs sticking out or soldered to another layer. More pictures at step 12 of the completed cube!

Step 11: The LED Cube: Top Layer

As mentioned before the fourth/top layer is soldered exactly the same as the first/bottom layer. Be aware though, for the fourth layer you have to put it back in the rig and bend all 8 outer legs upwards. This way we can easily solder it to the third layer. When this layer is properly tested, you can start soldering in on top of the third layer.

The top layer will have to be turned 90 degrees clockwise in comparison with the first layer (Just like the third layer in comparison to the second). This should mean the -legs of the fourth layer should be soldered onto the +legs of the third layer. The +legs of the fourth layer should now be in the right position to be soldered to the -legs of the third layer (which are bended downwards but not yet attached to something).

For pictures, check below! This should make the connections between the layers clear.

If you did everything right, the cube should now be complete!

Step 12: The LED Cube: Test It!

This time the setup for testing will be somehow different than before. We will now use 12 ports on the arduino for controlling all the LEDs. One important thing is that you connect the right legs to the right ports on your arduino, or the code will not work for you!

I will first explain to you what I mean when I will refer to the "front" of the cube. The front of the cube is the side where you have no outer legs and you have not soldered different layers together.

If you watch at the front, the left side should have the -legs of the first layer still sticking out to be connected. The right side should have the -legs of layer3 sticking out. On the back you should have a connection soldered between all four layers.

If all these things are the same with you, you probably have connected everything as you should have! Now you can connect the cube according to the table provided below or the picture above (I wrote port numbers next to the legs on the pictures). The resistors should be attached to the back!

    SIDE    |    PORTS

    left            0-3
    right         4-7
    back        8-11 (resistors first!)

By attaching the resistors to the back, we only need 4 because those 4 'lanes' are always used when any of the LEDs of the cube is switched on..

Now we can upload the code! As usual I provided a codebender project link and a link to pastebin.

We will first test the different layers by lighting them one by one, then lighting multiple of them at once and finally light all LEDs at once. Then we'll do some patterns so you can easily confirm all the LEDs work seperatly. If all goes as planned, you should see the same as in my video. Make sure every single LED lights up!

We are now ready to start creating our controller!

Step 13: The Controller: Preparing

First of all: you have many different sorts of joysticks for the arduino. I used one with a x, y and z-axis like you can see in the picture. One thing I didn't like is that I needed to provide each axis with VCC and GND cables (VCC should be connected to the 3.3v port on your arduino, GND to a GND port). So for the sake of having the least possible cables going from my controller to my arduino, I decided to solder them together first. I connected my joystick with female-female jumper wires (you might need other for your joystick).

In the other side of my jumper wires of the GND ports on the joystick I put some small electronic wires. Now the other sides of these three wires need to be soldered together with an male-male jumper wire and another somehow longer electronic wire. We will need the jumper wire to connect it to the arduino and the other wire will go to the other push-button.  I did the same with the VCC ports of the joystick only here the additional wire is not needed.

Note that you can skip these steps if you have another joystick like in the second picture (with only one VCC and GND). You should still connect the additional wire the the GND port of your joystick though!

If you have no z-axis on your joystick you will need to use two push-button and connect two additional wires instead of one.
Put some tape around the exposed parts of the wires so it wouldn't touch the other wires and short circuit your Arduino. You can of course also use heat shrink if you prefer to.

Once this is done you should be able to connect your joystick with your Arduino with only 5 wires (VCC, GND, x, y, z1, z2). We will need two wires for the z-axis because these are push-buttons with digital output (HIGH or LOW). Since we will need to go up and down on the z-axis two push-buttons are needed. Note that we only need one wire for the x and y axis because those are analog (the input can be read as a digital value between 0 and 1023 - a value smaller than 400 for example means right and a value bigger than 600 means left). Tag the wires with some tape and write their names on it so you won't forget which one is which! This is very important for later on!!!

Connect the GND wire to a GND port, the VCC to the 3.3v port and the z1 wire (directly from the joystick) to port 12. The additional wire is needed to connect the push-button to port 13. Please look at the picture of the breadboard because that is the easiest way to show it. If you don't have a z-axis you should connect two buttons like this. For now we'll use the breadboard for testing purposes.

Upload the code from codebender to your Arduino and test if you can move the dot and enjoy some 3D snake! I already have a case for my Arduino in my videos, but you should really wait with that until later ;). If you have to make some changes to the code (I tried to describe when you might have to in the code itself) you'll have to download the code and/or copy and paste the code into your own codebender project. Please let me know if you have difficulties with this!

If you cannot get this to work, first try checking you connected everything right (the cube but also the controller!), then check the connections of the cube are not touching. If you cannot seem to find the problem, please let me know in the comments! Also have a look at the code and make some changes if needed.

Step 14: The Controller: the Case

We will now start building the controller. Note that you don't really need to do all of these steps. If you don't have an old controller, your joystick does not fit in it, you just prefer a joystick without a case or whatever other reason you can skip this step. I  recommend you try to make at least some sort of controller case :). If you don't have an old controller you could for example attach the joystick and button to a piece of wood and create one yourself! This is even more in the spirit of DIY! Here I'll give you some general instructions and pictures of how I made my controller case.

I first opened my old controller and removed all the components that were inside it. Don't throw them away because you might need them later on! I tested whether my joystick would fit and it did! I removed some plastic because otherwise the controller couldn't close with my joystick inside it. This will be very specific for each controller case and joystick so I won't go into the specifics.

Solder two wires to the push-button. The extra wire from the GND joystick to one side and another loose wire to the opposite side (diagonally opposite, like we connected it before). This wire should be quite long - the same size as you want your whole cable from your controller to your cube to be.

Now you have to check where the push-button needs to be so it can be used with the controller case. I have these plastic cylinders sticking out, but since they were too high I had to cut off a piece. After you have the right length try to glue (plastic or silicon glue) your push-button to the case. You can also put it in a piece of styrofoam like I did and put that in the controller. I found the latter method the most easy and effective but this depends on your specific controller case of course.

If the push-button is put in it's place make sure that when you'll close the controller you will be able to click the button. I had a part to put on top of it (from the original controller) and had to make sure it would click when pressed, but also had place to 'unclick' when released.

Put all the components in place and close the case. Make sure the wires all come out of the controller. You can (and I strongly recommend) make a knot with the so pulling them won't break your connections you made inside.

I used some heat shrink to keep the wires together, but didn't put it around the whole length of my wires because it wass too stiff. If the case is closed you can put some silicon glue so pulling the wires won't break any of your connections.

Now you can solder some larger wires to your connections if needed (so your controller has space to move). You can choose yourself, just be sure to label them so you won't connect anything wrongly! Now you should have your controller completed and ready to connect to the Arduino! I recommend connecting and testing it now to ensure everything is still working correctly.

Step 15: The Case

Now we will start making a nice case for our cube! Because everybody will use something different for the case, it's quite hard to give instructions on this but I think everybody can figure out something creative by themselves if you got up to this point. I will give some general guidelines though.

[If you want to paint your case do it before you attach anything in it (especially the cube)!]

As I used a cardboard box it was easy to make some holes for the connections to go into. First find a suiteable place for your Arduino (a corner) and make a hole in your box for the USB connection. You can also make a hole for the power plug connection if you prefer to. I decided not to use it and so did not (yet) make a hole at this point in time. If the hole is in place try to attach your Arduino as good as possible. I used some tape but I'm sure you can come up with better and cleaner solutions ;).

Now we can connect the cube to the Arduino. On the back of the cube I attached the resistors and used the other side of the resistor to go through the carton. From the third layer I added parts of paper-clips to go straight down through the carton. You can use anything that transfers electricity though.For the simplicity and to prevent future difficulties, I wrote the port numbers next to the connections going through the carton.

On the inside of the cardboard box I used female-female jumper wires where I put the connections into. I had to fold the resistor legs because otherwise it would fall out of the jumper wire too easily. The benefit of working with jumper wires is everything can be disassembled easily for fixing or reusing later. But if you do use jumper wires, be sure it does not fall apart too easily! I then put a male-male jumper wire into the other side of each female-female jumper wire and connected these to the correct ports of the Arduino. Note you can use female-male jumper wires instead of what I did in two steps if you have some of those. I also taped some of the female-female jumper wires together four by four to clean up some of the mess in the cardboard.

For connecting the Controller I used a cable for multiple female-female jumper wires I took from an ancient Mackintosh :) (see pictures). I just connected the ports to one side and glued the other side of it into a hole in my case. Just remember to write down which wires of the controller should go in which holes (you can easily brick your Arduino if you connect them wrongly!).

As a last part, I put the battery in my case. You should first attach the toggle switch to the red lead. Then connect it to the GND and Vin ports of your Arduino. Its explained in great detail in this instructable:
If you want to power it through the Power Plug, you can check out this link:

Attach the toggle switch somewhere to your case so you can turn your game on!

If everything goes well, you should be able to power on your Arduino and start playing 3D Snake by toggling the switch.

Enjoy the game and don't forget to tweak my code and make it better :). Latest version of my code will always be on this codebender link:
Download it and make changes where needed.

If you enjoyed this Instructable, please consider voting for me in the microcontroller contest ;).

Microcontroller Contest

Participated in the
Microcontroller Contest