Programming for a custom LED matrix / display

I'm planning to create a custom LED matrix / display for a children's museum exhibit, and am struggling with developing the code to drive it. First of all, I am using 4051 chips as demuxs to control the rows of this display (there are 9 total rows, only eight can fit on the chip), then I will connect the columns all together. The idea will be to send a PWM signal to the row that contains the LED I want to turn on, then bring the corresponding column to LOW to activate just that one LED.

This is shown in the following schematic:

I'd like to display custom animations on this display, but am having trouble implementing the data structures I had in mind to do so. I was hoping to create 2D arrays, wherein I can describe an entire animation using frames (first dimension) and LEDs that should be on in those frames (second dimension). However, this is not easy (for me) in C / Arduino, and I'm having problems; mainly this is because my second dimension has different lengths, i.e.:

byte pattern1[][] = {{21,1,3},{2,4,5,6},{1}};

Arduino / C really does not like this.

Can someone explain how I should represent (and display) animations on my custom LED matrix?

Well, I'm going to need to build this project in about one week, I don't know that I have time to order all new parts (I already have the 4051s) and re-do the schematics / designs. Plus the code for the MAX7219 looks a bit more difficult than what I can understand right now.

I was thinking of using 2n2222s to switch a higher current power supply for the LEDs (one transistor per row / output of demux) so that the demux just outputs a control signal to the transistor, which supplies the current to the LED. Would that work?

I have tried using a QueueList (from libraries page) to make up linked lists of arrays, but again, they cannot work with arrays :frowning:

Gotcha, thats what I was hoping for :slight_smile: Got bags of both those components laying around, so it'd be great to use them!

You may be right about the simple programming solution - I just wanted to make things as compact and efficient as I could. I will actually be controlling two matrices (physically arranged in a sort of hockey stick shape): one is 9x2, the other is 7x3. They are standard 5mm LEDs and are rated at between 20-35mA (depending on color).

So my desire to make the code efficient can be seen in the following scenario: say I want to make a light 'chase' the perimeter of the 7x3 array. That is 16 frames of animation, lighting up one LED at a time. If I explicitly defined arrays for every frame, I would have many more 0s than 1s, meaning lots of memory that is being used to store useless data. If I could use only the amount of memory I need, that just gives me that much more space to put other animations / code into. See what I'm saying?

I'm leaning towards that approach right now, just for the sake of getting something done, but I still want to implement something more intelligent in the future.

for a chase animation, why would you even consider storing individual frames of an animation? that type of animation is clearly best implemented with a simple for loop...

then again, if you use some smart mapping, you can store all the data for one frame of your display with only 5 of bytes of memory, since you've got a maximum of 39 individual LEDs to control. that makes the frame data REALLY hard to read or edit by hand, though.

OK, I've decided to define all ones and zeros explicitly in arrays. Maybe I'll revisit it in the future, but I don't have time for a better solution right now.

I wanted to be sure of my schematic, though, before proceeding. Here is how I was planning to connect my LEDs to the 4051s:

The plan was to send a PWM pulse into the 4051 and select the appropriate output using the selector pins. The PWM should switch on the transistor, supplying +5V to a row of LEDs. Finally, I would pull one of the anodes to LOW using the Arduino, turning on one LED. Repeating this same structure for each of the rows I have (max of 8).

Should I add anything to this schematic, maybe some resistors? Or will this work as I intend it to?

I think you'd have better luck using a PNP transistor, this might work.
You need to get ~2V at the anode of the LEDs (or more, depending on what you're using).

How much current can a 4051 source? The BJT wants to amplifly that, by a 100 or so, so if you want 8) ( 8 ? ) LEDs, you will need 160mA thru the transistor.

I have a bag of 2n3906s, I could use. But why? I thought the 2n2222 would connect the LEDs to 5V?

The 5V at the collector of the 2n2222 is an external power supply, which can supply up to 1A. I was thinking all of the current drawn by the LEDs would be drawn from that external power supply, not the 4051. Is that wrong?

"I was thinking all of the current drawn by the LEDs would be drawn from that external power supply, not the 4051. Is that wrong?"

No, that is correct. I was more concerned about the 4051 being able to source enough current into the transistor to turn it on enough for the current flow needed to turn on the LEDs.

Having a logic level MOSFET here could be better, its easier to turn on with a low-current capable voltage source like a 4051.

Oh, I see. Could you recommend a logic level MOSFET to use here? Looking at the 2n2222's datasheet, it seems to indicate that the base can switch ON with only 0.3V @ 15mA, so a logic level "high" should more than suffice (I have used 2n2222's experimentally to control motors and LEDs using PWM signals generated by PICs, so I think they would be OK here).

The only things I am unsure about right now are whether or not I should add resistors to both the base of the 2n2222 and the collector of the 2n2222 - I don't quite understand why, but I've always heard it was 'good practice'.

Furthermore, the way I was planning on ensuring that an LED is off when its supposed to be off was by setting its 'column' line to HIGH, so that there is no path to ground. My concern is in the case when both a row and a column are at digital HIGH: will the LED blow up? Is there another way I can ensure that my LEDs do not get 'stuck' on?

Exactly my concern - turns with 0.3V, but needs 15mA thru its internal base/emitter junction.

The reason you add resistors is to limit current flow.
With no resistor on the base, the base pretty much looks like a diode connected to the emitter. Here you have a resistor from the emitter to the LEDs to eventually low, so I'm not sure how that will play out.

Do you a surface mount recommendation or a thru hole recommendation?
Digikey has good filtering.
search for p-channel mosfet.
then single fets (or maybe arrays, depending on your planned build configuration)
then in-stock
then logic level
then thru-hole or surface mount
that should narrow the list.
cick on sort by price, then start going to find a part with low Rds (down in the mOhm range), and a price/package you can work with.


The actual base current will be less than one milliamp as the gain of the transistor is over 50 and the collector current supplies most of the 50 mA for the LEDs, but I would prefer to have the LEDs and their resistors in the collector circuit (common anode connected ) and save the half a volt base bias. ( minus the saturation voltage to be pedantic )

I don't know what colour LEDs you are using, but it sounds like there are different ones for the range of current you mention.

Some LEDs have 3 volts forward drop, which if added to the base/emitter drop of say .6 volt, only leaves you with 1.4 volts to drop across each resistor ( 56 ohm at 25mA ) which is fine, but its better to have more voltage to play with ( as with the common emitter configuration ) to match the differences of forward voltage and brightness in the pair of LEDs .

A 1k8 resistor from the chip to the base of the 2N2222, ground the emitter, and each LED in series with its own resistor ( again to separate V forward differences in the LEDs )

Incidentally, you wont perceive too much difference in overall display brightness by running the LEDs 10% less than their max current.
The reliability will depend on the heat of each LED, which can vary with local conditions, sunshine for instance, and how well ventilated the LEDs are.

I mainly use red wide angle LEDs with a recommended current of 20mA, but I run them at 18mA.
Apart from during initial testing after soldering, where some have failed, in the last 10,000 LEDs used I have not had one failure reported ( I might be sorry I said that :slight_smile: )

It is a far different story with orange ones from my supplier, I don't use orange anymore !

Boffin, how'd you make out getting your thumb dial swithes read? Success in the end? I was just not following what you were trying to do there.

@ Crossoads I was up till 3am soldering in the 19 shift registers, it seems to be working ok with the 2 Artduinos hooked up, but I want to see it in real action.

There is somethng funny with the Shiftout function, but I will sort that out later today.

I will post some pics a bit later, the wiring in the remote is quite something !

@ Crossroads. I got the BCD switch scanner going, and have posted a photo on the original thread at

Saw the pics, looking good.
What software did you use for the PCB? That's pretty good size, folks have been asking recommendations for larger boards like that.

Post some pics of the remote also.

I have been using Sprint Layout for pcbs for a while now, it was only about $40 I recall, I didn't like auto-route on most packages I tried, so routed manually anyway, so this has been OK.
Its very basic in that when you move a component the trace doesnt move with it, but I have since downloaded which looks interesting, and has some nice reviews .
I havn't had a chance to even try it yet with so many projects on the go, but I like the price - its FREE.

If anyone has tried it I would like to hear how it works, as I hate wasting time learning a new program that will not work for me.

I hope to finish the remote this morning, and will put some pics up.

I hope to also finish a 4 digit version of this today, it uses the same scanning idea, but doesn't use the 4017s, just 4 pins of the micro to select the bcd switches in turn..

Kicad also looks interesting, if anyones tried it?

Lot of folks were praising kicad, haven't tried it myself.

I just tried designspark under Ubuntu 10.04.2 Wine 1.2. It crashed after about 5 min when I was attempting to move a window splitter bar. I have a dual screen display so perhaps my setup is not well tested...

Thats nice to know Andrew, I have downloaded the Kincad, and will check that when I catch up