Sep 30, 2011

Power Your Next Project

I explained an idea I had for a Halloween animation using pumpkins, LEDs, microcontrollers, and a PIR sensor in my last post. Making all these items work with each other sounds all well and good, but none of them will even turn on without a stable power supply. Being a power electronics guy I am all about power supplies and efficiency, which is why I am dedicating this post to telling you how to get a power solution for your next project that just works…as all power supplies should. I will show you an example using my Halloween animation project.

First, you need to identify all the necessary voltages and currents that your circuit elements are likely to require. The microcontrollers I am using operate on 5Vdc, the LEDs use anywhere from 2Vdc to 4.5Vdc, and the PIR sensor can accept any voltage from 5-12Vdc (a higher voltage gives you more range). To narrow this down I am going to choose 5Vdc for all my elements. The LEDs can be driven directly off the microcontroller output pins, which will output the supply voltage on any pin I set high in the code.

But there is a problem with this approach. Each microcontroller will require its own 5V supply rail and there are four of them. Running four 5 volt power supplies off some extension cord is really impractical and a mess aesthetically. Instead, the simple solution is to use batteries to power each of the devices. Go do a Google search for “5V battery” and see what you can find for me. I will be here when you get back.


Well that was fun. I’m guessing your search turned up nothing and now you have come back here looking for answers. Suckers. The truth is batteries do not come in all types of voltages because their performance is directly related to their cell chemistries.

But let’s say you were able to find a 5 volt battery. Would that be good to use to power all these devices? In short, no. Even if you were able to find a viable 5V battery, it would likely discharge below a useable voltage before too long. Batteries do not maintain their nominal voltage and then just suddenly plummet to zero at the end of their life. Instead, what tends to happen is that you will get a gradual decay in the voltage present on the battery terminals over time like in the picture on the right, but the exact curve is largely dependent on cell chemistry. Just because the system cannot run off the battery at a certain point does not necessarily mean the battery is dead. If you did try to use a 5V battery for these modules you would end up throwing away lots of energy capacity because the voltage fell too low for the circuits.

Now is about the time I tell you that if you try to use a higher voltage and count on the voltage dropping over a usable range then you will likely blow up your device. So how is it possible then to get to voltage to stay at 5V despite battery discharge when dropping too low shuts everything down and going too high blows everything up? While there are many answers to this question, the one I must shamefully recommend is the voltage regulator.

Why am I ashamed to tell you to use a voltage regulator? Well, for reasons I will go into in my entry on voltage regulators, they are very inefficient. Using a DC-DC converter like a SEPIC or a Buck would be far more efficient, but at the cost of design time, price, and complexity. Voltage regulators will provide a constant output regardless of the input voltage, though there are caveats. First, voltage regulators need the input voltage to be higher than the output voltage. If I apply this concept to my Halloween project I find that I need to apply a higher voltage to the input of the regulator than the 5Vdc I need out of it. A 9V battery should do nicely.

Secondly, voltage regulators have what’s called a “dropout voltage” rating, which tells you the minimum input voltage above your desired output. For instance, the LM7805 pictured above is one of the most famous and widely used regulators on the planet (look into the LM317 for another). It provides a stable 5Vdc output with input voltages up to 40Vdc and a typical dropout voltage of about 2 volts (for future reference, this figure can vary with loading conditions). That means that if you want an output of 5 volts, your input voltage must be at LEAST 7 volts. Certain regulators can operate down to a dropout voltage of 300mV or lower. We creatively call these regulators “Low-dropout” regulators, or LDOs for short.

Since most voltage regulators rely on feedback loops to control the output voltage, you will likely need to add some bypass capacitors on the output. This can sometimes be a hidden cost associated with voltage regulators, but they are usually not a big problem. Make sure to check out the datasheet for the regulator in question because often times it will tell you what size capacitor you need. The image below shows the sample circuit configuration for getting a stable 5 volt output from the LM7805, and you can see that they recommend using capacitors on both the input and output pins.
I will show you how I chose the regulator I am going to use in my Halloween project in another post. This entry was meant to be a high level look at how to get common voltages for your next project (3.3V, 5V, 9V, 12V). If you need something other than 5V, never fear because lots of regulators are adjustable so you can configure components around it to get the output voltage you want. Again, the part's datasheet should give you some information on how it can be done. Datasheets are an engineer’s best friend.

Sep 28, 2011

Halloween Preparations

Now that we are rolling into October it’s time to kick off the holiday projects. I really enjoy these types of projects because it gives me a chance to come up with ideas that wouldn’t make sense at any other time of the year. Plus the holidays give me a fixed deadline for getting stuff done, which make me actually put in effort. Granted, this isn’t the coolest or most complex project on the planet, but it does give me a chance to get back into microcontrollers and work with some different enclosures (e.g. pumpkins).

There are a few elements to this undertaking that, implemented individually, are not hard do, but it can be tricky to make them all work well together. I made an attempt to illustrate how the system should operate in the graphic below. Follow along if you can.

First, I am going to use a Passive Infrared (PIR) sensor to detect motion outside my door on Halloween. When kids come to the door for candy, the sensor will detect their motion via infrared radiation and output a voltage. If the sensor doesn’t detect motion it pulls the voltage down to zero. I am planning on using these triggers to enable microcontrollers in four pumpkins. At this point, I am still trying to come up with ideas on how I can relay the trigger from the sensor to the four micros in the pumpkins. My first instinct was to use RF transmission, but I am also considering Zigbee and additional IR channels. Either way I want to go wireless to clean up the presentation and avoid kids tripping over excessively long lines of hook-up wire.

Once the PIR sensor sends out the “enable” signal to the four microcontrollers they will start an LED animation. By animation I mean that the pumpkins will flash at different times and in different patterns to create a small light show. The key to making this all work will be the timing between all four microcontrollers. In the worst case, I can hardcode the timing into all four controllers by basically guessing and checking until everything works correctly. However, to make things more dynamic and harder on myself I want to find a way to set the timing using the analog-to-digital converters (ADC) in the micros. By using a potentiometer to vary the voltage I feed into the ADC, I can control the delay used in executing the code. If I do it correctly, I think I should be able to let the code run and adjust the timing on the fly without having to reprogram the chips constantly.

So far all that I have managed to do is gather up some fake foam pumpkins from Oriental Trading for the aesthetics (see the picture). I found similar items in Michaels for slightly more money ($1/pumpkin) if anyone out there is interested but doesn’t feel like waiting for shipping. I also picked up a pair of artificial pumpkin carving tools from Michaels for about $3, which I have already found very useful.

I will be using the PIC16F690 microcontroller from Microchip because I happen to have four of them in my parts bin, but given the simplicity of this project just about any micro will do – PIC, AVR, MSP430, etc. I have also started looking at LED diffusers to spread the light across the inside of the pumpkin evenly. I tried something like this last year and the LEDs created hotspots with poor light dispersion.

My goal is to get some audience participation on this one. I am looking for any feedback on how best to put this all together. Give me your opinions on faces to carve in the pumpkins (leave links if you can), LED colors to use, animations to try out (no fire, please), ways to communicate between the various pieces, extra features to tack on, and anything else you want to share.

I will of course post more on this project as Halloween approaches. Those entries will likely be short update posts to discuss any issues I am having or to show my progress.

Sep 15, 2011

Atmel STK600 Unboxing

In an effort to keep things fresh here on To the Rails, I wanted to shoot an unboxing video of my new STK600 AVR development kit from Atmel. Unfortunately, I have neither the voice nor the patience to pull it off. At some point I will post the outtakes video so we can all celebrate my ineptitude, because, unlike the bum over at Sculptor by Day (see link below), I believe failure is worth showing off. I will be incorporating more video demonstrations into posts as time goes on so look for those in the future – though I probably won’t be saying anything.

Let me also take this time to give a shout out to Amelia Dalton over at EEJournal (see my Electronics Links tab). I was lucky enough to win this kit as part of her “Nerdy giveaway” that she does each week on her Fish Fry podcast. I highly recommend tuning in if you are interested in the latest and greatest electronics news. She also does interviews with the higher ups of various electronics companies, often discussing their latest technology and where the company is headed. So thanks Amelia for giving me the chance to hack around with this kit for free (Retail: $200).

The AVR architecture was developed by two students in Norway, and the first AVR microcontroller was manufactured in a Norwegian ASIC house in 1996. I know I haven’t talked about microcontrollers in this blog before, but believe me there will be an in depth discussion coming at some point. For now, just know that a microcontroller is essentially a chip that is capable of being programmed to perform various I/O functions (i.e. read signals, output signals, perform mathematical operations, etc.). AVR is Atmel’s family of microcontrollers that competes with the MSP430 series from TI and the PIC line from Microchip, among several others.

Anyway, back to the board. The STK600 is an 8-bit and 32-bit development board and starter kit for the entire line of AVR microcontrollers. Near as I can tell it came out some time back in late 2007 as a replacement to the STK500. Notable upgrades include a 20MHz system clock, USB connectivity for programming, and more I/O functionality. It was actually released simultaneously with several other development boards, all of which were codenamed after Norse mythology. The STK600 is codenamed Odin.

I have embedded the YouTube video above where Atmel introduces the line of AVR development tools that includes the STK600, and I think it’s worth checking out if for no other reason than the concept art they use for the first two minutes or so of the video.

What makes this board unique is the routing system Atmel has implemented to make it compatible with their entire line of 8-bit and 32-bit micros. It works using a sort of programming sandwich. The top layer consists of a microcontroller loaded onto a “socket card”, which is a card that holds certain packages (shapes) of microcontrollers. The middle layer is called a “routing card”, which takes the pins of the microcontroller you are trying to program from the socket card and routes them to the correct places on the main board. This is often necessary because not all chips use the same programming or I/O pins. The bottom layer is the main board, where all the hardware for interfacing with the microcontroller resides. You can secure the sandwich together using plastic screws that are provided in the kit (pictured).

The STK600 kit also includes a USB 2.0 A/B cable, two 10-wire cables for programming with the JTAG header, one 2-wire cable for shorting jumpers, and an ATmega2560 microcontroller (8 bits, 40 pins) pre-soldered onto a board for testing/introduction purposes. You can see a picture of what comes in the kit above.

While the sandwich system makes this board very versatile, it also creates a huge burden on developers because you need to purchase a new routing card and possibly a new socket card each time you have to program a different type of microcontroller. In the hobbyist realm where I operate, Dual-Inline Packages, or DIPs, are the most popular because you don’t have to deal with getting a custom PCB for surface mounting. The DIP socket card is $49 and the subsequent routing cards for various micros go for $17 on Atmel's online store. While I am not particularly happy about having to drop $66 plus shipping on this board just to make it useful, I look at this as an opportunity to experiment with AVRs so I am willing to make the investment. Actually, I have found kits that sell the DIP socket card with 7 routing cards for around $110, so I may go that route.

One of the links below will take you to the technical specs for the STK600 on Atmel’s website if you are interested in taking a look. I will be sure to update on my progress working with this unit in the future.

Routing Cards on Atmel’s Store:

Sep 11, 2011

Volts Kill

I hear people say all the time, “voltage is not as dangerous as current”. If you are like me, when you hear this you immediately think “shouldn’t natural selection have taken care of these weak minded dregs by now?”, but later come to find that, alas, they still exist by the thousands. So to any of my normal readers and those who may have stumbled upon this blog by happenstance let me say this once and for all: voltage creates current flow! Do not ever assume that because something is “low-voltage” that is cannot deal you any significant damage. For the record, most industry products are considered “low-voltage” if they run off of less than 48 volts, which is enough to cause you discomfort. It also depends heavily on the voltage source of interest. Constant voltage sources will deliver a current according to Ohm’s law. Capacitors or other energy storage devices, on the other hand, are capable of delivering massive amounts of current quickly despite a “low voltage” on the capacitor.

Figure 1. Capacitor Discharge into a CD, Courtesy of Ben at Buxtronix
So how does a statement like this become so widely accepted? Well, like many scientific myths there is a grain of truth embedded in the nonsense. The truth is that human skin is generally quite tolerant of certain voltages because its resistance is high enough that the voltage source cannot provide, or drive, enough current through us to cause physical harm. The amount of current passing through your body is what will ultimately kill you so in that sense it’s true that current is the most dangerous aspect of working with electricity. However, there can be no current flow without a voltage source to push the current through a channel. That’s the end of the argument if there ever was one. But just to prove my point, let’s attempt some science.

Ohm’s law applies to humans as well as electronic components, but humans are not purely resistive. We are capable of building up charge like a capacitor and then discharging that energy through our skin. For instance, when you rub your socks along the carpet or a balloon against your hair you are building up charges in your body. To release the charges, you need to touch a grounded piece of metal, creating a static shock.  In order to model this effect in humans, researchers usually use what’s called an RC circuit. I will cover capacitors and RC circuits more in future blogs so don’t get weighed down in how these circuits operate. I will give you the highlights.

The human charge equivalent circuit is pretty simple: a 100pF capacitor and a 1.5kohm resistor in series like in Figure 2. The capacitor is capable of charging up to the source voltage, but it is only 100 picofarads meaning it stores very little energy at low voltages. In fact, we can figure out exactly how much energy is stored in this capacitor with a simple formula: E = 0.5*C*V2.
 Figure 2. Human static charge model

Let’s assume that you have charged yourself up to 10,000 volts (10 kV) by building up static charges on your body – and, yes, this voltage is typical of a strong static shock. Using the formula we get a total energy of 5 millijoules. For reference, you have radiated about 500 joules away as heat since you started reading that last sentence. I have also taken a picture of a low voltage 100pF capacitor so you can see exactly what sort of charge tank we are dealing with here.

Figure 3. Scale of a low-voltage 100pF capacitor

*****WORD OF WARNING*******
Here is where it is important to make the distinction between voltage and charge in a capacitor. The amount of energy that a capacitor can hold will increase with capacitance and it is critical that you understand this if you ever plan on handling larger capacitors. For example, let’s assume that a 100 farad capacitor is charged to 2 volts. Noobs may think, “only 2 volts, pfff, what’s the big deal?”. Here’s the big deal: its 100 farads of capacitance. Using the same equation as above, we find that this capacitor when fully charged will hold 200 joules, which is 40,000 times more energy than humans store in our bodies during a decent static shock. THIS AMOUNT OF CHARGE CAN KILL YOU!!!! If you were to discharge this capacitor through your body it would be equivalent to getting a shock from a defibrillator, which would mean game over noob.

How is it possible then that 5mJ of energy could be responsible for the sparks we see when we get a static shock? Hey, why do you ask so many freaking questions? (Sigh)… If I must explain… it has to do with air’s dielectric field strength. All you really need to remember is that voltage is like an electrical pressure, and with enough pressure you can knock down damn near anything. In the electrical sense, this means that with a high enough voltage you can get just about any material to conduct electricity, including air. Air’s dielectric field strength is rated anywhere from 10 – 30kV/cm depending on atmospheric conditions. That means if you had a 10kV voltage source (like your charged body) and a conductor (like a grounded piece of metal) 1 centimeter apart you would be able to ionize the air between the two and create a conductive path for electrons. This dielectric field strength is why you don’t get static shocks from across the room.

But there is another factor in this equation that we haven’t really considered:  time. The length of time you are exposed to a certain voltage has a huge impact on whether you survive or not. When you discharge yourself into a ground, there is a large initial current that flows through your body. Fortunately for lovers of shag this surge of current lasts only a fraction of a second and decays rapidly in magnitude (such is the nature of RC circuits). I have illustrated the expected current flow of our example static shock in Figure 4.

Figure 4. Example static shock discharge curve

You can see that at the very first instant a current path is created between you and ground, a current in excess of 6.5 amps is flowing out of your body. However, just 150 nanoseconds later the current drops to less than 2.5 amps. After just 1.5 microseconds, the current will drop below 300 microamps and you won’t feel anything anymore. While the initial current drain was large, the overall energy dissipated was small and it was dissipated quickly so no harm was done to the individual.

This may be the case in with static shocks and voltage, but what about electric circuitry? In electric circuits there generally isn’t a finite amount of charge stored somewhere that will dissipate once you touch it. Instead, a continuous current will flow through your body causing severe pain, cardiac arrest, muscle paralysis, and eventually death assuming the current is large enough. Again, though we are worried about continuous current flow and its magnitude, the voltage is ultimately what will push the current through your body.

Just to show some stuff blowing up, check out the video below. Ben Buxton of Buxtronix works with high voltages and large capacitances on a fairly regular basis. In the video, he uses a 0.25 microfarad capacitor charged to 23kV, which is about 150 joules according to our earlier equation. Check out the best way to blank a CD.

That ended up being a lot more information than I wanted to give in this post. For clarity, let’s review the important notes again:
  1. Voltage, charge, and current are all tied together. Saying one is more deadly than another is ridiculous.
  2. Energy storage devices like capacitors differ from generic electric circuitry because they are capable of delivering a massive amount of energy into a load (like you) very quickly.
  3. The amount of energy a capacitor can hold is dictated by the capacitance and the voltage. A 100 picofarad capacitor at 10kV has much less energy than a 100 farad capacitor at 2 volts.
I will never discourage someone from starting to work with electronics and high current applications. However, the hobbyists out there need to understand the nature of charge before you get into anything serious or you could up severely hurting yourself and/or someone else. Mistakes will happen, but make sure you have taken the precautions to be able to play another day noobs.

Sep 7, 2011

Series-Parallel Quiz Solution #2

Once again, one reply was all it took to get the right answer. I posted the circuit in Figure 1 just over a week ago after my second EE Fundamentals entry on the differences between series and parallel connections. The question asked you to find the total power dissipation in the circuit. You needed to apply concepts from all the previous EE Fundamentals segments to solve the problem.  

The correct answer: 3.6 watts.
 Figure 1. Quiz circuit from the last EE Fundamentals Post

Similar to my first quiz, the easiest way to solve this problem is to reduce the circuit to its simplest equivalent model. Step 1 is to look into the circuit from the perspective of the source, which is a voltage source in this case. Looking into the circuit from the left, we note that the source sees two 10 ohm resistors in series with a 20 ohm resistor on the far side. Adding up these values according to the rules of series connections we find that these three resistors can be reduced to a 40 ohm resistor. We now have a circuit that looks like Figure 2 below.
Figure 2. The circuit from Figure 1 after reducing the far right side

We want to keep reducing the circuit from right to left. After combing the first three resistors, we now have two 40 ohm resistors in parallel with each other. When two resistors of the same value are in parallel with each other, their equivalent resistance is half their nominal value. In this example, that means we can model them with a 20 ohm resistor like in Figure 3. If these resistances were different values, you would need to use the parallel resistance rules from my first series-parallel post.
Figure 3. Equivalent circuit after series and parallel resistance reductions

At this point, it looks just like what we started with in Figure 1 before reducing the circuit. I would hope that by now you could finish this up since you just do the same thing over and over until there is only one resistance left. When you are finished, you should end up with the circuit in Figure 4.
 Figure 4. Final equivalent model of the circuit in Figure 1

Cool. So we found the equivalent circuit….but that wasn’t the question I asked. To find the power dissipation you will have to make use of the hints I gave you in the quiz question. The power loss in a circuit is found by summing the power losses of all its elements. Power dissipation in a circuit element is equal to the voltage across the element multiplied by the current through the element. If we look at our circuit, we only have one element to worry about assuming the wires connecting the resistor to the source are ideal conductors.

So here is what we know:
  • Supply Voltage: 12 volts
  • Equivalent circuit resistance: 40 ohms
  • Power Loss = V * I in the resistor
To get the current, use ohms law (I = V/R) and divide the voltage across the resistor by its resistance. You should find that the current is equal to 300 milliamps. Therefore,

Power = V*I = 12 volts * 0.3 amps = 3.6 watts.

It is possible to go branch by branch and sum the various voltages, currents, and power losses, but that should not change the answer. Equivalent circuit models like the one we used in Figure 4 are great for looking at this type of circuit because they break it down to its simplest form while keeping the analysis completely valid. Next time, we solve RC circuits.