Electronic – arduino – Multiplexing LEDS and Multiple Output Pins

arduinoavrledmultiplexer

Ok…..i've been trying to understand something (and trust me, to most people it's a VERY newbish question)

So I see people all the time "multiplexing" LEDS. using just a few Pins.

Lets say for an Arduino that has….. 5 I/O pins for example, apparently this can control 20 LEDS (or so i've been told) using Multiplexing…but…..what Im confused about is that this is all controlled by 5 pins. Maybe im getting Multiplexing wrong but how exactly does this happen?

Also as iv'e been reading assembly tutorials for the AVR I notice that each Port has 3 registers, PORTB, DDRB and PINB, do these correspond with LOW/HIGH/OFF…..or is this entirely unrelated?

I guess I don't understand the whole idea of Multiplexing, I've read tutorials…..but I don't understand exactly how/why you hook them up, and for that matter where (and how much) to use resistors for, I know using certain combinations of High/Low/Input you can achieve a pretty awesome LED matrix. but How do I determine this. Im sure you would have to use transitors though right?

Sorry for the confusing question, it's even harder to ASk the question when Im not very sure about it.

edit: Also I understand Shift Registers/Multiplexing (at least somewhat) in terms of "Truth tables" but I dont understand actually how to implement it. Maybe anyone could recommend a book on using Transistors and the like with Electronics, since some of the basic books really only go over Capacitors/Resistors

Best Answer

I started writing about demultiplexers and calling the multiplexers, so the error should now be fixed. What you need to easily control multiple LEDs are demultiplexers, or use of multiplexers in reverse.

Here's a simple version: Imagine you have a telephone. The telephone is (in our simple world) connected to a switchboard and the board is connected to other telephones.

When you dial a number, the board recognizes it and connects your telephone to another telephone and that's how connection is made. You only need one telephone and one switchboard to connect that one telephone to multiple other telephones.

Demultiplexers work in a similar way, except here the data and address lines are separated.

So you have a hardware demultiplexer. It has address pins, input pin and output pins.

Your microcontroller also has output pins. In the LED example, you can connect one output of the microcontroller to input of the demultiplexer. Using that one pin, you can control one LED at a time. If you connect the other output pins of the microcontroler to the demultiplexer, you can set the address. To get back to out telephone example, you use the microcontroller output pins connected to demultiplexer address pins to dial a number. So you send a signal to the demultiplexer and the signal is actually a binary number. The output pin of the demultiplexer with that number is then connected to the demultiplexer's input pin and you can send data from the microcontroller to the device connected to demultiplexer's output. This itself isn't very useful, because the demultiplexer is then a fifth wheel.

Here comes the interesting part: When you're done using the device connected to that particular output, you can the use address pins to select another output pin with another device connected to it. This way, we get a simple switch, so we can select the device to which the microcontroller will communicate.

Now let's get to the LED example. You have a microcontroller with 5 output pins. Let's say that you have a demultiplexer with 4 address pins, one input pin and 16 output pins (Mo0 to Mo15). Let's also say that you want to control 16 LEDs. So you connect each LED to the output pin of the demultiplexer and you connect 4 output pins (Here called A0-A3) of the Arduino to the address pins of the demultiplexer. That leaves one pin for the demultiplexer input pin (here called Ao) .

Now let's say that you want to light up a LED. You can just power up the Ao pin. In this case, the demultiplexer will read the signal sent by the A0-A3 pins. Since they are low, it will interpret that as 0000. That is the number of pin Mo0, so it will connect Mo0 to Ao. Now let's say you want to turn on another diode. You can do that by setting A0 pin to high, so you'll get 0001 and demultiplexer will connect pins Ao and Mo1. This way, you can connect Ao to any of the Mo pins.

OK, that's nice, but how do you control all LEDs at the same time? Well, you don't. Instead, you'll exploit limits of human perception. If you turn an LED on and off fast enough, people won't notice.

For that you'll need a program which will cycle through selected output pins at a certain rate. The program should count up the A pins like binary numbers and that way you'll select which LED to control at each particular moment.

As for the registers you mentioned. Well, they are related, but at a different level. My advice is to learn first how to control one LED. After that learn how to control several LEDs connected to same port. When you learn that, you'll know how to set address pins for a demultiplexer high or low (it's same as turning on or off an LED) and since you already know how to control an LED, you'll be able to control the currently selected LED.

Here's a picture of a demultiplexer. The GEN on the left is input signal. The GEN on the top controls if the multiplexer is on or off. In a real world project, you'd do this using power connectors. The 3 GENs on the bottom control address pins and on the right are output signals. When the address lines are low, output number 0 is selected.

pin 0

Here we have pin 1 selected:

pin 1

Here we have pin 2 selected:

pin 2

And finally here we have pin 3 selected:

pin 3

Same results should be obtainable using a multiplexer and connecting the microcontroller's output to multiplexer's output and using multiplexer's input pins as output. Of course, before doing that datasheet should be checked to see if such thing is possible.

I just now noticed that you mentioned resistors. As I mentioned, before controlling large number of LEDs, first learn to control a single LED.

This one depends on microcontroller, but for AVR, when you set a pin to high state, it will be set to 5 V. Also maximum current through each pin should be limited. I used for example ATmega 162 and it can provide up to 200 mA for all pins and up to 20 mA for each pin. Another important thing is the LED current use. Different types of LEDs need different amounts of current to achieve maximum brightness. The second important characteristic of a LED is it's voltage drop. I used for example a 20 mA, 3.6 V LED. In this case, I need to limit current to under 20 mA. To do that, I used a resistor. There's a nice calculator here, which will tell you what type of resistor to get for the LED. Also, when choosing the LED current, keep in mind that you're looking for the lower of the two supplied currents. You shouldn't exceed current of microcontroller pin and you shouldn't exceed current of the LED itself.