Sunday 26 February 2012

Arduino - How to power LED matrices using shift registers: Part 1

I intend for this to be a comprehensive guide to controlling LED matrices, there are some guides out there (also as blogs) but it all depends on what you're doing.

If you're looking for a quick step by step guide, skip to part 2, if you want to know everything about what you're doing, read this before part 2.

Note - I have only been doing Arduino stuff since July 2011 and so I still consider myself to be a novice at it. While I have completed what I set out to do, there may be some things which I may have done wrong or are bad practice. If you have any questions please feel free to email me or ask in the comments below.

Background
LED matrices can be used for many things, I have chosen to use it to represent the bead sort algorithm (this is for my dissertation, I will probably post about that at some point in the future). This is where beads are sorted using the properties of gravity, and is handy because each LED can represent 1 bead. Here is a gif I found on google images that show you how it works: http://mgs.spatial-computing.org/ImageGallery/EXEMPLES/BeadSort/anim_bead_julien.gif

The equipment
Unfortunately the components for any build like this will be a little expensive, but it depends how you do it.
Here is a list of my equipment:

How the LED matrices work
When I first started using them, it was a bit of an alien world to me so hopefully this part will help. Remember the details for the matrices will be different for each model, check the matrix datasheet for all the necessary information. The matrices I'm using are a common cathode 24 pin matrix. The common cathode part of it means that for a row to be on, it has to be set to LOW (cathode-negative). For this particular matrix pins 1-4 and 21-24 control the rows, pins 5-12 control the columns with green, pins 13-20 control the columns with red. The mappings are below:

How the shift registers work
Shift registers are still a bit of an enigma to me. I don't know exactly how they work but know how to use them. The shift register I'm using (MAX7219) is meant for LED control. It will take serial input (bits, one after another) and give parallel output (all the bits, all at once). It has 24 connections in total. Rather than explain each one in detail, this guide on the Arduino website covers pretty much everything: http://arduino.cc/playground/Main/MAX72XXHardware

The guide above also tells you how to choose the correct resistance value of Rset (in my case 30-40KOhms).

Arduino program
My source code will be as a link on part 2, but I will explain a bit about the program here.

My program uses the LED control library (explained below) so that needs to be imported.

One of the first lines is LedControl lc=LedControl(2,3,4,1); This basically means create a new instance of LED control, using pins 2,3 and 4. The number 1 corresponds to the number of devices (which will always be 1 for my program).

Next I have the step definitions. Each step consists of and array of 48 bytes, as I have 24 steps thats 24 * 48 bytes or 1.125KB which is a heck of a lot to hard code. Luckily I have created a program to do that for me, but that's another subject. It is likely that anyone following this will be using far smaller "steps" and fewer devices.

After the step definitions there is a small bit of code denoting the delay between each step, this is mainly so I can simulate a bit of acceleration by reducing the delay per step.

Following that are the step assignments. In brief these assignments tell the program which parts of the array to assign to which LED control instance.

For my particular case at the end of each set of steps I wipe the LEDs and start again.

IMPORTANT - There must always be a delay at the beginning/end of your step definitions, I'm not sure why but it messes up otherwise.

And that's it :)

Using the LED control library
Firstly make sure you have the Arduino software running and working (version 1.0 was released not too long ago).

I have used the LED control library, details of which can be found here: http://www.arduino.cc/playground/Main/LedControl

You can also download the library as a .zip file from that link, the link to the file is in the section "Sourcecode and download".

Make sure the Library files go to the "Libraries" or "Lib" folders in the Arduino program.

To use the library, go to Tools -> Import Library -> LED control. You may have to close and open the Arduino program again if it was open when you added the library files.

Now you're set to go :D.

I will gladly try to answer any questions so please feel free to comment/email me with any queries.

Now have a look at part 2

Jake



1 comment:

Did you find this post interesting?