Yemto
Posts: 26
Joined: Thu Sep 03, 2015 7:57 pm

3-to-8 decoder (74VHC138M)

Tue Jan 05, 2016 12:49 pm

So I'm currently planning my 2nd and 3rd project I want to do with my pi2, which involves the GPIO pins. I been looking at buying these http://uk.rs-online.com/web/p/decoders- ... s/8021358/ so I can control more leds without using up so many GPIO pins, and that was the best I could find.

At first I thought that decoder only could light up one led at the time, so my plan was to see if I could turn off and on the pins going to it's address so fast that two or more appeared to be on. But after trying to understand the truth table, from what I understand it seems the address turns off the output, instead of turning it on, or am I misreading it? Also, would my idea work? I don't know if I can change the state of the GPIO pins that fast, or if it would damage them.

User avatar
DougieLawson
Posts: 39120
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: 3-to-8 decoder (74VHC138M)

Tue Jan 05, 2016 1:08 pm

You'll do better by looking at MCP23017 (I2C) or MCP23S17 (SPI) chips, each of of those gives you 16 GPIOs and you can stack eight on each bus (without needing anything else) That gives you 256 GPIOs without thinking.

Those two are also very commonly used (by everybody including me) so there's lots of free code out there to drive them.

There's even some commercially made add-on boards to mount an MCP23017 on your GPIO pins with no special wiring.
http://www.rapidonline.com/electronic-c ... pi-73-4594
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

Yemto
Posts: 26
Joined: Thu Sep 03, 2015 7:57 pm

Re: 3-to-8 decoder (74VHC138M)

Tue Jan 05, 2016 1:19 pm

Thanks, but no thanks. I'm looking to learn how to make my own circuits, and program them. So even if the decoder I been looking at is less optimal to use, as long as it can do what I want, I feel I learn more from using it.

User avatar
rurwin
Forum Moderator
Forum Moderator
Posts: 4258
Joined: Mon Jan 09, 2012 3:16 pm
Contact: Website

Re: 3-to-8 decoder (74VHC138M)

Tue Jan 05, 2016 1:32 pm

You are correct. The outputs go low when they are selected.

You should also limit the current that you are drawing through the output. An LED typically takes 20mA but that is right at the top end of the Absolute Maximum rating for the chip. Unfortunately the datasheet does not give typical ratings for the current. I would arrange the circuit to draw at most 15mA, which would give you reasonable brightness from the LED but ensure that you are not getting close to damaging the chip.

User avatar
hansotten
Posts: 341
Joined: Fri Jul 13, 2012 6:01 am
Contact: Website

Re: 3-to-8 decoder (74VHC138M)

Tue Jan 05, 2016 1:33 pm

Yemto wrote:So I'm currently planning my 2nd and 3rd project I want to do with my pi2, which involves the GPIO pins. I been looking at buying these http://uk.rs-online.com/web/p/decoders- ... s/8021358/ so I can control more leds without using up so many GPIO pins, and that was the best I could find.

At first I thought that decoder only could light up one led at the time, so my plan was to see if I could turn off and on the pins going to it's address so fast that two or more appeared to be on. But after trying to understand the truth table, from what I understand it seems the address turns off the output, instead of turning it on, or am I misreading it? Also, would my idea work? I don't know if I can change the state of the GPIO pins that fast, or if it would damage them.
A 3 to 8 address decoder like the 74VHC138M (the 74HC138 will do fine, and is cheaper and available in DIL) will make one of its outputs low and the others high. Not very useful if you want a LED to light. You cpuld use an inverter behind the 74ls138 like a 74hc245, then you can multiplex the leds.
http://duinorasp.hansotten.com for Raspberry Pi and Arduino and ESP8266
http://retro.hansotten.nl for retrocomputing with the 6502 cpu

jamesh
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 26659
Joined: Sat Jul 30, 2011 7:41 pm

Re: 3-to-8 decoder (74VHC138M)

Tue Jan 05, 2016 1:40 pm

Yemto wrote:Thanks, but no thanks. I'm looking to learn how to make my own circuits, and program them. So even if the decoder I been looking at is less optimal to use, as long as it can do what I want, I feel I learn more from using it.
Excellent attitude!
Principal Software Engineer at Raspberry Pi (Trading) Ltd.
Contrary to popular belief, humorous signatures are allowed.
I've been saying "Mucho" to my Spanish friend a lot more lately. It means a lot to him.

Yemto
Posts: 26
Joined: Thu Sep 03, 2015 7:57 pm

Re: 3-to-8 decoder (74VHC138M)

Tue Jan 05, 2016 1:41 pm

rurwin wrote:You are correct. The outputs go low when they are selected.

You should also limit the current that you are drawing through the output. An LED typically takes 20mA but that is right at the top end of the Absolute Maximum rating for the chip. Unfortunately the datasheet does not give typical ratings for the current. I would arrange the circuit to draw at most 15mA, which would give you reasonable brightness from the LED but ensure that you are not getting close to damaging the chip.
Then I don't think I can use that chip, since I want to output to go high when it's selected.

If cases like that, can I use a resistor after the chip, and before the LED, or does it have to be at a specific point?

drgeoff
Posts: 10765
Joined: Wed Jan 25, 2012 6:39 pm

Re: 3-to-8 decoder (74VHC138M)

Tue Jan 05, 2016 2:12 pm

You can use a 'low' to light a LED every bit as well as a 'high'. No need for an inverter.

User avatar
DougieLawson
Posts: 39120
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: 3-to-8 decoder (74VHC138M)

Tue Jan 05, 2016 2:14 pm

Yemto wrote:
Then I don't think I can use that chip, since I want to output to go high when it's selected.

If cases like that, can I use a resistor after the chip, and before the LED, or does it have to be at a specific point?
Back to the MCP23017 that you so rudely rejected.
Or use a bipolar transistor as an inverter.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

Yemto
Posts: 26
Joined: Thu Sep 03, 2015 7:57 pm

Re: 3-to-8 decoder (74VHC138M)

Tue Jan 05, 2016 2:23 pm

drgeoff wrote:You can use a 'low' to light a LED every bit as well as a 'high'. No need for an inverter.
I had already started to look at a new chip, http://uk.rs-online.com/web/p/decoders- ... s/7812784/ I would be at it's minimum output for 2 volts, and from what I can see, if I use a resistor so it draws less than 20 mA it should be okay.
DougieLawson wrote:Back to the MCP23017 that you so rudely rejected.
Or use a bipolar transistor as an inverter.
I'm sorry if I appeared rude, I just don't think that chip is something I would use right now when I just trying to learn, it seems way beyond my own skill level. I want to be able to write the code myself, and understand how the chip works.

danjperron
Posts: 3502
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: 3-to-8 decoder (74VHC138M)

Tue Jan 05, 2016 2:34 pm

At first I thought that decoder only could light up one led at the time, so my plan was to see if I could turn off and on the pins going to it's address so fast that two or more appeared to be on. But after trying to understand the truth table, from what I understand it seems the address turns off the output, instead of turning it on, or am I misreading it? Also, would my idea work? I don't know if I can change the state of the GPIO pins that fast, or if it would damage them.
The answer is yes!

So the enable is low. Then it is just a question of flipping the leds and use 3.3V instead of the ground.
Cathode on the output, Anode trought a resistor to 3.3V.

Like you said, for a leaning experience it will work.

Yemto
Posts: 26
Joined: Thu Sep 03, 2015 7:57 pm

Re: 3-to-8 decoder (74VHC138M)

Tue Jan 05, 2016 2:48 pm

danjperron wrote:The answer is yes!

So the enable is low. Then it is just a question of flipping the leds and use 3.3V instead of the ground.
Cathode on the output, Anode trought a resistor to 3.3V.

Like you said, for a leaning experience it will work.
So basically low becomes ground? and if so, wouldn't that also mean the other leds would get 3.3v in both directions, wouldn't that damage them?

User avatar
Burngate
Posts: 6302
Joined: Thu Sep 29, 2011 4:34 pm
Location: Berkshire UK Tralfamadore
Contact: Website

Re: 3-to-8 decoder (74VHC138M)

Tue Jan 05, 2016 2:48 pm

It goes back to the days when logic chips were mostly TTL (but not as far back as RTL or DTL)
Because the transistors were NPN, and the inputs were applied to their emitters, a low signal (0) had to sink current, but a high (1) didn't. An open-circuit appeared as a high or 1
So active-low used less power.

If all you want to do is light 8 leds, attach their anodes through one resistor to the 3v3 rail, and the cathodes to the 74x138 outputs.
You only need one resistor, because only one led will be taking current.
And don't assume they need 20mA just because the data sheet says they can take that - 4mA is plenty bright for a modern led, unless you're building a searchlight
so my plan was to see if I could turn off and on the pins going to it's address so fast that two or more appeared to be on
If you're driving the address lines as fast as you can, you may find that the leds flicker slightly.
That's not because you're driving them wrongly - it's because linux takes time out do do other things, and during that time the one that's lit is staying on longer than it should.
Since the eye can't see changes faster than a few tens of hertz, you could drive them a lot slower than "as fast as you can"

User avatar
Burngate
Posts: 6302
Joined: Thu Sep 29, 2011 4:34 pm
Location: Berkshire UK Tralfamadore
Contact: Website

Re: 3-to-8 decoder (74VHC138M)

Tue Jan 05, 2016 2:58 pm

Yemto wrote:So basically low becomes ground? and if so, wouldn't that also mean the other leds would get 3.3v in both directions, wouldn't that damage them?
If they each have their own resistor to 3v3, one end of the resistor-led chain is at 3v3 and the other is at ground (so it's lit) or 3v3 (so it isn't lit) there's never a situation where the led is reverse-biased.

If you use one resistor for all of them, then the one that's on will have 1v8 (or there-abouts) across it and be lit. The others will have 3v3 on their cathodes from the decoder, but only about 1v8 on their anodes so they will be reverse-biased.
But they won't care! They're diodes! They (generally) can take up to 5v reverse without dying.

PiGraham
Posts: 3932
Joined: Fri Jun 07, 2013 12:37 pm
Location: Waterlooville

Re: 3-to-8 decoder (74VHC138M)

Tue Jan 05, 2016 3:12 pm

Yemto wrote:Thanks, but no thanks. I'm looking to learn how to make my own circuits, and program them. So even if the decoder I been looking at is less optimal to use, as long as it can do what I want, I feel I learn more from using it.
It's good to try things out. By all means play around with the '138. Still, if what you want to do is add inputs or outputs your time would be much better spent playing with and learning about a different chip. A GPIO expander, as Dougie suggested (in and out), or a shift register (out or in) are much better options.

danjperron
Posts: 3502
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: 3-to-8 decoder (74VHC138M)

Tue Jan 05, 2016 3:34 pm

It is a good thing that you try to understand the basic TTL ic.


30 years ago I designed a panel for bank to display interest rates up to 16 different values. It was a simple unit that you could change the numbers using a I.R. remote control.

I was using a 74LS138 with a transistor to drive the 8 digits segments. One of my friend which I eventually gaved him the contract ask me why are you not using chip already made for that purpose. I told him simply 'cost'. At that time the max7219 was way overprice and all the parts I was using was under one max7219. I will have to use 16 of them and was way to costly.

User avatar
DougieLawson
Posts: 39120
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: 3-to-8 decoder (74VHC138M)

Tue Jan 05, 2016 4:30 pm

Yemto wrote:
DougieLawson wrote:Back to the MCP23017 that you so rudely rejected.
Or use a bipolar transistor as an inverter.
I'm sorry if I appeared rude, I just don't think that chip is something I would use right now when I just trying to learn, it seems way beyond my own skill level. I want to be able to write the code myself, and understand how the chip works.
Try it, they are the easiest thing in the Universe to use.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

PiGraham
Posts: 3932
Joined: Fri Jun 07, 2013 12:37 pm
Location: Waterlooville

Re: 3-to-8 decoder (74VHC138M)

Tue Jan 05, 2016 5:06 pm

DougieLawson wrote:
Yemto wrote:
DougieLawson wrote:Back to the MCP23017 that you so rudely rejected.
Or use a bipolar transistor as an inverter.
I'm sorry if I appeared rude, I just don't think that chip is something I would use right now when I just trying to learn, it seems way beyond my own skill level. I want to be able to write the code myself, and understand how the chip works.
Try it, they are the easiest thing in the Universe to use.
They are certainly no more difficult to use than a 74138.
For the stated purpose of IO expansion they are easier, because they were made for the job.
The OP is likely to learn plenty in using them, gaining more transferrable skills, and get a better result.
Last edited by PiGraham on Tue Jan 05, 2016 7:24 pm, edited 1 time in total.

User avatar
mikronauts
Posts: 2783
Joined: Sat Jan 05, 2013 7:28 pm
Contact: Website

Re: 3-to-8 decoder (74VHC138M)

Tue Jan 05, 2016 5:17 pm

Use a 74HC595 instead, as then you don't have to refresh the LED's.

3 inputs: CLK, DI, LATCH, easy to use, ton of sample code on the web
Yemto wrote:So I'm currently planning my 2nd and 3rd project I want to do with my pi2, which involves the GPIO pins. I been looking at buying these http://uk.rs-online.com/web/p/decoders- ... s/8021358/ so I can control more leds without using up so many GPIO pins, and that was the best I could find.

At first I thought that decoder only could light up one led at the time, so my plan was to see if I could turn off and on the pins going to it's address so fast that two or more appeared to be on. But after trying to understand the truth table, from what I understand it seems the address turns off the output, instead of turning it on, or am I misreading it? Also, would my idea work? I don't know if I can change the state of the GPIO pins that fast, or if it would damage them.
http://Mikronauts.com - home of EZasPi, RoboPi, Pi Rtc Dio and Pi Jumper @Mikronauts on Twitter
Advanced Robotics, I/O expansion and prototyping boards for the Raspberry Pi

PiGraham
Posts: 3932
Joined: Fri Jun 07, 2013 12:37 pm
Location: Waterlooville

Re: 3-to-8 decoder (74VHC138M)

Wed Jan 06, 2016 9:44 am

One 'benefit' of using TTL like the 74HC595 or 74HC138 is that you can breadboard the chip on it's own and drive it with a few pushbuttons and LEDs attached to see how it works. i2c and SPI interfaces are more useful things to understand but less accessible.

If it is the logic gates that interest you get some flipflops and gates and play with those as well. You could build a '138 from NAND gates, if you wanted a challenge.

You could treat the learning about logic separately to expanding the GPIO and lighting LEDs. There are logic simulators around that would allow you to experiment with all sorts of chips for free. Top hit on Google was http://sol.gfxile.net/atanua/index.html
Which seems to simulate the 74 logic family and is open source. I haven't tried it but the screen shots look nice.

BTW WiringPi for one supports the common gpio port expanders so that you can use the expansion port lines just like the Pi's native GPIO lines, setting in or out mode and reading or writing states with familiar functions, which is nice.

User avatar
DougieLawson
Posts: 39120
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: 3-to-8 decoder (74VHC138M)

Wed Jan 06, 2016 3:12 pm

PiGraham wrote:One 'benefit' of using TTL like the 74HC595 or 74HC138 is that you can breadboard the chip on it's own and drive it with a few pushbuttons and LEDs attached to see how it works. i2c and SPI interfaces are more useful things to understand but less accessible.
What makes you think you can't do that with an MCP23017?

http://www.raspberrypi-spy.co.uk/2013/0 ... pi-part-1/ has one mounted on a regular breadboard.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

PiGraham
Posts: 3932
Joined: Fri Jun 07, 2013 12:37 pm
Location: Waterlooville

Re: 3-to-8 decoder (74VHC138M)

Wed Jan 06, 2016 7:38 pm

DougieLawson wrote:
PiGraham wrote:One 'benefit' of using TTL like the 74HC595 or 74HC138 is that you can breadboard the chip on it's own and drive it with a few pushbuttons and LEDs attached to see how it works. i2c and SPI interfaces are more useful things to understand but less accessible.
What makes you think you can't do that with an MCP23017?

http://www.raspberrypi-spy.co.uk/2013/0 ... pi-part-1/ has one mounted on a regular breadboard.
Only because driving an i2c or SPI peripheral with pushbuttons is a bit trickier than driving a '138 or a '595.
That link shows the bus driven from the Pi i2c port.

Did you mean that any chip can be used on a breadboard instead of a PCB? Of course.

User avatar
DougieLawson
Posts: 39120
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: 3-to-8 decoder (74VHC138M)

Wed Jan 06, 2016 8:41 pm

Why? You'll need an extra GPIO (INTA or INTB) to get an interrupt when the MCP23017 detects a signal change on either GPIOA or GPIOB. It's not difficult to detect a keypress. So the MCP23017 can sense 16 GPIO transition changes with four wires + Vcc + GND.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

PiGraham
Posts: 3932
Joined: Fri Jun 07, 2013 12:37 pm
Location: Waterlooville

Re: 3-to-8 decoder (74VHC138M)

Wed Jan 06, 2016 9:07 pm

DougieLawson wrote:Why? You'll need an extra GPIO (INTA or INTB) to get an interrupt when the MCP23017 detects a signal change on either GPIOA or GPIOB. It's not difficult to detect a keypress. So the MCP23017 can sense 16 GPIO transition changes with four wires + Vcc + GND.
No, I don't mean reading button inputs with a MCP23017. That's easy.

I mean the OP, interested in how logic chips work, might like to drive the inputs to a 74HC138 or 74HC595 manually with wire links or switches. The user can play the part of the micro and see what happens in slowmo. IF you want to try that then the simpler interfaces of the 74 series chips are easier to step through than manually driving i2c or SPI with protocols and bi-directional data lines.

If the OP just wants to add some gpio lines then I agree with you that a MCP23017 or similar is far and away the best choice.

Yemto
Posts: 26
Joined: Thu Sep 03, 2015 7:57 pm

Re: 3-to-8 decoder (74VHC138M)

Wed Jan 06, 2016 9:32 pm

To tell the truth, for my project I don't even need any kind of chip, I can power all the leds I need directly from the pi. But I want to learn about them, so I will get 74HC595 and 74HC138 as practice, and I can actually understand their data sheet. It also lets me think about the limitations of the chips.

I have no doubts that the MCP23017 is a good chip. But at the moment, I'm not interested of using it. I might in the future when I really need it, know more about the chips, and know more about electricity in general.

One last thing, this thread is NOT of what's optimal for me to use. I'm just learning about these things, I want to take it slow, and use chips and code I can understand before I use more advanced chips.
Last edited by Yemto on Wed Jan 06, 2016 10:05 pm, edited 1 time in total.

Return to “Beginners”