Beginning Embedded Electronics - 1.1

Lecture 1 - Background and Power Supply



Page 1


       This is a series of lectures written for those with mild electronics background (aka Sophomore in Electrical and Computer Engineering) to learn about the wild world of Embedded Electronics. I assume only that you know what electricity is and that you've touched an electrical component. Everything else is spelled out as much as possible. There is quite a lot here so take your time! It is also my intention to get book-hardened EE's students to put down the calculator and to plug in an LED. Remember, if it smokes, at least you learned what not to do next
time!

Sorry for the confusion. When these tutorials were written and photographed, we used the ATmega8. We now carry the newer ATmega328. You will find all ATmega328 information in the following pages, but the pictures will show an ATmega8.

What's a Microcontroller?

       You may know what an OR gate is. An OR gate is a logic gate that takes two inputs and controls an output. You may have played with these types of gates, even possibly a DIP packaged OR gate with 4 OR gates built into it. This DIP package required a power pin and a ground pin. Electricity flowed through the IC and allowed it to operate. You may not be sure how the IC was built, but you understand that if you change the inputs, the output changes. You can do this by tying the inputs to either power (also known as VCC) or ground (GND).You probably played with one of the DIP ICs in a breadboard. If any of this is completely alien to you, don't fret! We'll try to ease you into it.

      A microcontroller is the same as an OR gate. You have some inputs, you have outputs. The crazy thing is that a micro runs code. Machine code to be specific. For instance, with a little bit of work, you can monitor the input of two pins A and B. And based on those inputs, you can control an output pin C. So to replicate an OR gate:


      if (A == 1 || B == 1)
     {
          C = 1;
      }
      else
      {
           C = 0;

      }

      It's C code! You can code up all sorts of different applications, compile code, load it onto a micro, power the micro, and the code runs. Very simple! Microcontrollers are used in all the electronics you take for granted such as your microwave, TV remote, cell phone, mouse, printer, there's over 150 microcontrollers embedded into new cars! There's one waiting for you to depress the brakes (BRAKES == 1) and for the tires to lock up (LOCK_UP == 1). When this happens, the micro releases the brakes, and you have ABS (anti-lock brake system).


       In the old days, microcontrollers were OTP or one-time-programmable meaning you could only program the micro once, test the code, and if your code didn't work, you threw it out and tried again. Now micros are 'flash' based meaning they have flash memory built inside that allows their code to be written and rewritten thousands of times. I've been programming micros for years and always burn out the microcontroller far before I hit the limit of

flash programming cycles.

      Flash micros are different than computers and RAM. Computers require tons of power and components to get up and running. Computers run HOT. Computers take forever and a day to boot. Micros are on and running code within milliseconds and if they're warm enough you can feel heat coming off of them, something is very wrong and you've probably blown the micro. Oh - and micros cost about $2.


      Now back to that OR gate IC. It had a bunch of pins, all dedicated to being either inputs or outputs of the various built-in OR gates (4 gates in one package = 8 inputs, 4 outputs, 2 power/gnd pins). 14 pins of fun. Now with a micro, the most basic pin function is GPIO - general purpose input/output. These GPIO pins can be configured as an input or an output. Very cool. Each input pin can be monitored and acted upon. Example:


      if (PORTC.2 == 1)

      then do something...
Each output pin can be pushed high or low. Example:

      while(1)

      {
             RB3 = 1;
             delay_ms(1000);
             RB3 = 0;
             delay_ms(1000);

      }

       Guess what that code does? It toggles a pin high/low every 2 seconds. Fancy right? This is the 'Hello World' of the microcontroller world. It seems trivial, but by god when you've been trying to get a micro up and running after 5 hours of tearing your hair out and you see that LED blinking for the first time, it's just glorious!



What types of microcontrollers are there and how do I get one blinking?

Here's a very shallow breakdown of the micros in my world:


  • PIC - This is the classic micro from Microchip. Very simple, very proven, but it lacks many of the features that other mfg's are building into their chips. This is a big deal for me. I was a die-hard PIC person for years and I've started to see the limits of PICs and the benefits of other micros!
  • AVR - This is basically a direct competitor of PICs. They do everything a PIC does, but in my new opinion, better, faster, cheaper, and simpler.
  • MSP - These are very good micros by Texas Instruments (TI), not as beefy as AVR or PICs. However they truly excel at low-power applications. More on this later, but imagine running a complete system on one AA battery for 5 years. This is in the realm of nano-amp current consumption. Crazy!
  • ARM - Why are all these three letters? I don't know actually... ARMs are the new kids on the block and they are huge. Very powerful, very low-cost, they are taking over the world but can be really intimidating if you've never played with a micro before.
  • 8051 - The '8051 core' was the de facto standard in 8-bit (and 4-bit!) microcontrollers. Developed by Intel in the 1980s, it still seems to be the instruction set they love to teach you in college. They are based on archaic, but field proven instruction sets. Very old tech in my humble opinion, but these ICs have been significantly improved over the years (now Flash based, ADC, SPI, etc.).
  • 68HC08/11 - Another very common instruction set developed by Motorola. Extremely popular, and a micro commonly taught at university, it's the microcontroller I love to hate. These original micros often lack on-board RAM and flash based memory.

Google any of these for more info. I have chosen the ATmega168 as the learning IC of choice. Why?


  • 20 MIPs (million instructions per second!) is powerful enough to do some really cool projects
  • It's cheap! $2.13 currently
  • It's got all the goodies under the hood (UART, SPI, I2C, ADC, internal osc, PWM, kitchen sink, etc)
  • 16K of program memory is enough for almost any beginner project
  • The tools are free! (C compilers for many of the other micros cost a lot of money)
  • The programming and debugging tools are low cost ($20 will get you started)

With a little work and probably $40 worth of parts, you too can get an LED blinking. As with any new hobby (also known as a drug addiction), the extra cost of 'goodies' can grow very quickly.



You want to play microcontrollers today?

      With any IC, you need to power the thing. There are two power connections on basic micros : VCC and GND. What the heck is VCC? This is the label for the positive voltage. Don't worry, after a few days of this, seeing 'VCC' will become very normal. GND is short for ground. All electrical current needs a way to flow back to ground. This can be called 'common' but is often just labeled GND.

      There are thousands of different micros out there, but 5V (five volts) is the typical VCC. 3.3V is also typical but you'll also see 2.8V and 1.8V VCCs on more exotic micros. For now, just worry about 5V and GND.



Where do I find this 5V?

      You need to hook up 5V and GND to your micro. Your house outlet runs at 110V AC (or 220V for many
countries). AC = alternating current and is very bad for 5V DC (direct current) micros. So you'll need to convert the 110V AC from your outlet to a useable 5V DC.

Quick note: If you reverse the connection on your micro - bad things happen. Always make sure your 5V power supply is connected to the VCC pins and GND to GND. If you reverse this and connect 5V to GND on the micro and GND to VCC on the micro, things won't explode, probably no smoke, things will probably heat up like crazy, and you'll probably damage your $2 micro. You probably will. I did. Many times. Try not to do it.


Ok! You need 5V. Time to build a simple voltage regulator circuit!


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


      The most common regulator is called the LM7805. Why? I dunno. I've never actually touched a component with LM7805 stamped on the outside. There's always other letters stamped on the outside like 'LM7805' or 'LV78X05' or some such craziness  Just know that there are many 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 in a breadboard, you'll also want it in the TO-92 or TO-220 package. More about packages in a later lecture, just go with it for the moment.


You've got your regulator in hand, you've got the wall wart. Time to connect them up.



      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 hook up a lot of these. When in doubt, always check the datasheet before hooking up a new part - or else be close to the on/off switch! Input is the input voltage of anything greater than about 7V. GND is ground. Output is the 5V output pin. Your wall wart 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. One more time - connect all grounds. This is the #2 reason why novii can't get a system to work. For our breadboard, we will be inputting 9V (or whatever transformer you've got up to about 15V) and outputting 0V (GND) and 5V to our breadboard rails.



We are going to go through a bunch of iterations of the power supply, adding parts as we go. Shown above, 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 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 wall wart connected to the GND pin on the regulator.


      Cool, But why doesn't the multimeter read 5.000V? Electronics are not that good. The cheap-o regulators are /-5% tolerate meaning you'll see 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 be worried about ripple. There is noise coming in the input pin, the regulator tries hard, but some of that noise gets onto 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 know someone with a 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 200mV of noise.


      Whoa - what's a filtering cap? Filtering capacitors are large bulky capacitors that help smooth out ripple. There've been lots of analogies about capacitors so here's another one for ya:


      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 see happen? Unfortunately not really. 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, or 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 basic caps...



100uF (one-hundred micro farad) on the input and 10uF on the output. You will use a lot of 100uF and 10uF around power systems and you will eat 0.1uF (point one micro farad) 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 (1ouF 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, where

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 is indicating that the 100uF and 10uF cap 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 is a positive pin and a negative pin.

  • Ceramic caps: These are the cheapest and most common cap you'll play with on a breadboard. They are NOT polarized so you can stick em in the breadboard any way you want. Ceramic caps cannot handle as large of capacitance as electrolytics so you'll need both on your breadboard system.
  • There are many more different kinds of capacitors but for the sake of your head exploding, we won't cover them here.


Okay - now you 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. Negative marked pin goes to ground, the other goes to the input pin.



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



     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 where 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 looks 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 6V rail, 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 10V cap backwards 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 'derate' 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.


      Back to our power supply! Don't worry about blowing things up just yet, you should be at low enough voltages you won't do any harm. Again, if things heat up/smoke/spark, just unplug or turn off the system. Speaking of turning things off - we need a power switch!




      This will allow you to turn on/off the system. Handy. It can get really annoying pulling and inserting the power wires to power/kill your system.


      Inside the small black enclosure, is a switch. The switch has three pins. It looks like a see-saw inside. The center pin is always connected to the middle of the see-saw and as you slide the switch back and forth, the see-saw rocks up and down. Slide the switch forward and the see-saw shorts from the center pin to the forward pin. Slide the switch back and the see-saw disconnects from the forward pin and shorts to the rear pin. We recommend you connect power to the center pin of the switch. When you slide the switch forward, power will short to an unconnected pin and do nothing (no power to your system). Slide the switch back and the center

power pin will short to the wire running into your regulator, delivering power to your system (power on!).

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




      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 backwards 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 on to our boards. If you plug the wrong type of wall wart onto a board, we want to protect you from yourself.


cont - 2 >

You may also like:-

1. 89SXX Development Board



No comments:

Post a Comment