Introduction to Embedded Electronics

Introduction to Embedded Electronics

Extracted from: Robot. Head to Toe Magazine Volume 7 Dec 2010

Robotics requires knowledge and skill in both hardware and software. Yet, I always stress hardware fundamentals. As basic and simple as a 5V supply is, many embedded development failure is caused by it. Let's kick off with an introduction to embedded electronics.

Introduction

Embedded electronics involve microcontrollers and of course, other electronic components such as resistors, capacitors, connectors, voltage regulators, logic gates, transistors, diodes, and many more. Microcontroller plays the main role in an embedded project to function. Nevertheless, other small components too should not be belittled as most failures in the embedded systems are caused by these tiny electronic components. So let's start with the power supply.

Playing with Microcontrollers?

Given any IC, its fundamental requires you to power it up. There are two power connections on the basic microcontroller: VDD (sometimes called VCC) and VSS (which is actually GND). What on earth is VDD? This is the label for positive voltage. Don't worry, after a few days of this, seeing 'VDD' will soon be a norm. VSS or GND is the abbreviation for ground. All electrical current needs a way to flow back to the ground. The VSS is occasionally called 'common' but is often labeled as GND.

There are thousands of different microcontrollers out there, but 5V (five volts) is the typical VDD. 3.3V is also typical but you'll also see 2.8V and 1.8V VDDs on more exotic microcontrollers. For now, we will just focus on 5V and GND.

Where do I find this 5V?

You can get all the parts mentioned here from many places, for instance, a common electronics store should have such parts in stock.

You need to hook up 5V and GND to your microcontroller. Our houses' supply runs at 240V AC (or 110V for many countries). This type of supply, which is the AC or alternating current, will definitely damage your 5V DC (direct current) microcontrollers. So you'll need to convert the 240V AC from your outlet to a useable 5V DC.

Quick note: If you reverse the connection on your microcontroller - bad things happen. Always make sure your 5V power supply is connected to the VDD pins and Ground to GND. If you reverse this and connect 5V to GND on the microcontroller and GND to VDD on the microcontroller, it will not explode, probably no smoke, but it will probably heat up like crazy, and you'll most likely damage your RM20 microcontroller. You will most probably, because I did it, numerous times. A piece of advice will be to try not to do it.

OK! To sum things up, you need a 5V power supply. Time to build a simple voltage regulator circuit!

You can buy something known as the AC to DC adapter. The common adapter takes a higher voltage in AC and converts it to a lower voltage in DC. DO NOT assume all adapters that are labeled '5V' will output 5V. This is a major misconception. To be safe, just hook up your multimeter to the barrel plug and see what is the real output voltage. You might get more like 8 or 9V. This will kill your microcontroller too. This is because some of the adapters are unregulated power supplies. So be careful.

Let's assume you are using an adapter output of something higher like 9V. Unfortunately, this 9V output is rather noisy - meaning there is a lot of ripples. What does ripple mean? When you want a DC voltage it means that you want a solid voltage (the opposite of alternating). Some adapter uses some cheap tricks to get 240V AC down to 9V DC. So the DC signal coming out of the adapter tends to alternate 100-500mV. Instead of a solid 9V DC, you see a signal that rises and falls between 8.5 and 9.5 volts. This 'ripple' can cause havoc to your system; furthermore, 9V is too high (we need 5V!) so we need to pass 240V through this adapter and send the 9V through a regulator to get down to a clean 5V DC signal. If all of these sound scary - don't worry. After you get your 5V power system built, you'll wonder why you were freaking out in the first place (it's simple, I swear).

The most common regulator is called the LM7805. Why? It is a surprise just to know that there are many manufacturers out there and they are all producing the same basic part, with small tweaks to each one. What you need is one of these generic parts that is designated as a '5V linear regulator. If you're playing on a breadboard, you'll also want it in the TO-92 or TO-220 package. More about packages later in another topic, just go with it for the moment.

You've got your regulator in hand, and you've got the adapter. It's time to connect them up.

A Typical LM7805 5V voltage regulator

 

Here you can see the 'pin-out' of the LM7805. Say 'IGO' in your head and commit this to memory (Input, Ground, Output). You'll probably encounter a lot of these. When in doubt, always check the datasheet before hooking up a new part - or else stay near the on/off switch! Input is the input voltage of anything greater than about 7V. GND is ground whilst Output is the 5V output pin. Your adapter should have two wires. One is 9V, the other is GND. All grounds need to be connected together for current to flow across the system and as a voltage reference point. I'll have to stress this part again - connect ALL grounds. This is the #2 reason why novices can't get a system to work. For our breadboard, we will input 9V (or whatever transformer you've got up to about 15V) and output both 0V (GND) and 5V to our breadboard rails.

clip_image001[8]

Basic LM7805 Connection

 

We will be going through a bunch of iterations on the power supply, adding parts as we go. Shown in the basic LM7805 schematic, we have a basic regulator configuration. 9V in, we should see a rough 5V on the output.

Schematic note: The two ground pins are not shown as connected. We assume that nets (the green wires) of the same name are connected together. Schematics can get big and complex, so you won't see all the wires together, but in your breadboard, you need to connect all the GND pins together. In this case, it's the GND wire from your adapter connected to the GND pin on the regulator.

Isn't it easy? Try measuring the output this time. If you still find the output a little way off than 5.000V, do not throw this book away. It is normal but why doesn't the multimeter read 5.000V? The answer lies in the fact that electronics are not that good. The cheap regulators have a +/-5% tolerance which explains exactly why you'll see an output between 5.25 and 4.75V. In practice, you should see between 5.1 and 4.9V with most run-of-the-mill regulators. You can of course spend many $$ and get tighter tolerances but 5.1-4.9V will work fine for our purposes.

Now we should divert our focus onto the ripples I've mentioned beforehand. There is noise coming into the input pin, and the regulator tries hard, but some of those noises still find their way to the output pin. Your multimeter says 5.08V, but that's because it's averaging many readings together and showing you only the average. Do you happen to know someone with an oscilloscope? If so, show them this tutorial and ask them to show you the noise on your 5V rail. With no filtering caps, you could see as much as 200 mV of noise.

Whoa, wait - what's a filtering cap? Filtering capacitors are large bulky capacitors that help smooth out the ripple. There've been lots of analogies about capacitors so here's another one: "Capacitors act like water tanks. When your circuit pulls a bunch of water out of the system, the capacitor helps hold the voltage up temporarily until the power system can catch up. For example, you may live in a city with water and water pressure. If you take a shower you affect the pressure in the municipal water system ever so slightly. If everyone turned on their shower and flushed every toilet in the city, odds are the water pressure would fluctuate quite a bit! A big water tank helps minimize these pressure fluctuations. A big cap helps minimize the voltage fluctuations on your breadboard."

Is this something you can witness with your own eyes? Unfortunately, the answer is no. You can probably run your system without filtering caps, but it's not good engineering practice. Give it a whirl without caps! But when things don't work, you'll wonder if it's the caps, your code, or your timing, or maybe you blew out the sensor. Too many unknowns will make you crazy. My recommendation: just use a couple of basic caps.

clip_image001[10]

LM7805 with 2 Capacitors

 

100uF (one-hundred micro farad) at the input and 10uF at the output. You will use a lot of 100uF and 10uF around power systems and you will eat 0.1uF (point one microfarad) caps like candy around micros. These two caps should smooth the input into the regulator and will smooth the output nicely.

Capacitors cannot deliver their stored energy instantaneously. Larger caps (10uF and 100uF) store more energy, but they react more slowly. The smaller the capacitor, the faster it can deliver its stored energy.  If you have a large power outage (power dips for 10-100ms), a big cap (100uF to 1000uF) will help "hold up" the falling voltage. A smaller cap (0.1uF) will help suppress higher frequency noise and shorter power dips (noise in the 1us to 100us range). Therefore, 0.1uF caps are located near the microcontroller to help with short bursts, whereas 100uF and 10uF caps are used on the power rails.

Now you see the schematic symbol looks a bit odd. What's with + and curved lines? This schematic component indicates that the 100uF and 10uF caps are polarized. Oh Jeebus, what's that? Time for a capacitor breakdown: "    Electrolytic caps: These are larger caps capable of storing 10uF to 1,000,000s of farads. They are cheap and great for bulk capacitance. They are polarized meaning there are both positive and negative pins.

clip_image001[12]

Electrolytic Caps

 

The cap has a minus '-' sign on the cover indicating that the pin needs to go to GND. "    Ceramic caps: These caps are the cheapest and most common caps you'll play with on a breadboard. They are NOT polarized so you can stick them in the breadboard any way you want. Ceramic caps cannot handle large capacitance as electrolytes do so you'll need both on your breadboard system. "    There are many more different kinds of capacitors but these two should be sufficient for this tutorial.

OK, next you will need to work through some logic here. You know the positive part of the 100uF cap needs to be connected to the input pin, but only the negative pin is marked. Yes, it's confusing - but you'll get used to it. The negative marked pin goes to the ground, the other goes to the input pin.

What happens if you get them switched? Well here's where things may go poof.

clip_image001[14]

From the left: Bad, good, ugly

 

This is what happens when you over-voltage or reverse voltage a polarized capacitor. The middle cap is normal. The cap on the left, you can see the top is slightly raised up. This is what happens when the electrolyte inside expands. And the cap on the right shows us what happens when this pressure is so great, it busts through the metal top. Notice the '+' imprinted into the tops of these caps? That imprint is there so that if the pressure does build up, the cap will fail like the unit on the right - rather than blowing the top half of the cap across the room.

This picture was taken from the inside of an old Gateway computer (circa 1999). Gateway had used some 'marginal' 1000uF/16V capacitors. The 16V means they are rated to 16V. A 16V rating means they can withstand voltages up to 16V but no more. These caps were sitting on the 12V rail to smooth out the ripple but obviously, they were failing. Gateway was trying to save $0.50 by using a capacitor that was too close to the maximum. Manufacturing is not perfect! With any production run, the population of capacitors and their tolerance look like a bell curve. The majority of the 16V rated caps can withstand 16V. Some can 18V, even 22V! But the tolerance bell curve goes both ways; a small number of the capacitors rated at 16V will fail at 10V, some at 8V. You get a big enough ripple on the 12V line and you could pop the 16V rated cap. This is why most engineers talk of 'de-rating' capacitors. If you have a 5V rail, you do not stick a 5V rated cap on the rail! A good rule of thumb is to de-rate any capacitor by 50%. So a 12V cap is good to be used on a 6V rail, a 24V cap on a 12V rail, etc.

Guess what happens when an electrolytic cap fails like the ones above? They quit working. In most cases, they 'fail safe' meaning they won't work as a capacitor anymore but they won't short to ground. The real fun begins when the failure is so bad that the internals fuse together and you get a short to ground - then you can have some fun meltdowns! In the case of this computer, the motherboard had all sorts of bad software failures because the power supply had too much ripple! The big filtering caps on the power supply had failed so the 12V was all over the place.

Similar failures can happen if you reverse the polarization of the cap. If the voltage is low (less than around 25V) the cap will probably just be damaged a bit. If you've got a vacuum bell sitting around and you want to really cause some damage, ask a trained professional to hook up the 10V cap backward to 10,000V. It should instantaneously blow up like a popcorn kernel.

For your power supply filtering caps, I recommend using a 25V rated 100uF cap (100uF/25V) on the input and a 10uF/10V cap on the output. Engineers will tell you to 'de-rate' the cap by 50% meaning if the label says 100V don't trust it past 50V. This is generally good practice. Following this idea, our 100uF/25V is good for inputs up to about 12.5V before we should worry that we may pop the electrolytes. Again, not mandatory, just don't expect a 5V-rated cap to withstand a 9V input.

Remember all the warnings about reversing VDD and GND and how that is bad? Well if you connect your power supply backward, that's bad. So let's protect ourselves!

clip_image001[16]

With diode and protection and switch to On and OFF

 

That's a diode (marked D1). A diode lets current flow in one direction (in the direction of the arrow) and it blocks current from flowing in the opposite direction. This will allow 9V to flow in the right direction, and if you accidentally hook your power supply up the wrong way, it will block current from flowing backward and damaging your system. Is it overkill? Pretty close. But we always design them into our development boards because we don't know what type of power supply you knuckleheads (also known as our paying customers) will plug into our boards. If you plug the wrong type of wall wart onto a board, we want to protect you from yourself.

There are some downsides to a protection diode: "    All diodes have a voltage drop, meaning 9V on one side will drop to about 8.5V on the other. So your 9V adapter just became 8.5V. "    Diodes have a current rating. If you try to suck 1A (1 amp) through a 0.1A (one hundred milli-amp) rated diode, the diode will quickly heat up and fail. For reverse protection, we recommend a 1A 1N4001/7 diode. These are dirt cheap and very common.

Note that diodes are polarized. They have a direction that you need to pay attention to. Many diodes have a band indicating the cathode. What's a cathode? Go google it. All you really need to know is that the line on the schematic part is the same as the line on the diode. If you can't remember which is which, remember 'arrow is for anode'. Cheesy, yes.

So if you want to install this 'reverse protection diode', the 9V from your adapter goes into the end of the diode without the band (the anode). The banded end (cathode) goes into your switch. Your switch then goes into the input. Throw the switch and you should see 5V on the output using your multimeter. Nifty. But I am tired of using my multimeter each time to check the 5V output. There must be a better way! Time to wire in the power LED.

Light-emitting diodes (LEDs) are bits of silicon that light up when current flows through them. Go google for the science. As a general rule of thumb, LEDs can have a 20mA max current flowing through them before they begin to fail.

clip_image001[18]

Adding LED to 5V supply

 

So if you hooked up your LED like in the above schematic, it would light up very bright for a split second and then burn out. That's because the LED is a diode and the current will flow from the anode (arrow) to the cathode (line) to the ground - uncontrolled! The silicon will flow current at something like 1 amp for a split second and burn up. To limit this current flow to 20mA, we need Ohm's law. Yeah, the bookworms in the room suddenly perked up: V = IR (this is Ohm's law)

 

If we have 5V, and we only want 20mA flowing through the LED: 5V = 0.02 * R R = 250 Ohm

 

Now, this is not completely true because the LED has a forward voltage drop, but don't worry too much about that. Hooking up LEDs is very common with microcontrollers. All you need to remember is that you're going to need to limit the current. The most basic way to do this is with a resistor. 220 Ohms will work (LED will be brighter), 330Ohm is also good (LED a bit dimmer), and 1K (1000) will work as well. 220, 330, and 1K are more common resistor values.

 

I highly recommend you get your hands dirty. Hook up an LED to a 1k resistor, then a 330, then a 220, 100, 50, then finally blow the thing up by hooking it with no resistor. That was fun right? Good. You had a backup right? Once the bit of silicon inside the LED is burned out, it is no good and the LED can be thrown away.

clip_image001[20]

Adding resister to LED in series

Our final power supply circuit. It seems like a lot of work, but once you set this up on your breadboard, you might never take it off. This is the basis for all things microcontrollers. The input voltage may change, and the output voltage may change (to 3.3V for example), but the basics are all there. Flip the switch and you should have a nice 5V rail and an LED that lets you know that everything is good. If the LED does not light up, that means that something else on the 5V rail is sucking so much current that the LED cannot light up. This is a very strong indicator that something is wrong. If you turn on your system and the Power LED does not turn on, immediately turn off the system and check your wiring and debug it.

Check out the next article about the loading program for PIC Microcontroller.

References:

Original article by Sparkfun Electronics: http://www.sparkfun.com/commerce/tutorial_info.php?tutorials_id=57, author: Nathan Seidle