Sunday, December 23, 2012

System Design and Parts Selection

A while back I said I was going to create a series of posts detailing how I go about building an electronics project from start to finish.  That hasn't happened - until now!

You see I'm usually pretty lazy on my off hours.  And as lazy as I am about actually working on an electronics project in my spare time I'm lazier about writing about it.  It turns out that I actually started working on a project months ago and haven't written anything about it despite having designed the electronics, fabricated the board and started developing the firmware.  But since I have a good head start on actually making this project I figured I could do some backfill on the writing and show off some of my process.

Here's the idea (on paper):

So I have a shirt with a traffic light on it.  The lights are several sets of red, yellow and green LEDs soldered onto a PCB and attached to the inside of the shirt with velcro or sewn on.  The traffic light on the front is probably an iron-on applique or a custom shirt if I decide to get expensive.  The lights are controlled by an accelerometer: if I sit still the light is red, when I move forward the light turns green and when I stop it turns yellow for three seconds then red again until the process starts over.  It's a simple clear idea that makes for an interesting electronics project.

The first steps in a project like this are to generate a block diagram and general system description so you can start finding suitable parts.  That's what I'll discuss today.  Here's the system block diagram I came up with:

Let's go over this briefly block by block.  The most obvious are the LEDs - I need one set of LEDs in each color of the traffic light.  There aren't many requirements for the LEDs.  I don't think they'll need to be super bright or require heat sinking.  I'm not dealing with any esoteric colors or optical properties - I just need colored light.  The only requirement I can think of is that since these LEDs are going to be velcro'd to the inside of a shirt that I will be wearing I'd prefer that there not be metal pins sticking into me.  In that spirit they should probably be surface mount LEDs.  Since I can't keep my hand steady due to excessive coffee intake I'd like them to be the largest surface mount package I can find.  Also I imagine I'll need a fair number of them - 30 or so.  Thus, they should be cheap as well.  This isn't a tall order for LEDs.

I usually order my electronics on Digi-Key. Digi-Key is rather... old school.  For example, they love their paper catalog.  They love it so much that they created an app for their website that actually shows you the pages from their catalog.  If you love the paradigm of paper please use this.  If you love the Google paradigm then read on for a less-confusing way to use their website.

When finding anything on Digi-Key you have to know what you're looking for and you have to be very patient.  This is how I found the LEDs I would use for this project:
  1. Go to, type 'LED' in search in upper right corner
  2. Browse through the categories until I find one that looks right - Opto-Electronics/Discrete LEDs.  If you're looking for something common it's best to select the category that has the most items in it.  In this case these should be common LEDs and the Discrete LED category has about 15K items in it.  It's a good bet.
  3. Now you have several columns you can use to filter all of the items in this category.  Always always filter on 'In-Stock' before you do anything else.  95% of the time if something is not in stock you don't want it anyhow.  
  4. Now start filtering on what you know you need.  Most of the columns you'll never touch.  Dominant wavelength?  Lens style?  Size?  No idea.  Surface mount? Yes!  Color? Green, yellow or red! Pick the things you know for sure first but select filters one at a time.  If you don't you might end up with 0 results and have to work backwards to figure out why. 
  5. Guess with the rest.  Look at the results - specifically the pictures - and determine roughly what you want, then try to apply filters to remove the other stuff.  
  6. Finally, look at price with an eye towards minimum quantities.  Typically you won't be buying in such bulk that you get price breaks so look for minimum quantities of 1 and (generally) select the lowest price.
The filters I applied to find my LEDs were (in this order):
  • Mounting Type: Surface Mount - For the reasons I discussed earlier
  • Colors: Red, Green and Yellow - This is obvious
  • Package/Case: 1206 - From experience I know this is the largest and most readily-available surface mount LED package.
  • Lens Type: Clear - I definitely don't want tinted and I've never liked the look of diffused (think frosted) lenses.  Personal choice.
  • Test Current: 20mA - More current means more brightness.  I'd like as much brightness as I can manage without too much work.
  • Lens Style: Rectangular - Honestly I have little or no idea about this one but the domed top lenses didn't sound like what I want and I didn't like the look of them from the picture, so I stuck with rectangular and flat top. It's worth noting that at this point I have 118 options remaining.
  • Price: Sort by lowest price with a minimum of 1 piece - If you click the up arrow in the Unit Price column it offers to sort by price with a specified minimum.  Always use a minimum of 1 piece.
Now at this point everything's pretty much the same except for color.  As long as there's nothing suspicious happening with price (ie, lowest price is $.01/unit but every other manufacturer is at least $.25/unit) you might as well select the lowest priced option.  In the end I selected LEDs with a bit more brightness than the cheapest ones.  Here are my selections for LEDs:
  • Green LED: 754-1440-1-ND
  • Yellow LED: 754-1144-1-ND
  • Red LED: 754-1143-1-ND
The LEDs need something to drive them and there are two main options for driving LEDs: voltage or current. Ultimately LEDs are current-mode devices: the more milliamps you push through them the brighter they get.  There are all sorts of methods to get this current through the LEDs in a controlled fashion - here are a few general ideas:
  • Use an LM317 as a constant current source
  • Use a dedicated current-mode LED driver IC
  • Build a switch-mode current driver from discrete components
  • Bias the LEDs with a resistor and turn them on and off with a transistor
In the end biasing the LEDs with a resistor and controlling them with a transistor is the best choice for this application because it's the simplest.  While it won't give me the most bang for the buck in terms of power expended vs. light produced it has the benefit of being simple and easily controllable compared to the others.  I'm not out to prove how clever I am with this project and even if I were I'm not that clever when it comes to analog circuits.  You can be as complicated as you want when it comes to controlling LEDs and have control over ever aspect of the system but realistically all this application calls for is to produce some light and wow a few people at Halloween.  In that spirit I'm going with the simplest driver I can - a transistor switch.  However, this requires a sanity check.

Usually the transistor I would select would be an old standby: the 2N2222.  I know as a rule of thumb that you get a current gain of 10 with the 2N2222: 10mA base current gives you 100mA collector current when the transistor is saturated.  However, if I have about 30 LEDs all drawing 20mA I need to deliver 600mA.  That's getting close to the current limit of the 2N2222 and it also means that I need to deliver 60mA base current to switch it.  That will be more than a typical microcontroller can source from its pins.  For this application I need something with more current gain - in this case a Darlington Array is probably best.  A Darlington Array is basically two transistors connected together such that their current gain is multiplied - so instead of a current gain of 10 you have a current gain of 100.  Also Darlington Arrays can usually handle more current than a single transistor.  

And in fact there's a part that was made for this application: the ULN2803.  It's eight separate Darlington transistors in one chip.  The only rub is that each Darlington channel is rated for a maximum of 500mA.  However, you can double up the channels and get a maximum of 1A.  So if each LED color gets two channels that still only uses six of the eight channels.  Also, the inputs are biased to allow TTL signals (such as those generated by microcontroller output pins) to be used so I don't have to worry about how much current they're drawing.  The ULN2803 can switch the LEDs and I can bias them with resistors to control the brightness. In the end I selected a through-hole DIP version of the ULN2803: Part #497-2356-5-ND

As far as the microcontroller is concerned there are many options.  The computation and timing requirements for this application aren't excessive so I won't need anything too esoteric.  That still doesn't really narrow it down - there are plenty of good microcontroller options in the hobbyist market.  To name a few that I've used: AVR, PSoC, MSP430, PIC and the ARM Cortex-M3.  Of course, there's many potential chips in each of those families as well.  

Since I don't intend to do anything fancy with the microcontroller so I'm going to use an ATMega328 - the microcontroller that powers the Arduino.  The microcontroller is part of Atmel's AVR line of microcontrollers which are a good all-around choice for hobbyists for several reasons:
  1. The development tools are free - AVR Studio can be downloaded from Atmel and has everything you need to write C firmware for both Atmel's 8-bit microcontroller families and the AVR32 family as well.  
  2. The programmer is cheap - the Dragon is a full-featured In-System Programmer (and a less-capable debugger) that will program nearly all of Atmel's 8-bit  for a cost of $50 (and I already own one)
  3. The chips themselves are cheap and I already have several on hand.
  4. Arduino-compatible so that others can easily reproduce the project if they want.
  5. It has a wide variety of peripherals ready to use.
You can either find them on Digi-Key with this part #: ATMEGA328P-PU-ND or you can get it from Sparkfun with or without the Arduino bootloader pre-programmed.  The prices are comparable at either place.

The accelerometer is a trickier question.  I know I'm going to buy it from Sparkfun because they have a good selection of accelerometers with breakout boards.  You can't really buy accelerometers in hobbyist-friendly packages like DIP so the breakout board is a necessity.  I only really need one axis but I might as well get three so I can use this as a platform for other projects.  I could use a serial interface but the ATMega328 has only one serial peripheral, so if I use it for the accelerometer I can't have a serial port for debugging.  Given that, I'm going to try to find a three-axis accelerometer with voltage outputs.  If you go to their site on the left they have their categories: choose Sensors/Accelerometers/3-Axis.  There are about a dozen options.  We can immediately ignore the ones that aren't breakout boards.  Next, we can ignore the ones that have serial interfaces - that is, either I2C or SPI.  That pretty much leaves one option: the MMA7361.

Power is the last issue, but there's a problem.  The accelerometer is 3.3V.  The ULN2803 prefers 5V.  The green LED has a forwad drop of about 3V.  The microcontroller could be either 5V or 3.3V.  From this, it looks like the LEDs will have to be driven with 5V and the microcontroller will also have to be 5V so it can control the ULN2803.  But we need 3.3V for the accelerometer as well.  There isn't an easy solution to this: we need both a 5V and a 3.3V source.  The 5V source has to be pretty hefty as well - each set of LEDs will draw about 600mA.  Only one set should be on at a time - by design and because otherwise it will draw so much power that the battery will die quickly.  For that sort of current draw I need another old standby - the LM7805.  It can supply up to 1A at 5V and is more than enough to power the LEDs and all of the 5V components. If you search for LM7805 on Digi-Key you get the option of either a Texas Instruments version at $1.66/unit or a Fairchild version at $.66/unit.  The difference seems to be that the TI part has no lead in it.  I'm going to choose the cheaper version: LM7805CT-ND.

For the 3.3V regulator I just typed '3.3V regulator' into the Digi-Key search and selected the 'Linear-LDO' category. I know this from experience - the 7805, LM317 and other similar voltage regulators I've used are all linear so I stuck with it.  After selecting 'In-Stock' I drilled down into the columns this way:
  • Regulator Topology: Positive Fixed - I don't need adjustable or negative voltages
  • Voltage Output: 3.3V - That's all I need
  • Mounting Type: Through-hole - I tend to prefer through-hole
  • Output Current: 250mA - I need so little current, but in case I want to use this part again in the future I prefer to select a part that can accommodate more applications
That leaves part #497-7246-1-ND as the 3.3V regulator. 

The last major thing I need is some sort of battery.  I'd like to not screw around with some combination of AA batteries - they only go up to 1.5V (1.2V for rechargeables) so If I have to generate 5V that means at least 4, possibly more batteries.  A 9V battery seems ideal - plenty of voltage overhead.  Keep in mind though, 9V batteries tend to have about 500mAH of capacity.  That's not a lot of time to run this costume, but it's the tradeoff I have to make to accommodate everything else.  It's mostly for novelty anyhow.  So I decided I wanted a nice 9V battery holder and not just a couple of wires and a connector.  If you search Digi-Key for '9V battery holder' you're presented with the 'Battery holders, clips and contacts' category in which you can drill down the columns thusly:
  • Battery Cell Size: 9V - Obviously
  • Number of Cells: 1 - Also obvious
At this point there weren't very many results, so I looked through all of the pictures to find this guy: BH9V-PC-ND.  It's a through-hole full 9V cell holder.  The other options were pretty much just the contacts.  This will securely hold the battery and allow me to mount it on the PCB.

All of the other parts are just miscellaneous: tactile switches, rectangular headers, resistors, capacitors, LEDs, jumpers, etc.  All of that I have on-hand and don't have to order.  With any luck I don't have to make any other supplemental orders to get more of anything or replace something I broke.  When ordering parts make sure to note whether you're close to a price break.  I needed about 30-35 LEDs (the number I thought I needed plus extras) and was able to get a slight price break by ordering a few more than I thought I needed.  And of course, always always order extras - you WILL break something.  I broke plenty of LEDs and perhaps a few microcontrollers during this process.

Next time I'll talk about capturing/generating the schematics.  For now I'll leave you with a bit of a teaser: pictures of the board after assembly.


Asic Chip Design said...

Thanks for this post. It provides some great insight.

Nissi Shruti said...

Use of LED in home and industrial application is different and many applications.