Geeks With Blogs
Joaquin Jares Just another geek's blog

In my previous post I used a lot of transistors (not very successfully, I might add) to drive 4 LEDs with 2 digital GPIO pins from a Netduino. I mentioned that there are Integrated Circuits (ICs) that may be used for that. In this post I’m going to use one of those ICs (specifically, a 74HCT138) to do the exact same thing. First, here is how it looks:

Very simple prototype of the use of a 74HCT138


The code I’m using is exactly the same as before. I have the Encoder class, which will receive a single int value an encode it in binary with as many pins as I provide:

1 public class Encoder 2 { 3 private OutputPort[] outputs; 4 5 public Encoder(params Cpu.Pin[] pins) 6 { 7 outputs = new OutputPort[pins.Length]; 8 for (int i = 0; i < pins.Length; i++) 9 { 10 outputs[i] = new OutputPort(pins[i], false); 11 } 12 } 13 14 public void Reset() 15 { 16 foreach (var port in outputs) 17 { 18 port.Write(false); 19 } 20 } 21 22 public void Encode(int code) 23 { 24 for (int i = 0; i < outputs.Length; i++) 25 { 26 outputs[i].Write((code & ((int)System.Math.Pow(2, i))) > 0); 27 } 28 } 29 }

This Encoder will be useful for the next few posts too. I declare the Encoder in my Baseboard using digital outputs 0 and 1 (line 9 in the code):

1 public class BaseBoard 2 { 3 public Button Button { get; private set; } 4 public Encoder Encoder { get; private set; } 5 6 public BaseBoard() 7 { 8 Button = new Button(Pins.ONBOARD_SW1); 9 Encoder = new Encoder(Pins.GPIO_PIN_D0, Pins.GPIO_PIN_D1); 10 } 11 }

And in my program, I simply encode the next number when the switch is released:

1 public class Program 2 { 3 private static BaseBoard board; 4 private static int code; 5 6 public static void Main() 7 { 8 code = 0; 9 board = new BaseBoard(); 10 board.Button.Released += new NoParamEventHandler(Button_Released); 11 12 board.Encoder.Reset(); 13 14 Thread.Sleep(Timeout.Infinite); 15 } 16 17 static void Button_Released() 18 { 19 code = (code + 1) & 3; 20 board.Encoder.Encode(code); 21 } 22 }

And that’s the code. I have a longer explanation of how the code works in my previous post if you’re interested. What I’m interested in this post is the hardware side of things.

The 74HCT138 is a 3-to-8 DEMUX, Inverting. That means that it can turn 3 digital outputs into 8 digital outputs. Inverting means that the encoded output will actually be LOW, not HIGH as would be expected. That means that if you encode, for instance, a 4, the 4rd pin (Y3, as this is zero based) will be LOW while pins 0, 1, 2, 4, 5, 6 and 7 will be HIGH. For the purposes of this demo, Inverting and not Inverting are exactly the same. I’ll show you how to wire the non-inverting version at the end of the post. This circuit has also a fantastic characteristic: it has 3 enable inputs. The circuit will only activate if E1 and E2 are LOW and E3 is high. This is used for chaining, which is something I’ll explore in a later post.

So let’s go to the wiring. The project in the video looks something like this when wired:


It doesn’t show clearly in the picture, but the marking of the chip is facing left. The chip has 16 pins, 8 per side. The right side of the chip holds Vcc and pins Y0 to Y6. The left side holds A0, A1, A2, E1, E2, E3, Y7 and GNC. The pins marked as Yx are the encoded outputs. The ones marked with Ax are the selectors (in this case, the value to encode) and the ones marked with Ex are the enablers. If you check the datasheet you’ll notice that E1 and E2 have a little bar over them. That means that the input needs to be LOW for them to work.

The wiring is pretty straightforward. Vcc is connected to Netduino’s 3.3V and GND to Netduino’s GND. A0 is connected to D0 and A1 is connected to D1. A3 I’m not using (I only have 4 LEDs). I could use it if I where connecting up to 8 LEDs. Since I’m not using it, I have to connect it to GND so it’ll always be LOW. E1 and E2 are also hardwired to LOW and E3 is hardwired to HIGH. Simple, isn’t it?

As per de LEDs, since I’m using an inverting IC I have to do something that’s counterintuitive: the LED’s are connected to the power line in the breadboard via their anode (the long leg) and to the output pins in the IC via their cathode (the short leg). That is, the IC is working as ground for the LEDs. When the output is HIGH, the LED will not turn on, when it’s LOW it will.

Let me show you how this works in a 74HCT238:


If you look closely, you’ll notice that the only difference is that the leds are connected to ground via their cathode and to the IC via their anode. That is because the 74HCT238 is not inverting. When you encode a 4, the 4th pin will be HIGH and all the other pins will be LOW. Even the enablers work exactly the same in both ICs. This difference will let us connect 64 LEDs with only 2 chips. I’ll do that in a later post.

A single IC can turn 3 digital outputs into 8 exclusive digital outputs. But these ICs can be chained in many many ways, and I’ll explore the ones I thought of in the next few posts.

Off topic: The soft I’m using for the images is open source and extremely cool. It’s called Fritzing, and it lives on donations and on selling an Arduino UNO starter kit I’m going to buy before year’s end. If you are planning on buying one, support them by buying from them! I’m not related to that project at all but I’m using it so much to write these posts that I feel that I owe them.

UPDATE: Reading the datasheet closely, I found out this ICs work only as DEMUXs. I updated the post to remove the references to MUXing.

Posted on Saturday, November 6, 2010 9:02 AM | Back to top

Comments on this post: DEMUX with Netduino

No comments posted yet.
Your comment:
 (will show your gravatar)

Copyright © Joaquin Jares | Powered by: