Connect with me on Linkedin

Sunday, 6 March 2016

LED CUBE 4*4*4

How to Build a Simple LED Cube 4x4x4 - Part 1




Project Features
  • Easy to build
  • Only two active components (apart from the LEDs, of course)
  • Includes detailed build instructions, schematics and software

Introduction
When I posted my charlieplexed 4x4x4 LED cube I did so in order to show an alternative implementation for such a cube. Its unique feature is that it only uses 9 controller ports. For some reason it gets a lot of interest from lots of people. And I had quite a few requests for schematics because people wanted to build it.

The problem with it is that it is only simple in terms of hardware requirements. But it is relatively complicated to understand its structure. So I thought I should try my hands on a really simple, easy to build LED cube and write a post including build instructions for people with basic skills in electronics.


Required skills and knowledge
Unfortunately I cannot cover every last detail about how to build electronic circuits. So this is what you should be able to do:
  • Soldering wires and components
  • Programming the flash memory of an AVR ATMega8L
  • Reading simple schematics

Components you need

LEDs: 
Obviously you will need 64 LEDs. I am using ultra-bright red 5mm LEDs for this project. They have a special shape called "straw hat" which give you a wide viewing angle.

Some LEDs have a very narrow viewing angle, like 15° for example. This means that most of the light goes straight up. But we want to look on the cube sideways. This is the reason why I chose the wide angle.




Choose what shape, size or colour you want. But if you use any other colour, this might mean that you need a higher operating voltage. My LEDs have a forward voltage (Uf) of around 2 Volts and the whole circuit will run off two AA batteries (3 Volts). It is also possible to run the cube with three AA cells to get brighter light. In that case the pulse current per LED will be around 50 mA which is fine.


My set of 100 pieces cost me under 3€. Value for money...


The only problem that I had with my LEDs was that their leads where relatively short. I simply didn't notice that, when I ordered them. But what it means is, that the cube got a bit smaller than intended. This is because I only use the leads to connect the LEDs to each other.

In general it is probably a good idea to get the gaps between the LEDs as wide as possible. Otherwise you have problems seeing all LEDs at the same time. But I didn't want to wait for another batch to arrive, so I went ahead anyway.

Controller:

Not much to say there. It is an ATMega8L in a DIP28 package. The "L" is important, because it means that the microcontroller can run at voltages down to 2.7 Volts. The version without the "L" needs at least 4.5 Volts. The Mega8 has got more than plenty of memory for generating a variety of effects or patterns - if you know how to generate them efficiently.


Prices vary depending on where you buy (2 € or more)

Driver:

The microcontroller's ports cannot drive more than one LED, so they need the help of drivers. I am using an ULN2803, which contains 8 separate drivers. It is available for under 0.50€.


Optional / Misc Components:

You will need some wire and quite likely a piece of perfboard. It might be a good idea to use sockets for the ICs. Depending on your power source you might need a battery holder. And finally, if you want to be able to change the firmware of the controller, a ISP connector will be useful.

The whole set of components can be bought for under 10€. Not too bad, I think. 

How it Works
Basically, the cube is wired like a standard multiplexed LED matrix in a 8x8 configuration.

Most people seem to base their design on a 16x4 matrix. This has the advantage that the LEDs are multiplexed with a 1:4 duty cycle, whereas my design has a 1:8 duty cycle. So my design is less bright. But I found that even my charlieplexed LED cube with a duty cycle of only 1:9 is very bright - because I use ultrabright LEDs.

Another advantage of the 16x4 matrix is, that you only need to drive 4 groups of LEDs, for which many people use discrete transistor stages. There are 8 groups in my design. So I need 8 drivers. But since the IC that I chose has got 8 drivers, that is no real disadvantage.

One advantage which my 8x8 structure has, is that I need only 8+8=16 controller ports, whereas the 16x4 design needs 16+4=20 ports. This means less connections (i.e. less soldering, less wiring) and it leaves you with some ports for further extensions (e.g. switches, a serial connection, etc.)

Also,  I think, that my version is a little easier to build. This is because it really isn't a 4x4x4 cube, but instead consists of two independent 2x4x4 "half-cubes". In my opinion, these are much easier to assemble. Find the details about that in the next paragraph.


Building the Cube Structure
This is the hardest part. Building the 3D structure will take about 3 - 4 hours, depending on your soldering skills and patience. There are several steps in this build. Keep testing it! Once the cube is assembled, it is next to impossible to repair anything. So keep testing! I know why I say this, believe me...

TIP: A simple way to test LEDs is with a multimeter set to "diode test". The multimeter will drive a small current through the LED, so it will light up dimly. Try all possible combinations of connecting columns and levels of your cube with your test leads. Do this after every step. 

But first:

Step 1: Prepare the LEDs
The LEDs need some preforming of theirs anodes. We start with a standard LED.


Use pliers to hold the longer wire (the anode).




Make a 90° bend.



 And finally bend it the other way.


This is the result.


And here is the reason why we have to do this. The LEDs will be stacked later. In each column of the cube, the anodes of the LEDs will be used for this. And without the preforming, it would be difficult to reach the anode below. Here is a photo just to demonstrate that point.Note that we don't stack yet.



So keep repeating this until all of your 64 LEDs are prepared for step 2. 
How to Build a Simple LED Cube 4x4x4 - Part 2
Building the Cube Structure 

In part one we prepared all LEDs for the assembly of the cube. Now, we start in earnest...


Step2: Build the groups

Each group consists of eight LEDs. Here they are:





You need a template to assemble the LED groups. My LEDs are 5mm, so I drilled a set of 8 5mm-holes in two rows with 4 holes each into a piece of wood. 



Insert 4 LEDs in the upper row of the template. Make sure all anodes point towards North-West.



Bend the cathode leads as shown. To give you a better understanding, the LEDs are slightly rotated in this photo:



Solder the LEDs to their neighbours.Insert the remaining 4 LEDs, again with anodes oriented towards North-West.





Again, bend the cathodes so you can use them to connect the LED. This is now the opposite way (basically all wires are bent clockwise).


Solder them to their neighbours so that the cathodes form a ring-like structure. Align anodes and make sure they are vertical. And then test!




Yes, it works...



Repeat this process another 7 times. You will now have set of eight groups of LEDs. Here they are, hinting at their later organisation.



Step3: Assemble the cube
Finally, all groups are connected. This step needs to be executed carefully. Make sure everything is level and aligned.

Insert the first group into the perfboard.




  
Then, add a second group. Put it on the perfboard in exactly the same orientation. Later, the columns of these two LED groups will be connected in parallel.



Now, you should do something that I only did after I had the cube finished: Connect the common cathodes to the perfboard. I wanted that connection to be invisible, so I put each vertical wire in the middle of its group. Better add them while adding LED groups, because access is easy now.

Here is a top view of the finished cube, with those vertical wires marked.

Now test those first two groups!

If everything works, start with the second level. Connect the two groups of the second level through their anodes to the corresponding anodes of the first level. Take your time! To make sure everything looks even, just start with two connections on opposite corners. If everything looks good, connect the rest.


This should be the moment for you to understand why half-cubes (i.e. groups of 8 LEDs) are easier to assemble. All solder locations are accessible from the outside. Of course it is a bit more difficult when you connect the second group of a level, because the first group gets in the way. But it is still easier that connecting a whole level at once.


Here is a side view of the next two groups.





Again, add the cathode wires and test. Add Half way there!

Keep going with the next level.


Test!

Finally, add the top level.





As mentioned before, the corresponding columns of the two half cubes have to be connected. Like so:



  
And this is the end result. The perfboard is cut so that the cube just fits on it. The rest of the board is used for the electronics.



Make sure everything works. Try all combinations of columns and levels. If you didn't know what multiplexing means: You are doing just that. Manually, of course. But that is what we have a controller for. And of course we can make the 8 LEDs of each group light up at the same time.

Congratulations! This is the hardest part done. What remains to be build is the electronics to control the cube. It is very simple. See 
part 3 
How to Build a Simple LED Cube 4x4x4 - Part 3
Connecting the Electronics
There isn't much, really. First, solder the microcontroller's socket to the perfboard, then the socket of the driver. I didn't use a socket for the driver initially, which I regretted later...

Here is a schematic.






If you do this in a clever way, there will not be too much effort in connecting the driver to the controller. Port B controls the common cathodes, so all port B pins connect to the driver inputs (green). PB0 is on the wrong side of the controller, but that is no problem. PB6 and PB7 are somewhat out of place. I simply used two wires to connect them. That won't win me a beauty contest, but the electronics will vanish under the cube anyway. Make sure that PB6 and PB7 are connected in the right order: It changes between the controller and the driver.

OK, start by positioning the two ICs/sockets. Note that there is an offset of two holes on the board. 



Solder.



Now, connect port B to the driver. Start with the conveniently placed ports 1 to 5. I didn't even use wires, but it might be easier to do so.



Now turn the board over and connect the two ground pins of both ICs.



Next, connect port 6 and port 7 using wires. Note that the yellow and white wires are changing order.



On the bottom side, connect port 0 to the driver (here marked in yellow) and connect analog and digital ground on the ATMega8 (black wire, white outlines).



Also connect digital and analog Vcc pins (red wire, pins VCC and AVCC) and GND pins.



Finally, add a red and a black wire to the ATMega's Vcc/Gnd pins do connect your battery. Done!






Final Assembly
Now we have to connect cube and electronics. For this we need another sixteen wires. Let's start with the common cathodes, i.e. the LED groups. So far that vertical wires from each group just end on theboard. Now we need to connect wires to each of them.



And we connect them to the driver output. Start with the top right group, then connect the top left group. Continue with the second level. Keep alternating between right and left.



Do exactly the same thing with the bottom layers (layer 3 and 4). Alternate between right and left as shown.



Now connect the columns of the cube. Here is how. This time there is no driver; the columns of the cube are directly connected to the microcontroller. Here is a bottom view of the cube. Every column is shown with their respective controller pin.


Again, begin by soldering wires to the "feet" of the columns, i.e. the anodes of the LEDs.


Connect the LEDs in the right order. Start in the corner and connect all four LEDs of one row.



Then connect the LED anodes of the second row. Here, two columns are connected.



And the last two anodes are done.



If your wires aren't too long, you can flip the electronics over and hide the wiring.



I fixed the two parts of perfboard to each other with a few pieces of wire. I soldered them to one board, inserted threaded them through the other board and just bend them at 90°. See the picture below.


And that is the hardware completed. In the picture below, there is a pigtail connected to the ISP pins of the controller. I used that to program the controller while I kept adding new patterns. Depending on your means of programming, you might or might not need this.




So, all that is left is the software. A brief description and the final part of this project will be posted in 
part 4.
How to Build a Simple LED Cube 4x4x4 - Part 4
Software
The software is derived from my 
earlier LED cube, the charlieplexed version. Of course the "hardware layer" had to be changed, because physical control of the LEDs is different. But the rest is very similar.

To add a bit more structure, I partitioned it into several files. There is the main routine (in LEDCube444.c), the file HWControl.c for low level hardware support and a collection of software routines to generate procedural patterns called PatternTools.c.

The patterns or rather pattern sequences are in individual files. This has the advantage that they can be included optionally or rearranged. You can also add your own creations.

If you are not interested in modifying anything, just skip the details. But if you are, here it goes.

LEDCube444
It contains the main routine.First it invokes the initialisation routine, then it displays the HW check pattern sequence once and finally it starts an endless loop displaying all the patterns. The patterns are #include'd so it is very easy to manage them.

HWControl
This file contains two important sub-routines. First, there is the "ioinit" routine. It initialises the IO ports of the controller and sets the timer forthe multiplexing interrupt. Finally, it enables interrupts.

Second, there is the interrupt service routine (ISR). It switches throughthe LED groups (i.e. the half-levels) and sets the appropriate anode values. It gets these values from a pre-calculated array, so there is little processing in the ISR. As a small addition I added a dimming function. The global variable "dimmer" can be set to values between 0 and 32 to set the cube's brightness.

A secondary task in this routine is the implementation of an egg timer. It can be set to a certain value and then will count down in the ISR. This is a simple way to create delays. Quite often in sequences, the waiting time is always the same, so there is a global variable "current_delay". If set, all you have to do is invoke the "standard_delay" routine in order to get a fixed delay.

There are a few other routines in this module. They are secondary display functions. Most important is the calculate_IO routine. It translates the cube array values to port values for use in the ISR.

The cube array ("LED_Cube") contains the LEDs to be set. Each LED corresponds to one array element. The top, right, front LED is LED0. The one behind is LED1, after that follow LED2 and three. LED4 is the top, front, second from right LED. This continues in a regular sequence until LED63, which is bottom, back, left.

To help writing compact code, there is a "display" routine. It invokes the calculate_IO and then the standard_delay routine. And then there is the "do_plus" routine. It is the same as the "display" routine but also clears the cube after the delay. It is then ready for the next LED setting to be generated.



PatternTools
My pattern sequences are more or less generated by algorithms rather than just read back from memory somehow. To that end I created a set of tools to create certain elements.

The most obvious is a "set_LED" routine. It is almost trivial. The LED number is the argument and it is used the set the corresponding cube array element.

Far more interesting and powerful is the "set_pattern" routine. Duringmy first attempts to generate patterns, I found that a lot of elements like a fully set plane or column are actually regular patterns in the cubearray. As an example, setting the top plane means just to set the first 16 LEDs. To set the second plane you have to set 16 LEDs again, but this time with an offset of 16.

In order to set a vertical plane, you have to set 4 LEDs in sequence, then skip the next 12 LEDs. Then you have to repeat this 4 time.

The "set_pattern" routine has therefore 4 parameters: The offset ("start"), the no. of LEDS to be switched on ("LED_on"), the number of LEDs to be off ("LED_off") and a "repeat" value.

Based on this algorithm, there are many other pattern generating routines, like setting columns in various directions.

Finally, there is a support routine to generate "pseudo_random" numbers, via a LFSR.

Pattern Sequences

These are located in a sub-directory "patterns". Take a look if you want to write your own sequences. If you are not that firm, you canstart by modifying the existing patterns. The loop variables i ,j and kare available for the pattern generating algorithms.

Programming the Controller
The software was written and compiled under Atmel AVR-Studio. The download package contains a hex-file in case you want to quickly get the cube working, without compiling the firmware first. 

There is only the flash ROM to write. EEPROM is not used.

The fuse setting is default, but the internal RC clock oscillator is set to 4 MHz. In AVR-Studio the fuse setting is reported as:
HIGH: 0xD9
LOW: 0xE3

Further Possibilities
The patterns shown in the youtube video take less than 50% of the flash memory of the ATMega8 controller. So first of all it would be possible to add more pattern sequences. Then, there are still a few pins available. So, you could add some buttons and create some kind of interactive display. 

Since the serial RX/TX pins are left over deliberately, it would bepossible to use some kind of remote control scheme to control the cube.

Lots of possibilities to experiment! 

Last Words 
That is it. If you build this cube, I would appreciate it if you could let me know. I am always interested to learn if this post inspired anyone. And if you have proposals to improve it, do let me know.

In case you have questions, either leave a comment or get in touch via email. But please give some kind of name or alias. Otherwise it can get confusing...

Oh and the software, as with all my projects is available through the 

Have fun


No comments:

Post a Comment

Featured post

Iron Man Infra Red palm Cannon

simple Atmega8 kit a buzzer and an infra red sensor and blue LED C Program #include<avr/io.h>         //This is...