MCU with lots of GPIOs vs. smaller MCU + GPIO expander - Page 1
Forum section: electronics / projects

author: leonerd
author_rank: Regular Contributor
timestamp: December 17, 2015, 02:53:10 pm
content: I'm contemplating a project requiring lots of GPIOs (ideally two independent 8bit ports, supporting high/low/hiZ input tristate per pin) that don't need to be very fast, and in addition an SPI port, duplex UART, and a few other GPIOs for miscellaneous buttons/LEDs/etc. I don't need much program flash or RAM; I imagine 4Ki / 256bytes ought to suffice. No need for any analog, fancy timers, or other such.About the cheapest I can find in a single chip is the ATmega48PB, which comes in about £6 per chip.If instead I consider using a GPIO expander to get those big 8bit ports, such as the MCP23x17 chips, I can get one of those and an ATtiny841 for about £1 each, so looking at just over £2 for the pair. Looking at SOIC-sized SMD parts, in combination these two aren't much bigger than a single-chip ATmega48PB anyway.I wonder - is this a valid design choice? Is this the kind of thing that actually the GPIO expander parts are designed for? Noting that I need an SPI port in my design anyway, talking that GPIO over the SPI expander only requires one more pin for its SS line, so that starts to look quite a reasonable idea.

author: firewalker
author_rank: Super Contributor
timestamp: December 17, 2015, 03:00:48 pm
content: ATmega8?Alexander.

author: mikerj
author_rank: Super Contributor
timestamp: December 17, 2015, 03:04:07 pm
content: Does it have to be an AVR? There are plenty of e.g. ARM Cortex micros that meet your requirements for a fraction of the price.EDIT: actualy £6 for a Mega48PB is insane,Digikeyhave these listed at £1.21.

author: ajb
author_rank: Super Contributor
timestamp: December 17, 2015, 03:04:53 pm
content: It's a valid choice, whether or not it's the best choice depends on the application specifics. Expanders are especially good when your IO needs to be spread out over the board (or over several boards), but typically if you're only short by a handful of IO going to the next size MCU up is an easier option.

author: Fungus
author_rank: Super Contributor
timestamp: December 17, 2015, 03:07:56 pm
content: Quote from: leonerd on December 17, 2015, 02:53:10 pmYes.But AVR chips don't cost £6.I can buy a whole Arduino with Mega328 chip for $2, worldwide shipping included.eg.http://www.ebay.com/itm/221030168024

author: leonerd
author_rank: Regular Contributor
timestamp: December 17, 2015, 03:12:26 pm
content: Quote from: mikerj on December 17, 2015, 03:04:07 pmIt doesn't *have* to be AVR, but so far everything I've built is AVR-based, so I have lots of tooling, software, experience etc... in that direction.Quote from: mikerj on December 17, 2015, 03:04:07 pmHuhh... That does sound a rather much more reasonable price by that point yes.

author: leonerd
author_rank: Regular Contributor
timestamp: December 17, 2015, 03:28:34 pm
content: Quote from: Fungus on December 17, 2015, 03:07:56 pmThe 328P and friends are just a little short for me. Those have 8 + 8 + 6 IO pins in total. Once you take out the 16 GPIO lines I need, the 4 SPI pins and the 2 UART pins, that leaves none left at all for the peripheral LEDs and buttons. The extra four IO pins added by the -PB variants of those chips just tip the balance into something that might actually work for me.Now, granted I could chain the LEDs off the edge of the SPI port in a daisychain from the other SPI device I have, but now we're looking at extra chips again. And that doesn't solve my button problem.

author: Fungus
author_rank: Super Contributor
timestamp: December 17, 2015, 03:31:44 pm
content: Quote from: leonerd on December 17, 2015, 03:28:34 pmLEDs? Maybe you could charlieplex them.Buttons? A matrix.Arduino heads have come up with all sorts of ways to save pins. Maybe you could tell us what's connected.

author: kony
author_rank: Regular Contributor
timestamp: December 17, 2015, 03:34:38 pm
content: For very generic I/O, when just few pins or even several bytes are missing, there are4021, 4094 and 74HC595. If you need the maskable interrupts (that, FYI, don't hold information about what input caused it in case of that particular MCP expanders, just the fact that there was IRQ) trough, then dedicated part might be right choice - depends on many factors to be considered in each design.Show some love for old generic 40xx parts!

author: Fungus
author_rank: Super Contributor
timestamp: December 17, 2015, 03:55:22 pm
content: Quote from: leonerd on December 17, 2015, 03:28:34 pmActually the quad flatpack chips they use on the Pro Mini have 8+8+8. That's why the Pro Mini has extra holes on the PCB - for A6 and A7.The two extra pins can only be used as analog inputs, not general I/O, but that's OK for buttons.You can even connect a couple of push buttons to each pin via. resistor dividers. Pressing different buttons gives a different voltage levels at the pin.Result: The two extra pins give you four push buttons (yes, I've actually done that)

author: Scrts
author_rank: Frequent Contributor
timestamp: December 17, 2015, 04:01:09 pm
content: All those expanders these days are just PITA. You select a micro with more pins and that's it.Have a look into this one for example:http://www.newark.com/freescale-semiconductor/mke04z8vfk4/microcontroller-mcu-32-bit-cortex/dp/08X2777

author: Fungus
author_rank: Super Contributor
timestamp: December 17, 2015, 04:03:06 pm
content: Quote from: Scrts on December 17, 2015, 04:01:09 pmUmmm.... that particular one haslesspins.

author: tszaboo
author_rank: Super Contributor
timestamp: December 17, 2015, 04:05:13 pm
content: Yes, it is a valid option. There could be variety of reasons why you would just use an I2C or SPI GPIO expander. Most of us doesnt design big quantity enough to justify the cost saving of doing an extra firmware for a second MCU. And it is convenient also, with libraries written, and pins being next to each other and in order.Even at few hundred quantities I take a GPIO expander over a bigger MCU, any time.

author: leonerd
author_rank: Regular Contributor
timestamp: December 17, 2015, 04:09:27 pm
content: After a little more searching, it seems the ATmega164 might not be a bad bet. It's a 40/44pin chip with four full 8-bit IO ports, giving a nicely generous 32 IO pins. The best prices I can find for it put it just under £3, which is still a little more expensive than the £2.20 I can get a pair of ATtiny + MCP23S17, but for the greater amount of spare GPIO pins and the convenience of single-chip solution, I think the mega164 might just win in the end.

author: rx8pilot
author_rank: Super Contributor
timestamp: December 17, 2015, 04:10:57 pm
content: Quote from: Fungus on December 17, 2015, 03:07:56 pmFake?Sent from my SAMSUNG-SM-G890A using Tapatalk

author: newbrain
author_rank: Super Contributor
timestamp: December 17, 2015, 04:12:41 pm
content: Quote from: Fungus on December 17, 2015, 03:55:22 pmNo real shame, you are not the first and probably not the last.A very good technique to spare pins.For brand name example look at e.g. bottom right of page 21 (and more or less everywhere else) in:http://www.kallhovde.com/pioneer/tricomp/Audio/XC/XC-L7_RRV1946.pdfAnd I could swear on someone else's head I've seen the same in some Yamaha, with up to about ten buttons!

author: leonerd
author_rank: Regular Contributor
timestamp: December 17, 2015, 04:15:12 pm
content: Quote from: Fungus on December 17, 2015, 03:55:22 pmSure; but that still has zero spare pins I can use for outputs, to drive the status LEDs. One option remains at that point, which is to daisychain a 74'595 off the top of the other SPI device, giving me 8 LED output lines for zero extra pins, but that's still a dual-chip solution. If I was going to do that, there's other nicer things I could do instead.Quote from: Fungus on December 17, 2015, 03:55:22 pmMy ATtiny-based radio remote control uses a single line shared between ADC and Anacomp input pins to scan 3 pushbuttons while at the same time using the anacomp's threshold detector to wake the MCU up on any button press; so I don't even have to poll them. I'm quite familiar with analog button chains

author: Fungus
author_rank: Super Contributor
timestamp: December 17, 2015, 04:18:36 pm
content: Quote from: rx8pilot on December 17, 2015, 04:10:57 pmThat seller has sold 3846 of them and still has 99.9% feedback.I've bought hundreds of them over the last few years (from various sellers) at those sorts of prices, never had a problem.AFAIK the AVR chips haven't ever been cloned. Atmel makes the chips, everything else is basically just a breakout board.

author: Scrts
author_rank: Frequent Contributor
timestamp: December 17, 2015, 04:24:39 pm
content: Quote from: Fungus on December 17, 2015, 04:03:06 pmWell, you can go for bigger:http://www.farnell.com/datasheets/1898381.pdf

author: Fungus
author_rank: Super Contributor
timestamp: December 17, 2015, 04:25:48 pm
content: Quote from: leonerd on December 17, 2015, 04:15:12 pmHow many LEDs? You can drive six LEDs using three pins if you charlieplex them.A 74HC595 is a cheap/simple way to drive LEDs.(Attaching 8 LEDs is actually way out of spec for the chip's current rating but everybody's been doing it for years without incident, so...)Edit: How many pins do you actually need? What for...? The chip you mentioned originally only has 27 I/O pins so theremustbe a trick to get a 24 pin chip to work.

author: Fungus
author_rank: Super Contributor
timestamp: December 17, 2015, 04:28:02 pm
content: Quote from: Scrts on December 17, 2015, 04:24:39 pmI'm sure you can ... but the OP already said he wants to go the AVR route if possible.

author: mariush
author_rank: Super Contributor
timestamp: December 17, 2015, 04:49:19 pm
content: Something like PIC16F1526 is $2 if you buy 1, less in quantity, and gives you ton of I/O besides 2 uarts, spi etc etc :http://www.digikey.com/product-detail/en/PIC16F1526-I%2FPT/PIC16F1526-I%2FPT-ND/2651377

author: leonerd
author_rank: Regular Contributor
timestamp: December 17, 2015, 05:04:40 pm
content: Quote from: Fungus on December 17, 2015, 04:25:48 pmYes; I'm fully aware of charlieplexing. However, as I keep trying to point out - the 328P-shaped approach would leave me with absolutely no pins to attach LEDs to. Not three.Quote from: Fungus on December 17, 2015, 04:25:48 pmYup - that could work. However, that's again into the realm of attaching multiple chips together just to cover for the fact that the single core MCU doesn't have enough pins on its own. If I was doing that route, I think I'd still go with putting my main 16 GPIO pins on an expander like the MCP23S17. So hanging LEDs off a simple SR doesn't improve upon that design.Quote from: Fungus on December 17, 2015, 04:25:48 pmThe overall design has the 16 first GPIOs split into two banks of 8, to sit at either end of a multicore cable of some kind (with a variety of adapters for various plug shapes); and the SPI port attaches to an 8x8 LED matrix, showing simultaneous display of through-connectivity of that cable. It's for cable pinout testing/verification. The UART is to attach it to a computer to perform some automated testing or setup of special kinds of cable, so as to drive some LEDs with simple PASS/FAIL indication. Finally a few extra buttons to give some local control of special tests, like per-connector loopback probing or detection of shorted pins. As the main 16 GPIOs are only doing low-bandwidth pin probing down the cable, they don't need high speed so sticking them in a GPIO expander seemed fine. Also has the advantage that it further isolates the cable end from the core electronics, which might have good properties for damage limitation, etc...

author: Fungus
author_rank: Super Contributor
timestamp: December 17, 2015, 05:42:20 pm
content: Quote from: leonerd on December 17, 2015, 05:04:40 pm...but you never actually listed the pinsQuote from: leonerd on December 17, 2015, 05:04:40 pmOK, that'salmosta list.Yes, you'll need at least 74HC595 for the LEDs and even so it will be very tight.Your big problem is the UART. Not many ATtinys have one. There's the ATtiny2313 but that only has 2kb flash.You need to find the cheapest possible AVR chip with a UART then put your cable on a 16-bit I/O chip. The rest is easy.Edit:The cheapest chip might actually be the ATmega48 you mentioned in your first post - we've gone full circle

author: leonerd
author_rank: Regular Contributor
timestamp: December 17, 2015, 05:55:10 pm
content: Quote from: Fungus on December 17, 2015, 05:42:20 pmATtiny841, ATtiny1634,...Quote from: Fungus on December 17, 2015, 05:42:20 pmYes - indeed that was my original question. I could use a UART-capable ATtiny and an MCP23S17, or at a smaller extra cost (admittedly not as much as I first thought, but still something), I could use a single-chip solution for the whole lot. I was looking for some discussion on what might be a better choice to make of these two.In reality, it's not really a one-vs-two chip design anyway; it's two-vs-three because of the MAX7219 LED driver. So maybe the incremental annoyance of having that separate GPIO expander isn't as annoying as first appeared.

author: Kleinstein
author_rank: Super Contributor
timestamp: December 17, 2015, 05:57:25 pm
content: Low cost AVRs with UART are Tiny2313 , Mega48 . For more Pins you could use a Mega16 (old) or mega164. The Mega328 is just the higher memory version similar to the mega48. For high pin count, the X-mega series is also worth a look - but this is 3.3 V only and rather small form factor.Keep in mind that the UART usually required a crystal clock to be reliable. So 20 useful IO pins are left on the mega48 and similar.

author: Fungus
author_rank: Super Contributor
timestamp: December 17, 2015, 06:03:53 pm
content: Quote from: leonerd on December 17, 2015, 05:55:10 pmIt really comes down to how many you're going to make. If you're only making a few dozen then spend the extra $50 to make your life easy.If you're making thousands,thenyou have to start penny-pinching.I wouldn't worry about having an extra chip. The overall complexity hardly goes up at all with a single extra I/O chip.

author: westfw
author_rank: Super Contributor
timestamp: December 18, 2015, 03:48:03 am
content: One of the nice things about microchip is that PIC16F59 in DIP40 for <$1.50...

author: Ian.M
author_rank: Super Contributor
timestamp: December 18, 2015, 04:53:05 am
content: That may be nice for the MBAs considering 100K+ production units, but for the rest of us:One of the *nasty* things about microchip is that PIC16F59 in DIP40 for <$1.50...Its a different pinout to any newer 40 pin part so once you commit you are stuck with it forever, and if I start flaming its baseline architecture, I'll end up sounding worse than a rabid Atmel fanboi, so just take it from me - its *BAD*. It also needs ancillary components to do anything useful - its got no internal oscillator and its internal POR reset can only be trusted if Vdd comes up cleanly and fairly sharply. I wish Microchip would simply jack up the price to just above the cheapest 40 pin PIC16F1xxx part - it would at least minimise the number of new designers who would have to suffer it!

author: leonerd
author_rank: Regular Contributor
timestamp: December 18, 2015, 02:04:49 pm
content: Quote from: westfw on December 18, 2015, 03:48:03 amHowever, it's still a PIC and not an AVR - as I said all my experience, tooling, reusable library code, etc.. are all centred around AVRs - mostly ATtiny at that. So I'd like to stick there if I can.I think on reflection of the relatively small difference in actual price and annoyance, I'll stick with my two-chip idea of a tiny841 + MCP23S17. That has the advantage of being two chips I already know and use. The difference in price or complexity of the board isn't that much higher than going with a single (new to me) chip anyway.Thanks all for the interesting discussion

author: Delta
author_rank: Super Contributor
timestamp: December 18, 2015, 02:21:45 pm
content: Just use a 40pin PIC18 and be done with it, I/Os up the ying yang!

author: Macbeth
author_rank: Super Contributor
timestamp: December 18, 2015, 03:00:07 pm
content: Quote from: Fungus on December 17, 2015, 03:07:56 pmOoh! Look at Mr Moneybags and his ostentatious display of wealthI doff my cap to you sir, us lesser proles have to make do with AliExpress $1.50 worldwide shipping included, e.g.http://www.aliexpress.com/item/Free-Shipping-1pcs-lot-ATMEGA328P-Pro-Mini-328-Mini-ATMEGA328-5V-16MHz-for-Arduino/32541624244.html, though as it's christmas I think I could splash out an extra few cents for a seller with a better rep.

author: Skashkash
author_rank: Regular Contributor
timestamp: December 18, 2015, 03:03:13 pm
content: Sometimes IO expanders bring extras to the design.Higher current, led drivers, level translation. That can sometimes reduce the BOM.But in general, just moving to a part with more pins will be cheaper.

author: westfw
author_rank: Super Contributor
timestamp: December 19, 2015, 07:58:02 am
content: QuoteI had in mind putting something relatively trivial on the PIC, like IO expander code. Then you can put your "hard" code on an Attiny of some kind.

author: Ian.M
author_rank: Super Contributor
timestamp: December 19, 2015, 08:06:15 am
content: Even so, you wouldn't want to use a baseline PIC. Heck, you cant even return from a subroutine with a number in the accumulator (W) because the only return instruction loads W with a literal, and half the program memory map is inaccessible to subroutine CALL instructions!

author: obiwanjacobi
author_rank: Super Contributor
timestamp: December 19, 2015, 12:07:21 pm
content: Atmega128 for 1.10http://www.ebay.com/itm/IC-ATMEGA128A-AU-QFP-64-8-bit-Microcontroller-NEW-GOOD-QUALITY-/291548875901?hash=item43e1aacc7d:g:OkQAAOxyPH9RvvjChttp://www.atmel.com/devices/ATMEGA128.aspx

author: lovemb
author_rank: Contributor
timestamp: December 19, 2015, 03:24:41 pm
content: I've bought 2x atmega128 exactly from that ebay seller.I received them and both work perfectly fine so far, but they seem to be harvested from used equipment??Here's a couple pics:

author: leonerd
author_rank: Regular Contributor
timestamp: December 19, 2015, 04:00:58 pm
content: Quote from: obiwanjacobi on December 19, 2015, 12:07:21 pmOoh hmm. That's another idea. Use a chip with even more IO pins, that can do the LED matrix muxing as well, then I wouldn't actually need the SPI port or the MAX7219 on there. That might actually be a winner.

author: mikerj
author_rank: Super Contributor
timestamp: December 19, 2015, 04:17:32 pm
content: Quote from: lovemb on December 19, 2015, 03:24:41 pmThis should come as no surprise. If you buy components like this cheaply through Chinese ebay sellers then the chances of getting a original, new, full specification part are very low. You are somewhat fortunate that the devices are actually what the part number says they are; remarking ICs is very common.