# Controlling LEDs with a Demultiplexer

## Introduction

In this project, we will demonstrate how to use a demultiplexer (also referred to as a demux) to selectively power one of four LEDs. If you are unfamiliar with demultiplexers, you can read more about them by following the red tab in the related materials section to the right.

##### Before you begin, you should:
• Understand current limiting resistors.
• Know how to wire an external LED.
##### After you're done, you should:
• Understand how to use the SN74HC139 Demultiplexer

## Inventory:

Qty Description Typical Image Schematic Symbol Breadboard Image
1 220 Ω resistor
4 LED
1 SN74HC139 demultiplexer

## Basic Theory

#### Getting Familiar With the SN74HC139 Demultiplexer IC

In order to complete this project, you will need to be familiar with the demultiplexer provided in the Analog Parts Kit. The SN74HC139 contains two demultiplexers; pins 1-7 on the left control the 1st demux, while pins 9-15 control the 2nd demux (see Fig. 1). For this project, we will only need to use one demux, so we will ignore pins 9-15.

## Figure 2 Note:

When working with the SN74HC139 demultiplexer, remember the IC contains two identical demuxes. One is controlled with pins 1-7 while the other is controlled with pins 9-15. The logic table from Fig. 2 shows how to specifically drive pins 1-7. This table will also work with regard to controlling pins 9-15.

Referring to Fig. 2, note which logic is used to control the demux. Notice that when pin I (1G) is driven HIGH that the selection pins have “X”s (“don't cares”) beneath them, and all of the outputs are HIGH. This is because the SN74HC139 is an active LOW circuit, which means is if you drive the input LOW the circuit will be active, but if you drive the input HIGH you will disable the circuit. You can tell that this demultiplexer is an active LOW device due to the bar over 1G. This bar indicates that the input pin 1G is configured to respond to a LOW input. Another clue that this demultiplexer is an active low circuit is the “X”s shown in the table. Those “X”s represent don't cares. They tell us that the value of a particular input pin, whether it is HIGH or LOW, has no effect on the output. So when 1G is HIGH, pins 1B and 1A have no effect on the output. Hence driving 1G HIGH disables the demultiplexer.

## Step 1: Assembling the Circuit

Before we begin wiring the circuit, we need to consider how we will set it up. Since our demux is active LOW, the output pin we select will be driven to ground. This means we will always want the negative side of an LED to be connected to one of demultiplexer's output pins. Normally when working with multiple LEDs, you need a current limiting resistor for each. Since the demux will only activate one LED at a time, we can simplify our circuit and use one current limiting resistor for all four LEDs.

Next, it is important to consider how we will order the LEDs on the breadboard. The point of using the demux is to light up a specific LED. So we will place and name our LEDs in increasing order from left to right. The leftmost will be the 0th LED and rightmost will be the 3rd LED, as depicted in Fig.3. This order will match up with the pin naming convention used for the demultiplexer (Y0–Y3).

#### Assembly Steps:

NOTE: The text coloring in this section does not correspond to the MPIDE keyword color coding used elsewhere in the document.

1. Connect the chipKIT™ 3.3V source to the bottommost power rails.
2. Connect the chipKIT's ground pin to the breadboard's blue ground rail.
3. Place the SN74HC139 demux IC so that it straddles the breadboard's valley. Make sure you position it so it is exactly three holes from the right, as shown.
4. Connect the bottom 3.3V rail to the Vcc pin on the demux.
5. Add a second 3.3V rail by running a wire from the demux's Vcc to the top red power rail. Adding another 3.3V rail will help keep our circuit organized.
6. Connect the bottom and top blue ground rails of the breadboard.
7. Run a wire from the demux's ground pin the uppermost blue ground rail.
8. Connect chipKIT pin 26 to demux pin 1G. See Fig. 4 for demux IC pinout.
9. Connect chipKIT pin 27 to demux pin 1A.
10. Connect chipKIT pin 28 to demux pin 1B.
11. Place two LEDs oriented and spaced as shown. The anodes of both LEDs should occupy the node. This node will be highlighted in orange and referred to as Node A.
12. Place two more LEDs oriented and spaced as shown. The anodes of both LEDs should occupy the node. This node will be highlighted in yellow and referred to as Node B.
13. Place a 220 Ω current limiting resistor between the pairs of LEDs. Make sure one end of the resistor is connected to the top 3.3V rail. The other end of the resistor should be connected to the node directly below. This node will be highlighted in green and referred to as Node C.
14. Connect Node A and Node B to Node C.
15. Run a wire from LED0's cathode to demux pin 1Y0.
16. Run a wire from LED1's cathode to demux pin 1Y1.
17. Run a wire from LED2's cathode to demux pin 1Y2.
18. Run a wire from LED3's cathode to demux pin 1Y3.

The schematic for the circuit can be seen in Fig. 5 above. Note that the demultiplexer in the schematic uses the actual IC pin names, rather than generic demux pin names. Also, the Vcc and GND pin connections to the demux are omitted from the schematic for simplicity.

## Step 2: Writing the sketch

The code for this sketch will be very simple. First we will setup chipKIT pins 26, 27, 28 for output. We will constantly drive demux pin 1G LOW to keep the demultiplexer enabled. Next we will write a function called selectLED(). It will use a switch statement to select one of four cases. Each case will drive demux pins 1A and 1B according the function table seen in Fig. 2. Finally, inside loop() we will call selectLED() a few times in conjunction with delay(). This will generate a test pattern which cycles through the LEDs from left to right then pauses. The implementation for this sketch can be seen below.

					//Define Demux Control Pins
int demuxPin1G = 26;
int demuxPin1A = 27;
int demuxPin1B = 28;

void setup()
{
//Enable demux control pins for output
pinMode(demuxPin1G, OUTPUT);
pinMode(demuxPin1A, OUTPUT);
pinMode(demuxPin1B, OUTPUT);
digitalWrite(demuxPin1G, LOW); //Enable Demux
}

void loop()
{
//LED test pattern
selectLED(0);//Select LED 0
delay(100);
selectLED(1);//Select LED 1
delay(100);
selectLED(2);//Select LED 2
delay(100);
selectLED(3);//Select LED 3
delay(500);//Long Pause

}

void selectLED(int value)
{
switch(value)
{
case 0://Drive Demux pin 1Y0 LOW
digitalWrite(demuxPin1B, LOW);
digitalWrite(demuxPin1A, LOW);
break;
case 1://Drive Demux pin 1Y1 LOW
digitalWrite(demuxPin1B, LOW);
digitalWrite(demuxPin1A, HIGH);
break;
case 2://Drive Demux pin 1Y2 LOW
digitalWrite(demuxPin1B, HIGH);
digitalWrite(demuxPin1A, LOW);
break;
case 3://Drive Demux pin 1Y3 LOW
digitalWrite(demuxPin1B, HIGH);
digitalWrite(demuxPin1A, HIGH);
break;
}
}